Domanda

Ho usato il jQuery completamento automatico per il mio Struts2 applicazione.

In pratica, la mia azione ha fatto una lista di stringhe che utilizzano jQuery. Questo è lo script:

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

Durante la digitazione appaiono la scatola con i suggerimenti. Il problema è che la casella renda un altro valore, esattamente rendono il codice dei miei JSP (link ai CSS per il plug-in di completamento automatico).

Come posso risolvere questo?

Questo è il mio 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>

È stato utile?

Soluzione 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>  

Ora, l'Azione:

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 */

Al termine del struts.xml

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

Altri suggerimenti

Credo che sia troppo tardi per una risposta, ma i probles è ancora rimanendo in Struts2. Ho fatto alcune modifiche per risolvere tale problema:

  1. Copiare il file jquery.struts2.js in voi JS percorso.

  2. Modificare il fil jquery.struts2.js e cercate 'successo: funzione (dati)' nel manico del Autocompleter Widget, e sostituire la funzione per il prossimo:

    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. Ora, se ti iscrivi un metodo onSelectTopics sul vostro codice JSP, si avrà un nuovo elemento item.id disponibili, in modo da poter impostare il valore id in una nascosta o quello che vuoi.

Ora, la tua autocompletamento mostrano una lista con la parola forte, riempie l'ingresso con la selezione, e mantiene l'id in una variabile si può prendere.

Ricordate di aggiungere i js modificati nella sezione includere intestazione.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top