问题
I would like to add a GWT autosuggest textbox in JSP.
Could someone provide some insight into this?
Thanks
回答1:
Typically GWT is considered a web application framework which is different to a widget framework. Personally I would consider GWT too heavy to just add an autosuggest to a simple web page and instead use something like jQuery autocomplete.
Having said that, there's nothing magical about running GWT code. Follow GWT standard module layout and just set up your JSP-page as a GWT host page where you alter the paths to be absolute to your compiled module.
回答2:
Here an example of how I was able to get a suggest box to work. I make an RPC call to the database while the user is typing.
I agree that you could do something similar in jQuery but why would you when GWT has the widget available?
Hope this helps!
vendorSuggestBox = new SuggestBox(new SuggestionOracle()); //client package
public class SuggestionOracle extends SuggestOracle { //shared package
public boolean isDisplayStringHTML() {
return true;
}
@SuppressWarnings("unchecked")
public void requestSuggestions(Request request, Callback callback) {
ItemMovementRemoteServiceAsync service=GWT.create(ItemMovementRemoteService.class);
service.getVendors(request, new SuggestionCallback(request,callback));
}
@SuppressWarnings("unchecked")
class SuggestionCallback implements AsyncCallback {
private SuggestOracle.Request req;
private SuggestOracle.Callback callback;
public SuggestionCallback(SuggestOracle.Request _req, SuggestOracle.Callback _callback) {
req=_req;
callback=_callback;
}
public void onFailure(Throwable caught) {
callback.onSuggestionsReady(req, new SuggestOracle.Response());
}
public void onSuccess(Object result) {
callback.onSuggestionsReady(req, (SuggestOracle.Response) result);
}
}
public SuggestOracle.Response getVendors(Request req) { //server package
Connection db=null;
PreparedStatement ps=null;
ResultSet rs=null;
SuggestOracle.Response resp = new SuggestOracle.Response();
List<Suggestion> suggestions=new ArrayList<Suggestion>();
int count=0;
try {
db=Database.open("ACM0");
ps=db.prepareStatement(
" SELECT VE_CD,upper(VE_NAME) VE_NAME" +
" FROM AP.VE_WEB " +
" WHERE (VE_NAME NOT LIKE 'AC Moore%') " +
" AND (lower(VE_NAME) LIKE ? OR VE_CD LIKE ?)" +
" ORDER BY VE_NAME");
ps.setString(1, "%"+req.getQuery().toLowerCase()+"%");
ps.setString(2, "%"+req.getQuery().toLowerCase()+"%");
rs=ps.executeQuery();
while(rs.next() && count < 25) {
suggestions.add(new ASuggestion(rs.getString("VE_NAME").trim()+"-"+rs.getString("VE_CD").trim()));
count++;
}
resp.setSuggestions(suggestions);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
Database.close(db);
}
return resp;
}
public class ASuggestion implements IsSerializable, Suggestion { //shared package model object
private String s;
public ASuggestion(){}
public ASuggestion(String s) {
this.s=s;
}
public String getDisplayString() {
return s;
}
public String getReplacementString() {
return s;
}
来源:https://stackoverflow.com/questions/5367091/gwt-autosuggest-in-jsp