Getting json return type and html return in one action

I will try to be as specific as possible.

I have an action with two methods, one called through ajax and the other through a regular submit.

The fact is that it is impossible to receive a request from a regular submit, I only get action properties.

public class ClientAction{ @SMDMethod public Map<String, Object> findClient(String myParam){ ... } public String saveClient(){ Map<String, String[]> parameterMap = this.getRequest().getParameterMap(); } } 

getRequest from saveClient returns null !!! But why??? I did not declare this with @SMDMethod

and here is struts.xml

 <action name="client" class="myCompany.ClientAction"> <interceptor-ref name="customJSON"><param name="enableSMD">true</param></interceptor-ref> <result type="json"><param name="enableSMD">true</param></result> </action> 

I made all the other announcements. I used to have two separete classes, one for each method, but with ClientAction and ClientActionJSON disproportion was not easy.

Any thoughts on how to use both methods, one ajax and the other not, in the same class.

+4
source share
1 answer

I will immediately try to write a sample:

 <action name="xclient" class="myCompany.ClientAction" method="jsonMethod"> <result type="json"></result> </action> <action name="yclient" class="myCompany.ClientAction" method="htmlMethod"> <result type="dispatcher">/pages/y.jsp</result> </action> 

now just create both jsonMethod () and htmlMethod () methods in your ClientAction, one json handler and the other html response.

[EDIT]

I read it again and it seems that you only need one action, well, and then just try to use the field (query parameter) to select the return type.

 public String execute(){ //..Other code if(returntype.equals("json")){ return "jsonresult"; } else{ return "htmlresult"; } } <action name="client" class="myCompany.ClientAction" method="jsonMethod"> <result name="jsonresult" type="json"></result> <result name="htmlresult" type="dispatcher">/pages/y.jsp</result> </action> 

Above, I assumed that returntype is a String variable that you sent along with each request indicating the expected return. You can simply send it as a submit and set it in an ajax request.

+3
source

Source: https://habr.com/ru/post/1498667/


All Articles