Struts 2: Send a JSON string with JQuery ajax submit to be mapped into a List of complextypes(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.
推荐答案
您应该定义您的操作结果
type="json"
并检查您的类路径中是否有 json 插件,确保您有
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 字符串以映射到复杂类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Struts 2:发送带有 JQuery ajax 提交的 JSON 字符串以映射到复杂类型列表


基础教程推荐
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01