struts2 jquery标签选择不加载数据

时间:2014-12-13 05:55:41

标签: jquery ajax json struts2

我正在尝试使用<sj:select>标记在Struts2 jsp中呈现我的列表。

在同一地点,我也使用了标签

数据在标记中填充,而<sj:select>标记中没有填充任何数据。

  • struts2-jquery-plugin ver:3.7.0
  • jQuery UI:1.8.2
  • struts2-json-plugin ver:2.3.16.3

这也用于:

<sj:head jqueryui="true" />

正常使用struts2 选择标签,给我结果

<s:select list="languageList"></s:select>

但是在同一个jsp中,但是只使用另一个div,即使使用相同的对象(List)这里命名为languageList,也没有任何内容,如片段中所示

        <s:url id="remoteurl" action="sample2"/> 
    <sj:select 
        href="%{remoteurl}" 
        id="echo" 
        name="echo" 
        list="languageList" 
        emptyOption="true" 
        ="-1" 
        headerValue="Please Select a Language"
/>

根据建议编辑了问题。

我的意思是说动作类正在返回对象languageList,但是jsp中的UI无法呈现一个,请从O / P屏幕检查快照以便更好地理解。

请帮我弄清楚这里有什么问题?

我试图在struts jQuery select上复制教程,如链接所示:http://struts.jgeppert.com/struts2-jquery-showcase/index.action

Source :: JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sb" uri="/struts-bootstrap-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Double Combo</title>

<sj:head jqueryui="true" />
<sb:head includeScripts="true" includeScriptsValidation="true" />
<meta http-equiv="X-UA-Compatible"
    content="IE=EmulateIE7; IE=EmulateIE9">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport"
    content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no" />

<!-- begin jquery -->

<script src="<%=request.getContextPath()%>/jq/jquery-1.8.2.min.js"
    type="text/javascript"></script>

<script src="<%=request.getContextPath()%>/jq/functions.js"
    type="text/javascript"></script>

</head>
<body>
    <div id="simple">
        <s:select list="languageList"></s:select>
    </div>

    <s:form id="formSelectOne" action="echo" theme="simple"
        cssClass="yform">
        <fieldset>
            <legend>AJAX Form populated by a String List</legend>
            <div class="type-text">
                <label for="echo">Echo: </label>
                <s:url id="remoteurl" action="sample2" />
                <sj:select href="%{remoteurl}" id="echo" name="echo"
                    list="languageList" emptyOption="true" headerKey="-1"
                    headerValue="Please Select a Language" />
            </div>
            <div class="type-button">
                <sj:submit targets="result1" value="AJAX One" indicator="indicator"
                    button="true" />
                <img id="indicator" src="images/indicator.gif" alt="Loading..."
                    style="display: none" />
            </div>
        </fieldset>
    </s:form>



</body>
</html>

行动类:

package com.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.*;

import com.opensymphony.xwork2.ActionSupport;
@ParentPackage( value = "showcase")
public class Sample extends ActionSupport{

    private static final long serialVersionUID = -2223948287805083119L;
    private List<String> languageList;
    private List<ListValue> languageObjList;
    private Map<String, String> languageMap;

    @Actions({
        @Action(
        value="/sample2", 
        results={
        @Result(name="success",type="json")
        }) 
    })
    public String execute() {

      languageList = new ArrayList<String>();
      languageObjList = new ArrayList<ListValue>();
      languageMap = new HashMap<String, String>();

      languageList.add("Java");
      languageList.add("PHP");
      languageList.add("C++");

      languageMap.put("J", "Java");
      languageMap.put("P", "PHP");
      languageMap.put("C", "C++");

      languageObjList.add(new ListValue("J", "Java"));
      languageObjList.add(new ListValue("P", "PHP"));
      languageObjList.add(new ListValue("C", "C++"));

      return SUCCESS;
    }

    public String getJSON(){
        return execute();
    }

    public List<String> getLanguageList()
    {
      return languageList;
    }

    public Map<String, String> getLanguageMap()
    {
      return languageMap;
    }

    public List<ListValue> getLanguageObjList()
    {
      return languageObjList;
    }

    public class ListValue {
    private String myKey;
    private String myValue;

    public ListValue(String myKey, String myValue) {
      super();
      this.myKey = myKey;
      this.myValue = myValue;
    }

    public String getMyKey()
    {
      return myKey;
    }

    public void setMyKey(String myKey)
    {
      this.myKey = myKey;
    }

    public String getMyValue()
    {
      return myValue;
    }

    public void setMyValue(String myValue)
    {
      this.myValue = myValue;
    }
  }

}

Action Sample2返回此json:

{"JSON":"success","languageList":["Java","PHP","C++"],"languageMap":{"P":"PHP","C":"C++","J":"Java"},"languageObjList":[{"myKey":"J","myValue":"Java"},{"myKey":"P","myValue":"PHP"},{"myKey":"C","myValue":"C++"}]}

2 个答案:

答案 0 :(得分:0)

您不需要两次加载jQuery,不同的版本可能会相互冲突。这段代码错了

<script src="<%=request.getContextPath()%>/jq/jquery-1.8.2.min.js"
    type="text/javascript"></script>

<script src="<%=request.getContextPath()%>/jq/functions.js"
    type="text/javascript"></script>

如果您的操作返回JSON,sj:select标记应该有效,但它使用旧的表示法。将其更改为

<s:url var="remoteurl" action="sample2"/> 
<sj:select 
    href="%{#remoteurl}" 
    id="echo" 
    name="echo" 
    list="languageList" 
    emptyOption="true" 
    headerKey="-1" 
    headerValue="Please Select a Language"
/> 

检查jquery,json插件是否在类路径上并且taglib工作正常。

答案 1 :(得分:0)

<强>解决。

这可能对其他人有所帮助,

我所要做的就是更改web.xml,如下所示:

 <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <session-config>

之前我已将过滤器设置为

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
  <session-config>

所以 struts2-jquery标记找不到相应的 .js 文件

解决方案:从<url-pattern>*.action</url-pattern>更改为<url-pattern>/*</url-pattern> 因此,请务必查看web.xml这样的异常情况,这些情况在我的案例中经常被忽视。