我想要Ajax请求的JSON结果,但我的要求是只发送对象的ArrayList
,并排除action类中存在的一些参数。那么,如何做到这一点?
这是我的行动课
public class AjaxAction{
private int color;
private String prodId;
private List<Product> productList;
public String execute(){
productList= service.getProductList();//this method is getting list of products
HttpServletResponse response = ServletActionContext.getResponse();
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(productList,
new TypeToken<List<Product>>() {
}.getType());
JsonArray jsonArray = element.getAsJsonArray();
response.setContentType("application/json");
try {
response.getWriter().print(jsonArray);
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
我的struts.xml
文件是:
<package name="json" namespace="/" extends="json-default">
<interceptors>
<interceptor-stack name="mystack">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="json">
<param name="enableSMD">true</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="getProduct" class="com.xyz.action.AjaxAction">
<result name="success" type="json" />
<param name="root">productList</param>
</action>
</package>
因此,当我检查我的控制台数据返回的行动是有其他变量在它。如何避免这种情况?
1条答案
按热度按时间6qftjkof1#
您可以返回
stream
结果在行动中,