How to make an Action to accept dynamic JSON data from the user interface in Struts 2?(如何在 Struts 2 中创建一个动作来接受来自用户界面的动态 JSON 数据?)
问题描述
我想要一个 Action
类,它应该接受从用户界面构造的 JSON 字符串,而 Action
类中没有 setter 和 getter.
I would like to have an Action
class which should accept a JSON string constructed from user interface with no setter and getter in the Action
class.
有可能吗?如果是这样,我需要在 Action
类和配置文件 (struts.xml
) 中遵循哪些约定?
Is it possible? If so, what conventions would I need to follow in Action
class and in configuration files (struts.xml
)?
推荐答案
将它们作为内容发布,类型为 application/json"
.您可以通过一个简单的 jQuery Ajax 调用来完成,您可以在其中指定 content-type
和 dataType
.
Post them as content with type "application/json"
. You may do it with a simple jQuery Ajax call where you could specify the content-type
and dataType
.
$.ajax({
type: "POST",
url: "/the/action/url",
data : {},
dataType:"JSON",
contentType: "application/json; charset=utf-8"
});
使用 json-lib-2.3 将 json 插件 添加到项目依赖项中-jdk15.jar
.拦截器 json
从请求中读取并填充操作.
Add json plugin to the project dependencies with json-lib-2.3-jdk15.jar
. The plugin supplied with the interceptor json
that reads and populates an action from the request.
如果您不想填充操作,则不应使用此拦截器.而是使用此库或任何其他第三方库手动解析请求以获取 JSONObject
类.或者您可以重写拦截器并注释使用 JSONPopulator
类 但使用 JSONUtil
类.
If you don't want to populate the action then you should not use this interceptor. Instead manually parse the request with this or any other third party library to get the JSONObject
class. Or you could rewrite the interceptor and comment that code that is using JSONPopulator
class but deserialize the object with JSONUtil
class.
此外,您可能会发现 this 示例在手动创建/解析 JSON 数据时很有用.
Also, you might find this examples useful when manually creating/parsing JSON data.
这篇关于如何在 Struts 2 中创建一个动作来接受来自用户界面的动态 JSON 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Struts 2 中创建一个动作来接受来自用户界面的动态 JSON 数据?
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01