Struts 2:发送带有 JQuery ajax 提交的 JSON 字符串以映射到复杂类型列表 [英] Struts 2: Send a JSON string with JQuery ajax submit to be mapped into a List of complextypes

查看:24
本文介绍了Struts 2:发送带有 JQuery ajax 提交的 JSON 字符串以映射到复杂类型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个struts 2动作类有一个属性

If a struts 2 action class have a property of

列表<用户>用户列表.

并且用户类具有类似的属性

And the user class has properties like

用户名、密码、创建日期

是否可以通过传递适当的 JSON 字符串来使 struts 填充列表,同时提交指向该操作的 jQuery ajax 请求.

Is it possible to make struts populate the list by passing the appropriate JSON string, while submitting a jQuery ajax request pointed at that action.

如果有可能怎么办?特别是 JSON 字符串的外观如何?

If it is possible how? Specially how will the JSON string have to look like?

请询问任何澄清.

如果我不够清楚,我很抱歉.我想要做的不是从 Action 获取数据到 JSP,或者提交表单来填充数据.我想提交一个带有 JSON 数据的 AJAX 请求,Struts 2 可以使用它来自动填充 Action 类中的 List.

EDIT : I'm sorry if I wasn't clear enough. What I want to do is not get data from Action to a JSP, or submit a form to populate the data. I want to submit a AJAX request with JSON data which Struts 2 can use to automatically populate the List in the Action class.

不使用 AJAX 提交表单.

Not submit the form with AJAX.

推荐答案

  1. 您应该定义您的操作结果 type="json" 并检查您的类路径中是否有 json 插件,确保您有

  1. you should define your action result type="json" and check you have json plugin on your classpath, make sure you have

<struts>
    <package name="your_package" extends="struts-default, json default" namespace="/">
    // your action here
    </pacakge>
    ...
</struts>

  • 像这样编辑你的动作类

  • edit your action class like this

    public class myAction extends ActionSupport {
        private List<User> userlist; // getter and setter
        public String execute() {
           // your moves
           return SUCCESS;
        }
    }
    

  • 然后你可以在你的jsp页面上做一些事情来显示结果,比如:

  • then you can do some on your jsp page to show the result like:

    <s:iterator list="userList">
      <s:property value="username"/>
      <s:property value="password"/>
      <s:property value="createdDate"/>
    <s:iterator>
    

  • 希望对你有帮助..

    更新

    如果你已经有一个 json 对象:

    if you have already a json object:

    {
       "userList" : [
          {"username" : "username", "password" : "afgasdfa", "createdDate" : "date-in-format"},
          {"username" : "username", "password" : "afgasdfa", "createdDate" : "date-in-format"},
          {"username" : "username", "password" : "afgasdfa", "createdDate" : "date-in-format"},
          {"username" : "username", "password" : "afgasdfa", "createdDate" : "date-in-format"}
       ]
    }
    

    这应该是格式.

    那么你应该使用 List<User> 从你的动作类中获取这个对象.用户列表

    只是尝试并反馈.

    更新 2

    也许您应该使用 String 获取您的 json 对象,然后使用以下方法将其转换为 Java 对象:

    maybe you should get your json object using String, then convert it to Java Object using:

    String stringObject; // which get your string from http
    List<User> userList;
    
    userList = (List<User>)JSON.deserialize(stringObject,List<User>.class);
    

    请再次反馈.

    这篇关于Struts 2:发送带有 JQuery ajax 提交的 JSON 字符串以映射到复杂类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆