Struts2 JSON interceptor not populating my Action class

自古美人都是妖i 提交于 2019-12-13 00:50:11

问题


I am working on a web app in which the client JavaScript library (ExtJS) sends requests to my Struts-enabled backend with a JSON request payload. My problem is that I can't figure out how to get the variables from the payload into my Action class.

I know from using log statements that the correct method in my action class is being called by the frontend. It's just that the variables I am expecting are null when they shouldn't be.

I'm trying to use struts2-json-plugin as an interceptor. Struts interceptors are new to me, and I haven't been able to find any clear examples on the web about using struts2-json-plugin in this way.

Here are the Struts dependencies in my pom.xml:

<dependency>
  <groupId>org.apache.struts</groupId>
  <artifactId>struts2-core</artifactId>
  <version>2.2.3.1</version>
</dependency>
<dependency>
   <groupId>org.apache.struts</groupId>
   <artifactId>struts2-json-plugin</artifactId>
   <version>2.2.3.1</version>
</dependency>        

Here is the request body (care of Firebug):

{"id":15,"translation":"Blah blah"}

The relevant parts of my Action class look like this:

public class TranslationsAction extends ActionSupport {

    private Long id;
    private String translation;

    public String save() throws Exception {

        log.debug("TranslationsAction.save() called.");
        log.debug("ID: " + this.id + ", Translation: " + this.translation);

        return "{\"foo\":\"bar\"}";
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setTranslation(String translation) {
        this.translation = translation;
    }

}

Here are relevant parts of my struts.xml:

<package name="foo" namespace="/foobar" extends="json-default">    
    <action name="save-translation" method="save"
            class="my.package.name.action.TranslationsAction">
        <result name="success">/jsp/json.jsp</result>
    </action>
</package>

Any ideas?


回答1:


I found the problem myself. I had failed to configure struts.xml properly. I needed to first define an interceptor within my package, and then tell my action to use it, as in:

<package name="foo" namespace="/foobar" extends="json-default">    

    <!-- Added this: -->
    <interceptors>
        <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>
    </interceptors>

    <action name="save-translation" method="save"
            class="my.package.name.action.TranslationsAction">

        <!-- And added this: -->
        <interceptor-ref name="myDefaultInterceptorStack" />
        <interceptor-ref name="json" />

        <result name="success">/jsp/json.jsp</result>
    </action>
</package>

It was a silly omission on my part. :-\




回答2:


Well JSON plugin require some steps to follow in order to work properly and here are few of them

  1. The "content-type" must be "application/json".
  2. The JSON content must be well formed, see json.org for grammar.

Its better if you could paste the relevant part of the JS (ExtJS) from where you are calling your Action class and sending the data to your action class.

Please have a look at the official JSON plugin Page for details

json-plugin



来源:https://stackoverflow.com/questions/8886713/struts2-json-interceptor-not-populating-my-action-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!