struts2 - Getting json return type and html return in the same action -


i'll try specific possible.

i have action 2 methods, 1 called via ajax , other 1 via regular submit.

the point can't request regular submit, i'm getting action properties.

public class clientaction{      @smdmethod     public map<string, object> findclient(string myparam){     ...     }      public string saveclient(){                     map<string, string[]> parametermap = this.getrequest().getparametermap();     } } 

getrequest saveclient method returns null!!! but, why??? didn't declare @smdmethod

and here 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 did others declarations. used have 2 separete classes, 1 each method, maintainability wasn't easy clientaction , clientactionjson.

any thoughts on how have both methods, 1 ajax , other not, in same class.

i'll straight away consider write 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 create both methods jsonmethod() & htmlmethod() in clientaction, 1 handling json , html response.

[edit]

i read again , seems require one-action, consider using field (request parameter) decide 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 assumed, returntype string variable sent along each request specifying return expected. can send hidden in form-submit , set in ajax-request.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -