2

I'm working on a Struts2 project, in which there are some actions that contain the most of fields in common.

I have aggregated these fields in a superclass that generalizes actions and extends ActionSupport. For each field I have created getter/setter methods.

The problem is that the framework doesn't seem to invoke setter methods if the fields are inherited.

Below some snippets of my implementation:

Struts.xml

<action name="PresaInCaricoView" class="av.pei.action.view.PresaInCaricoView">
    <interceptor-ref name="servletConfig"/>
    <interceptor-ref name="prepare"/>
    <result>/PresaInCarico.jsp</result>     
</action>

Java Code (Important: Action invocation is from JSP form, field is from JSP)

Base action

public class LottoView extends ActionSupport implements ServletRequestAware, Preparable{    

    @Getter @Setter public String idLotto = null;   
    @Getter @Setter public String idTipoDoc = null;     
    @Getter @Setter public String idStatoLotto = null; 

    public void prepare() throws Exception { }

}

Action

public class PresaInCaricoView extends LottoView {
    public String execute() {       
        System.out.println(idLotto);     
    }
}

Is there any way to implement this behavior?

Thanks in advance, Paolo

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Pavoletto
  • 140
  • 10
  • Are they public, or protected ? If they're protected, they're not available to JSP, only to actions. – Andrea Ligios May 17 '16 at 09:44
  • BTW consider adding some sample code to make more clear what are you doing exactly, and how are you trying to do it – Andrea Ligios May 17 '16 at 09:48
  • Fields is from JSP and are declared protected.I've edited my question with some code. I've tried to declare fields public instead protected, but nothing is changed. I've found the solution, problems is in the Preparable interface implemented in the superclass. Removing the Preparable interceptors it works! :| – Pavoletto May 17 '16 at 12:39
  • Dude... run the damn whole `defaultStack` :) or at least the `basicStack`. You're running only two interceptors, missing **a lot** of mandatory things, like the parameters interceptor... – Andrea Ligios May 17 '16 at 12:46
  • Ok, maybe I need a revision of Interceptors. :) How can i execute some code (with Servlet request available) in the Base action before the execute() method? – Pavoletto May 17 '16 at 13:11
  • 1
    `prepare()` is the right tool for that. Just use ` /PresaInCarico.jsp `. For ServletRequestAware, [declare the attribute and implement the setter](http://stackoverflow.com/a/19540712/1654265) (but are you sure you need the ServletRequest ? ;) – Andrea Ligios May 17 '16 at 13:59
  • Yes, because I need to get an attribute from the HttpSession. Sorry I have omitted a lot of information from this question :) . Anyway thank you very much for your help, I don't know how to increase your reputation from the comments. – Pavoletto May 17 '16 at 14:33
  • 1
    You're welcome :) You can upvote the answer linked in my last comment if you want. Have a nice day – Andrea Ligios May 17 '16 at 14:48

0 Answers0