JSP 中的 GWT AutoSuggest

发布于 2024-10-23 13:34:54 字数 65 浏览 5 评论 0原文

我想在 JSP 中添加 GWT 自动建议文本框。

有人可以对此提供一些见解吗?

谢谢

I would like to add a GWT autosuggest textbox in JSP.

Could someone provide some insight into this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长不大的小祸害 2024-10-30 13:34:54

通常,GWT 被视为 Web 应用程序框架,与小部件框架不同。就我个人而言,我认为 GWT 太重了,无法仅向简单的网页添加自动建议,而是使用类似 jQuery autocomplete< /a>.

话虽如此,运行 GWT 代码并没有什么神奇之处。遵循 GWT 标准模块布局,只需将 JSP 页面设置为 GWT 主机页面,您可以在其中将路径更改为编译模块的绝对路径。

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.

神经大条 2024-10-30 13:34:54

这是我如何让建议框发挥作用的示例。当用户打字时,我对数据库进行 RPC 调用。

我同意你可以在 jQuery 中做类似的事情,但是当 GWT 有可用的小部件时你为什么要这样做呢?

希望这有帮助!

在此处输入图像描述

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;
}

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!

enter image description here

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文