Two actions, one result each one
You can simply use two actions, mapped to the same action class, pointing to different methods, returning different results (one JSP, one JSON, in this case):
<package name="foobar" namespace="/" extends="json-default">
<action name="jspList" class="foo.bar.MyAction">
<result>myList.jsp</result>
</action>
<action name="jsonList" class="foo.bar.MyAction" method="jsonExecute">
<result type="json">
<param name="root">myList</param>
</result>
</action>
</package>
Note: method="execute"
in actions and name="success"
in results are omitted due to Intelligent Default.
In myList.jsp
read with Struts tags the myList
action attribute and use it for what you want (populate a Select tag, a table, or whatever...). Or use a Stream result instead of a standard Dispatcher.
In the Action class:
public class MyAction extends ActionSupport {
private List<Object> myList; // Getter
public String execute(){
doBusiness();
return SUCCESS;
}
public String jsonExecute(){
doBusiness();
return SUCCESS;
}
private void doBusiness(){
myList = // load the list here
}
}
One action, two results
Of course, instead of two actions each one with one result, you can use a single action with two results, picking up the right one to return basing on a parameter (like you was trying to do, appearently)...:
<package name="foobar" namespace="/" extends="json-default">
<action name="list" class="foo.bar.MyAction">
<!-- success -->
<result>myList.jsp</result>
<!-- json -->
<result name="json" type="json">
<param name="root">myList</param>
</result>
</action>
</package>
In the action, perform the check on the parameter:
public class MyAction extends ActionSupport {
private int isJSON; // Setter
private List<Object> myList; // Getter
public String execute(){
myList = // load the list here
if (isJSON==1) {
return "json"; // mapped to a JSON result
}
return SUCCESS; // mapped to a dispatcher result
}
}
Content-Negotiation
Another way is to do a real Content-Negotiation based on HTTP headers, returning a result in the format the browser is claiming to accept:
public class MyAction extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request; // Setter
private List<Object> myList; // Getter
public String execute(){
myList = // load the list here
String acceptHeader = request.getHeader("Accept");
if (acceptHeader != null && acceptHeader.equals("application/json"){
return "json"; // mapped to a JSON result
}
return SUCCESS; // mapped to a dispatcher result
}
}
Also read how the JSON plugin works with the root
object.