从用户界面接受动态json数据的操作

时间:2022-12-04 15:15:18

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类,它应该接受一个从用户界面构造的JSON字符串,在Action类中没有setter和getter。

Is it possible? If so, what conventions would I need to follow in Action class and in configuration files (struts.xml)?

可能吗?如果是这样,我需要在Action类和配置文件(struts.xml)中遵循哪些约定?

2 个解决方案

#1


4  

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.

将它们作为“application / json”类型的内容发布。您可以使用简单的jquery ajax调用来执行此操作,您可以在其中指定content-type和dataType。

$.ajax({
   type: "POST",
   url: "/the/action/url",     
   data : {},
   dataType:"JSON",
   contentType: "application/json; charset=utf-8"
});

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-lib-2.3-jdk15.jar将json插件添加到项目依赖项中。拦截器json提供的插件,用于从请求中读取和填充操作。如果您不想填充操作,则不应使用此拦截器。而是使用此或任何其他第三方库手动解析请求以获取JSONObject。或者您可以重写拦截器并注释使用JSONPopulator但使用JSONUtil反序列化该对象的代码。

Also, you might find this examples useful when manually creating/parsing JSON data.

此外,您可能会发现此示例在手动创建/解析JSON数据时很有用。

#2


0  

If you want to send JSON object with multiple key-value pair in it and you want to get these key-values in your action class without creating any setters/getters,

如果要在其中发送带有多个键值对的JSON对象,并且希望在操作类中获取这些键值而不创建任何setter / getter,

In the Action class decleration, we should implement ServletRequestAware and we should override the setServletRequest() method and set the HttpServletRequest attribute

在Action类的decleration中,我们应该实现ServletRequestAware,我们应该覆盖setServletRequest()方法并设置HttpServletRequest属性

public class YourClass extends ActionSupport implements ServletRequestAware

private HttpServletRequest request;

@Override
public void setServletRequest(HttpServletRequest request) {
    this.request = request;
}

public HttpServletRequest getServletRequest() {
  return this.request;
}

And in our targeted function, we should use above request object to get the parameter (by its name as it is set in the json as the key):

在我们的目标函数中,我们应该使用上面的请求对象来获取参数(通过它在json中设置的名称作为键):

public String fetchData() {
  String key1Data = getServletRequest().getParameter("key1");
   String key2Data = getServletRequest().getParameter("key2"); 
  return SUCCESS;
 }

Where your JSON object should have data something like:

您的JSON对象应该具有以下数据:

var jsonData = {"key1": "Hello", "key2":"There"};

#1


4  

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.

将它们作为“application / json”类型的内容发布。您可以使用简单的jquery ajax调用来执行此操作,您可以在其中指定content-type和dataType。

$.ajax({
   type: "POST",
   url: "/the/action/url",     
   data : {},
   dataType:"JSON",
   contentType: "application/json; charset=utf-8"
});

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-lib-2.3-jdk15.jar将json插件添加到项目依赖项中。拦截器json提供的插件,用于从请求中读取和填充操作。如果您不想填充操作,则不应使用此拦截器。而是使用此或任何其他第三方库手动解析请求以获取JSONObject。或者您可以重写拦截器并注释使用JSONPopulator但使用JSONUtil反序列化该对象的代码。

Also, you might find this examples useful when manually creating/parsing JSON data.

此外,您可能会发现此示例在手动创建/解析JSON数据时很有用。

#2


0  

If you want to send JSON object with multiple key-value pair in it and you want to get these key-values in your action class without creating any setters/getters,

如果要在其中发送带有多个键值对的JSON对象,并且希望在操作类中获取这些键值而不创建任何setter / getter,

In the Action class decleration, we should implement ServletRequestAware and we should override the setServletRequest() method and set the HttpServletRequest attribute

在Action类的decleration中,我们应该实现ServletRequestAware,我们应该覆盖setServletRequest()方法并设置HttpServletRequest属性

public class YourClass extends ActionSupport implements ServletRequestAware

private HttpServletRequest request;

@Override
public void setServletRequest(HttpServletRequest request) {
    this.request = request;
}

public HttpServletRequest getServletRequest() {
  return this.request;
}

And in our targeted function, we should use above request object to get the parameter (by its name as it is set in the json as the key):

在我们的目标函数中,我们应该使用上面的请求对象来获取参数(通过它在json中设置的名称作为键):

public String fetchData() {
  String key1Data = getServletRequest().getParameter("key1");
   String key2Data = getServletRequest().getParameter("key2"); 
  return SUCCESS;
 }

Where your JSON object should have data something like:

您的JSON对象应该具有以下数据:

var jsonData = {"key1": "Hello", "key2":"There"};