从用户界面接受动态json数据的操作 [英] Action to accept dynamic json data from user interface
问题描述
我想要一个 Action
类,该类应接受从用户界面构造的JSON字符串,而 Action中没有setter和getter
class。
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插件与 json-lib-2.3-jdk15.jar
的项目依赖项。插件随拦截器 json
,用于读取和填充请求中的操作。如果您不想填充操作,则不应使用此拦截器。而是使用此或任何其他第三方库手动解析请求以获取 的JSONObject
。或者您可以重写拦截器并注释使用 JSONPopulator
但是使用 JSONUtil
。
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. 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
. Or you could rewrite the interceptor and comment that code that is using JSONPopulator
but deserialize the object with JSONUtil
.
此外,您可能会发现这个示例在手动创建/解析JSON数据时很有用。
Also, you might find this examples useful when manually creating/parsing JSON data.
这篇关于从用户界面接受动态json数据的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!