4

我会尽量具体。

我有一个具有两种方法的操作,一种是通过 ajax 调用,另一种是通过常规提交调用。

关键是无法从常规提交中获取请求,我只获取操作属性。

public class ClientAction{

    @SMDMethod
    public Map<String, Object> findClient(String myParam){
    ...
    }

    public String saveClient(){            
        Map<String, String[]> parameterMap = this.getRequest().getParameterMap();
    }
}

来自 saveClient 方法的 getRequest 返回 null !!!但为什么???我没有用@SMDMethod 声明它

这是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>

我做了所有其他的声明。我曾经有两个单独的类,每个方法一个,但是使用 ClientAction 和 ClientActionJSON 可维护性并不容易。

关于如何在同一个类中同时拥有两种方法的任何想法,一种是ajax,另一种不是。

4

1 回答 1

3

我会立即考虑编写一个示例:

<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>

现在只需在您的 ClientAction 中创建两个方法 jsonMethod() 和 htmlMethod(),一个处理 json 和另一个 html 响应。

[编辑]

我又读了一遍,似乎你只需要一个动作,那么只需考虑使用一个字段(请求参数)来决定返回类型。

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>

上面我假设,returntype是一个字符串变量,您将它与每个请求一起发送,指定预期的返回值。您可以简单地将其隐藏在表单提交中并将其设置在 ajax 请求中。

于 2013-08-24T18:57:53.820 回答