views:

1161

answers:

1

I used the jQuery autocompletion for my Struts2 application.

Pratically, my action made a list of strings that jQuery use. This is the script:

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

During typing appear the box with the suggestions. The problem is that the box render another value, exactly render the code of my JSP ( links to CSS for the autocomplete plug-in).

How can I solve this?

This is my 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>

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

Now, the Action:

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

At the end the struts.xml

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