3

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.

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

Roman C
  • 49,761
  • 33
  • 66
  • 176
Laxmikanth Samudrala
  • 2,203
  • 5
  • 28
  • 45
  • 3
    Why without getter and setter? I could explain to you otherwise. – Armaggedon May 25 '13 at 13:29
  • I have many static json formats embedded in java script files based on the context of the page i need to send those static json string to corresponding action classes – Laxmikanth Samudrala May 25 '13 at 14:22
  • XSS paradise O_O Load them from server, instead of from client ://// – Andrea Ligios May 25 '13 at 15:49
  • It makes no sense to expect this to be possible w/o a setter. A JSON string is just that: a string. Expose a string property. Set it. Simple. You can *parse* it however you want, like with Google GSON, etc. – Dave Newton May 25 '13 at 16:12

2 Answers2

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.

$.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 class. Or you could rewrite the interceptor and comment that code that is using JSONPopulator class but deserialize the object with JSONUtil class.

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

Roman C
  • 49,761
  • 33
  • 66
  • 176
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,

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

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

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

Where your JSON object should have data something like:

var jsonData = {"key1": "Hello", "key2":"There"};
Shibaram Sahoo
  • 31
  • 1
  • 1
  • 6