javascript 使用 Struts 2.2.x 的简单 JSON 示例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5906491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:45:47  来源:igfitidea点击:

Simple JSON example using Struts 2.2.x?

javascriptjsonjspstruts2

提问by johnnieb

I'm having trouble getting JSON results working with Struts 2.2.1.1.

我在使用Struts 2.2.1.1 时无法获得 JSON 结果。

Does anyone have a simple working example that returns a JSON result to a JSP using Struts 2.2.1.1and is ready to run in Eclipseas a dynamic web project?

有没有人有一个简单的工作示例,它使用Struts 2.2.1.1将 JSON 结果返回给 JSP ,并准备好在Eclipse 中作为动态 Web 项目运行

Please include the struts.xml, action class and JSP code. Also, note dependencies. Thank you.

请包括 struts.xml、动作类和 JSP 代码。另外,请注意依赖性。谢谢你。

回答by johnnieb

Here's how to create a simple JSON example using the Struts 2 jQuery plugin.

下面是如何使用 Struts 2 jQuery 插件创建一个简单的 JSON 示例。

  1. Go to Struts2 jQuery Plugin Showcase
  2. Navigate to Ajax Forms > Buttonset / Checkboxes
  3. Review the code for Buttonset that was populated from AJAX JSON Result. This is code I selected to create a simple example.
  4. Create dynamic web project in Eclipse
  5. Create a Java package and name it test.
  6. Download the Struts 2 jQuery plugin showcase source(struts2-jquery-showcase-x.x.x-sources.jar) and extract the JAR file.
  7. Import Echo.java, JsonSample.java, and ListValue.javainto the testpackage and move the code into the package with quick fix.
  8. Change the class annotation in Echo.javaand JsonSample.javato @ParentPackage(value = "test")
  9. In addition to the standard Struts 2 libraries, ensure that the struts2-json-plugin-x.x.x.jar, struts2-jquery-plugin-x.x.x.jar, and struts2-convention-plugin-x.x.x.jarfiles are in your classpath.
  10. Create a struts.xmlfile and add the following XML:

    <struts>
        <constant name="struts.devMode" value="true" />
        <constant name="struts.convention.action.packages" value="test" />
        <package name="test" extends="json-default” namespace="/">
        </package>
    </struts>
    
  11. Create an index.jsp file and insert the following code:

    <s:form id="form2" action="echo" theme="xhtml">
     <s:url id="remoteurl" action="jsonsample" />
    <sj:checkboxlist href="%{remoteurl}" id=“remoteCheckboxlist” name="echo"  list="languageList" label="Language" />
    <sj:submit targets="formResult" value="AJAX Submit" indicator=“indicator” button="true"/>
    </s:form>
    
  12. Run the example.

  1. 前往Struts2 jQuery 插件展示
  2. 导航到Ajax 表单 > 按钮集/复选框
  3. 查看从 AJAX JSON Result 填充的 Buttonset代码。这是我选择来创建一个简单示例的代码。
  4. 在 Eclipse 中创建动态 Web 项目
  5. 创建一个 Java 包并将其命名为test
  6. 下载Struts 2 jQuery 插件展示源( struts2-jquery-showcase-xxx-sources.jar) 并提取 JAR 文件。
  7. 导入Echo.javaJsonSample.java以及ListValue.java进入测试包和代码转移到包速战速决。
  8. Echo.java和 中的类注释更改JsonSample.java为@ParentPackage(value = "test")
  9. 除了标准的 Struts 2 库之外,请确保struts2-json-plugin-xxxjarstruts2-jquery-plugin-xxxjarstruts2-convention-plugin-xxxjar文件在您的类路径中。
  10. 创建一个struts.xml文件并添加以下 XML:

    <struts>
        <constant name="struts.devMode" value="true" />
        <constant name="struts.convention.action.packages" value="test" />
        <package name="test" extends="json-default” namespace="/">
        </package>
    </struts>
    
  11. 创建一个 index.jsp 文件并插入以下代码:

    <s:form id="form2" action="echo" theme="xhtml">
     <s:url id="remoteurl" action="jsonsample" />
    <sj:checkboxlist href="%{remoteurl}" id=“remoteCheckboxlist” name="echo"  list="languageList" label="Language" />
    <sj:submit targets="formResult" value="AJAX Submit" indicator=“indicator” button="true"/>
    </s:form>
    
  12. 运行示例。

回答by lschin

Must see: struts2-x.x.x-all.zip /apps/struts2-showcase-2.2.1.war

必看:struts2-xxx-all.zip /apps/struts2-showcase-2.2.1.war

Struts 2 and JSON example
Struts 2 autocompleter + JSON example

Struts 2 和 JSON 示例
Struts 2 自动完成器 + JSON 示例

回答by dbyuvaraj

It is very simple to get Json work with struts2.

使用 struts2 使 Json 工作非常简单。

For this,

为了这,

  1. you need to add struts-json plugin*(jsonplugin-0.32.jar)* to classpath.

  2. Your struts.xml file should extends json-default

    <package name="base" namespace="/" extends="json-default">
    
  3. Your action result be like this.

    <result type="json"><param name="root">jsonData</param></result>
    
  4. Inside action class, declare json as

  1. 您需要将 struts-json plugin* (jsonplugin-0.32.jar)* 添加到类路径。

  2. 你的 struts.xml 文件应该扩展json-default

    <package name="base" namespace="/" extends="json-default">
    
  3. 你的行动结果是这样的。

    <result type="json"><param name="root">jsonData</param></result>
    
  4. 在动作类中,将 json 声明为

private LinkedHashMap<K, V> jsonData new LinkedHashMap<k, V>();

private LinkedHashMap<K, V> jsonData new LinkedHashMap<k, V>();

and then add the result list to json like

然后将结果列表添加到 json 中

jsonData.put("result", anyList or object);

Thats all we have to do. Then we can access the resultusing javascript.

这就是我们所要做的。然后我们可以使用javascript访问结果

回答by Prabhakar Manthena

Try this, will help you in Struts 2.0.14 with jsonplugin-0.32.jar.

试试这个,将在 Struts 2.0.14 和 jsonplugin-0.32.jar 中帮助你。

struts.xml:

struts.xml:

<struts>
     <package name="example" extends="json-default">
        <action name="HelloWorld" class="example.HelloWorld"  >
            <result type="json" />
        </action>
              <action name="HelloWorld1" class="example.HelloWorld"  >
            <result name="success" >example/HelloWorld.jsp</result>
        </action>
    </package>
</struts>

action class Helloworld.java:

动作类 Helloworld.java:

package prabhakar;

import glb.DB;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Prabhakar
 */
public class HelloWorld  {


    private List<StateMaster> stateList= new ArrayList<StateMaster>();
    private List<RegnMaster> regnList= new ArrayList<StateMaster>();

    private Integer stateId;
    public Integer getStateId()
    {
    return this.stateId;
    }
    public void setStateId(Integer stateId)
    {
    this.stateId=stateId;
    }
    public List<StateMaster> getStateList() {
        return stateList;
    }

    public void setStateList(List<StateMaster> stateList) {
        this.stateList = stateList;
    }
     public void setRegnList(List<RegnMaster> regnList) {
        this.regnList = regnList;
    }
    public List<RegnMaster> getRegnList() {
        return regnList;
    }

    public String execute() throws Exception {

        stateList=DB.getStateData()//
        if(stateId !=null)
          {
         regnList=DB.getRegnByStateId(stateId);
          }

        //setMessage(getText(MESSAGE));
        return "success";
    }

    /**
     * Provide default valuie for Message property.
     */

}

You can directly call HelloWorld.action to view the JSON data or else you can bind the JSON data to a form element below.

您可以直接调用 HelloWorld.action 来查看 JSON 数据,或者您可以将 JSON 数据绑定到下面的表单元素。

JSP page HelloWorld.jsp:

JSP 页面 HelloWorld.jsp:

  /*
     Prabhakar
  */

<%@ page contentType="text/html; charset=UTF-8" %>

<%@ taglib prefix="s" uri="/struts-tags" %>
<script>
<%@include file="../js/jquery-1.7.1.min.js"%>
</script>
    <html>

<!-- JavaScript Plugins -->
  <script>
       function getLoad(){


       var stateId = $('#state').val();

$.getJSON('HelloWorld.action', {'stateId': stateId},
    function(data) {

           var divisionList = (data.regnList);

                var options = $("#regn");
                options.find('option')
    .remove()
    .end();
     options.append($("<option />").val("-1").text("--Select--"));
$.each(divisionList, function() {

    options.append($("<option />").val(this.regnId).text(this.regnName));
});
    }
);}
   </script>

<!-- jQuery-UI Dependent Scripts -->

    <body>
        State List <s:select name="stateId" list="stateList" id="state" listKey="stateId" onchange="getLoad()" listValue="stateName" headerKey="0" headerValue="--select--" />
        Regn List <s:select name="regnId"  list="regnList" listKey="regnId" id="regn" listValue="regnName" headerKey="0" headerValue="--select--" />
    </body>
</html>

Happy coding :)

快乐编码:)