Struts2 + jQuery自动完成

时间:2010-01-04 15:06:25

标签: dojo struts2 jquery-autocomplete

我在jQuery应用程序中使用Struts2自动完成功能。

实际上,我的动作制作了一个jQuery使用的字符串列表。这是脚本:

$().ready(function() {
        $("#tag").autocomplete("/myAction/Action", {
            multiple : true,
                autoFill : true,
                    minChars:1
                });
            });

在输入过程中出现带有建议的框。问题是该框呈现另一个值, 准确呈现我的JSP代码(指向自动完成插件的CSS链接)。

我该如何解决这个问题?

这是我的JSP:

<html>
<head>
    <script src="<%=request.getContextPath()%>/scripts/jquery-latest.js"></script>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/scripts/main.css" type="text/css" />
    <link rel="stylesheet" href="<%=request.getContextPath()%>/scripts/jquery.autocomplete.css" type="text/css" />
    <script type="text/javascript" src="<%=request.getContextPath()%>scripts/jquery.bgiframe.min.js"></script>
    <script type="text/javascript" src="/<%=request.getContextPath()%>/query.dimensions.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/scripts/jquery.autocomplete.js"></script>
    <script type="text/javascript">
    $().ready(function() {
        $("#tag").autocomplete("/myAction/Action", {
            multiple : true,
                autoFill : true,
                    minChars:1
                });
            });
</script>
</head>
<body>
    <s:form action="Action" theme="simple">
        <s:iterator value="elencoMateriali" var="asd">
            <s:property value="#asd" escape="false"/>   
                    </s:iterator>
        <s:textfield id="tag" name="tagField" label="tag" />
    </s:form>
</body>

2 个答案:

答案 0 :(得分:1)

我认为答案为时已晚,但问题仍然存在于Struts2中。我已经做了一些修改来解决这个问题:

  1. 将jquery.struts2.js文件复制到js路径中。

  2. 编辑jquery.struts2.js fil并在Autocompleter Widget的句柄中查找“success:function(data)”,并替换下一个函数:

    success: function(data) {
    var x = 0;
    if (data[o.list] !== null) {
        var isMap = false;
        if (!$.isArray(data[o.list])) {
            isMap = true;
        }                                   
        var result = [];
        $.each(data[o.list], function(j, val) {
            if (isMap) {
                result.push({
                    label: val.replace(
                            new RegExp(
                                    "(?![^&;]+;)(?!<[^<>]*)(" +
                                    $.ui.autocomplete.escapeRegex(request.term) +
                                    ")(?![^<>]*>)(?![^&;]+;)", "gi"
                                ), "<strong>$1</strong>" ),
                    value: val,
                    id: j
                });
            }
            else {
                if (o.listkey !== undefined && o.listvalue !== undefined) {
                    result.push({
                        label: val[o.listvalue].replace(
                                new RegExp(
                                        "(?![^&;]+;)(?!<[^<>]*)(" +
                                        $.ui.autocomplete.escapeRegex(request.term) +
                                        ")(?![^<>]*>)(?![^&;]+;)", "gi"
                                    ), "<strong>$1</strong>" ),
                        value: val[o.listvalue],
                        id: val[o.listkey]
                    });
                }
                else {
                    result.push({
                        label: data[o.list][x].replace(
                                new RegExp(
                                        "(?![^&;]+;)(?!<[^<>]*)(" +
                                        $.ui.autocomplete.escapeRegex(request.term) +
                                        ")(?![^<>]*>)(?![^&;]+;)", "gi"
                                    ), "<strong>$1</strong>" ),
                        value: data[o.list][x],
                        id: data[o.list][x]
                    });
                }
            }
            x++;
        });
        response(result);
    }
    

    }

  3. 现在,如果您在jsp代码上订阅onSelectTopics方法,您将有一个新的item.id元素可用,因此您可以将id值设置为隐藏的o,无论您想要什么。

    < / LI>

    现在,你的自动填充器会显示一个包含强词的列表,用选择填充输入,并将id保存在你可以捕获的变量中。

    请记住在include标题部分添加修改后的j。

答案 1 :(得分:0)

<%@ taglib prefix="s" uri="/struts-tags" %>
    <%@ taglib prefix="sx" uri="/struts-dojo-tags" %>

<s:form action="ActionJson" theme="simple">
    <s:url id="elencoFuffa" action="ActionJSON"/>
    <sx:autocompleter  id="autocompleter" name="materiale" loadOnTextChange="true" preload="false" href="%{elencoFuffa}" size="24" loadMinimumCount="1" showDownArrow="false" cssClass="dropDown" onchange="submit();"/>

        <s:submit value="Prosegui"/>
        <s:property value="luoghiSmaltimento"/>
    </s:form>  

现在,行动:

public class Action extends ActionSupport {

private String materiale;
private String luoghi;
private List<String> elencoMateriali;
private Map<String, String> json;

public String execute() throws Exception {

    System.out.println("------------------" + materiale);

    return SUCCESS;
}

public String getMateriali() throws Exception {
    MaterialiDAO dao = new MaterialiDAO();
    List<Materiali> toResult = new ArrayList<Materiali>();
    toResult = dao.findAll();
    elencoMateriali = new ArrayList<String>();
    Iterator i = toResult.iterator();

    while (i.hasNext()) {
        Materiali m = (Materiali) i.next();
        elencoMateriali.add(m.getValueNomemateriale());
    }

    return SUCCESS;

}

public String getJson() throws Exception {
    json = new HashMap<String, String>();

    if (materiale != null && materiale.length() > 0) {
        MaterialiDAO dao = new MaterialiDAO();
        List<Materiali> materiali = dao.findByLikeValue(materiale);
        for (Materiali m : materiali) {
            json.put(m.getValueNomemateriale(), "" + m.getIdMateriali());
        }
    }

    return SUCCESS;
}

public String trovaLuogo() throws Exception {
    LuoghismalDAO daoL = new LuoghismalDAO();
    MaterialiDAO daoM = new MaterialiDAO();
    Materiali m = daoM.findByMaterialiName(materiale);

    if (m != null) {
        luoghi = m.getLuoghismal().getValueDescrluogo();

        return SUCCESS;

    } else {
        luoghi = "-- Scegliere un materiale --";
        return SUCCESS;
    }
}

/* Getters and setters */

最后是struts.xml

        <action name="ActionJSON" class="it.action.Action"
        method="getJson">
        <result type="json">
            <param name="root">json</param>
        </result>
    </action>