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

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

问题描述

如果struts 2动作类的属性为



List< User>用户列表



用户类的属性类似于



用户名,密码,createdDate 等。



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

如果有可能如何?特别是JSON字符串将如何显示?



请询问任何说明。



编辑:我'对不起,如果我不够清楚的话。我想要做的不是从Action获取数据到JSP,或提交表单来填充数据。我想提交一个带有JSON数据的AJAX请求,Struts 2可以使用它来自动填充Action类中的List。



不使用AJAX提交表单。

解决方案


  1. 您应该定义您的行动结果 type =json并检查你的类路径上是否有json插件,确保你有

     < struts> 
    < package name =your_packageextends =struts-default,json defaultnamespace =/>
    //您的行动
    < / pacakge>
    ...
    < / struts>


  2. 像这样编辑你的行动类

     公共类myAction扩展ActionSupport {
    private List< User>用户列表; // getter和setter
    public String execute(){
    //你的动作
    返回SUCCESS;
    }
    }


  3. 然后你可以在你的jsp上做点什么页面显示结果如:

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


我希望这可以帮助你..



更新



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

  {
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>来从你的动作类中获取这个对象。 userList



只需尝试并反馈。



UPDATE 2



也许您应该使用 String 获取您的json对象,然后使用以下命令将其转换为Java Object: / p>

  String stringObject; //从http 
获取你的字符串列表<用户>用户列表;

userList =(List< User>)JSON.deserialize(stringObject,List< User> .class);

请再次反馈。


If a struts 2 action class have a property of

List<User> userList.

And the user class has properties like

username, password, createdDate etc.

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.

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

Please ask any clarifications.

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.

Not submit the form with AJAX.

解决方案

  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>
    

  2. 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;
        }
    }
    

  3. 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>
    

I hope this can help you..

UPDATE

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"}
   ]
}

this should be the format.

then you should get this object from your action class with List<User> userList

just try it and feedback.

UPDATE 2

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);

Please feedback again.

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

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