Struts 2 .json URL扩展

时间:2012-05-05 00:05:06

标签: ajax json struts2

我正在尝试编写Struts 2 Action,它接受常规HTTP请求以及基于Ajax的HTTP请求,到目前为止我已经提出了这个问题:

定义内容类型和操作扩展名:

<constant name="struts.action.extension" value="do,json"></constant>

<result-type name="json" class="org.apache.struts2.json.JSONResult">
    <param name="contentType">text/plain</param>
</result-type>

使用两种结果类型定义操作

<action name="xyz" class="com.abc.actions.TestAction">
    <result name="success">testoutput.jsp</result>
    <result name="json" type="json"></result>
</action>

public class TestAction implements Action {
    public String execute() throws Exception {
        // do whatever you want here. also add setters and getters for the JSON resposne to work
        HttpServletRequest request = ServletActionContext.getRequest();
        String ext = request.getRequestURL().substring(request.getRequestURL().lastIndexOf(".") + 1);
        if(StringUtils.isNotEmpty(ext) && ext.equals("json")) {
            return "json";
        }
        return "success";
}

所以,当我运行http://{domain}/xyz.do时,它运作得很好。

但是,运行http://{domain}/xyz.json只会返回一个空页面。

处理没有例外。我不知道该怎么办。我尝试用不同的东西(比如“jsp”)替换.json扩展名并且它有效。

我也是Struts(以及Java)的新手,所以请提醒我,如果我错过了任何重要的信息。

0 个答案:

没有答案
相关问题