質問

私が使用したのは、 jQuery 私のオートコンプリート ストラット2 応用。

実際、私のアクションは 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

<%@ 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>

他のヒント

答えを出すには遅すぎると思いますが、Struts2 にはまだ問題が残っています。このような問題を解決するためにいくつかの変更を加えました。

  1. jquery.struts2.js ファイルを js パスにコピーします。

  2. jquery.struts2.js ファイルを編集し、「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 値を任意の非表示に設定できます。

これで、オートコンプリーターは強い単語を含むリストを表示し、入力に選択内容を入力し、キャッチできる変数で ID を管理します。

変更した js をインクルード ヘッダー セクションに忘れずに追加してください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top