如何将自动完成器中的选定值作为URL中的参数发送?

时间:2015-05-06 14:26:46

标签: java jquery url autocomplete struts2

我正在使用Struts 2jQuery插件。

我有一个sj:autocompleter的JSP需要在sj:a中提交其值作为参数。

<s:url id="loadClientURL" action="loadClient"/>
<sj:autocompleter
        id="client"
        name="client"
        list="listClient"
        href="%{loadClientURL}"
        formIds="guardarVBForm"
        loadMinimumCount="2" 
        size="70"
/>
<s:url id="myActionURL" action="myAction"> 
    <s:param name="client" value="%{client}"/>
</s:url>
<sj:a href="%{myActionURL}" cssClass="btn" targets="errorMsg" dataType="json">
    Select Client
</sj:a> 

2 个答案:

答案 0 :(得分:0)

我有这个问题,我更喜欢以不同的方式解决,而不是使用struts jquery标签。这是自动完成的代码:

的index.jsp

$( "#tags" ).autocomplete({
      source: : function(request, response) {
            $.ajax({
                url : 'blog/listTag.html',
                type : "POST",
                data : {
                    term : request.term
                },
                dataType : "json",
                success : function(jsonResponse) {
                    response(jsonResponse.tagList);

                }
            });
            },
    });
  });

struts.xml中

<action name="listTag" method="listTag" class="com.action.BlogAction">
            <result  type="json"/>
        </action>   

BlogAction.java

private String term;
private List<String> tagList=new ArrayList<String>();

//getters and setter 

public String listTag() {
        logger.info(Logger.EVENT_SUCCESS, "listTag");

        try {
            tagList=facade.listTag(term);

       } catch (Exception e) {

            logger.error(Logger.EVENT_FAILURE,
                    "could not list tag , error: *" + e.getMessage()
                            + "*");
}

BlogService.java

@Override
    public List<String> listTag(String term) {
        // TODO Auto-generated method stub
        List<String> list = new ArrayList<String>();

        String queryText = "select distinct name from Tag t where t.name like :term";

        try {
            TypedQuery<String> query = (TypedQuery<String>) em
                    .createQuery(queryText.toString());
            query.setParameter("term", "%" + term + "%");
            list = query.getResultList();

        } catch (Exception e) {
            logger.error(Logger.EVENT_FAILURE, e.getMessage());
            return null;
        }
        return list;

    }

我认为这会对您有所帮助,并为您提供自动填充的建议!

答案 1 :(得分:0)

感谢您的想法,我已经解决了这个问题:

JSP

    <div class="ui-widget ui-widget-content ui-corner-all" style="padding: 10px; margin-bottom: 10px;">
        <label><b>Search Client(*):</b></label>
        <s:url id="loadClientURL" action="loadClient"/>
        <sj:autocompleter
                id="client"
                name="idClient"
                list="listClient"
                href="%{loadClientURL}"
                loadMinimumCount="2" 
                size="70"
        />
        <s:url id="autocompleteClientURL" action="autocompleteClient"/>
        <sj:a href="%{autocompleteClientURL}" cssClass="btn" targets="errorMsg" formIds="saveForm" dataType="json" onCompleteTopics="checkError">Mostrar Información del Cliente</sj:a>
   </div>
   <div>
       <%-- label showing some customer information --%>
   </div>

Struts的

<action name="autocompleteClient" class="myClass" method="autocompleteClient">
             <result type="json">
                <param name="ignoreHierarchy">false</param>
                <param name="includeProperties">actionErrors.*,actionMessages.*,fieldErrors.*,**</param>
            </result>
            <result name="error" type="json">
                <param name="ignoreHierarchy">false</param>
                <param name="includeProperties">actionErrors.*,actionMessages.*,fieldErrors.*,**</param>
            </result>
</action>

Java Action方法

public String autocompleteClient(){

        if (this.idClient!=null && !this.idClient.isEmpty()){
            try{
                ItClient client = this.myservice.findClient(Integer.valueOf(this.idClient));
                if (client !=null){
                     return SUCCESS;
            } catch(Exception e){
                LOG.error(e.getMessage());
                addActionError(e.getMessage());
                return ERROR;
            }
        }
        else{
            addActionError("idClient is null");
            return ERROR;
        }

}


public String loadClient(){
        this.listClient = new LinkedHashMap<String, String>();

        List<ItClient> list = null;
        if(term!=null && !term.isEmpty())
            list = is.findAllClientsByName(term);
        else    
            list = is.findAllClients();
        Iterator<ItClient> it = list.iterator();
        while (it.hasNext()){
            ItClient clientAux= it.next();
            listSolicitante.put(clientAux.getId().toString(), clientAux.getNombre());
        }
        return SUCCESS;
    }