小编典典

struts2 jQuery标签选择不加载数据

ajax

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

在同一位置,我也使用了标签

标签中已填充数据,而标签中未填充数据<sj:select>

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

这也被使用:

<sj:head jqueryui="true" />

正常使用struts2 select 标签,可以得到结果

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

但在同一个JSP,而只是一个内部的div给什么,即使同一对象(名单) 这里命名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

资料来源:: 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;
    }
  }

}

动作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++"}]}

阅读 288

收藏
2020-07-26

共1个答案

小编典典

解决了。

希望这对其他人有帮助,

我要做的就是更改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,这些异常通常被我忽略。

2020-07-26