0

In have a custprofileview Action which show a JSP page with all details of customer and in my JSP all fields are like my

<s:textfield name="custprofileVO.email" value="%{custprofileVO.email}" />
<s:textfield name="custprofileVO.phone" value="%{custprofileVO.phone}" />

and do so on, and there is a submit button that calls Action updatecustprofile.

In updatecustprofile Action, instead of directly mapping properties I have a member variable private CustprofileVO custprofileVO; with setter and getter.

In CustprofileVO Class I have fields like email, phone and all other fields with their setters and getters methods.

Problem is: in updatecustprofile Action I am implementing Prepareable Interface, and in implementation of prepare() method I have custprofileVO.setDefaultID("Select"); and setting 4 more fields but when I run the program by clicking on submit button I get NPE in the very first line that is custprofileVO.setDefaultID("Select");

It looks like the framework is not instantiating CustprofileVO custprofileVO. If I manually instantiate custprofileVO just above setting of the field (by doing custprofileVO = new CustprofileVO() then it works. Problem is- ideally struts2 framework should give me instance which it is not doing, want to understand the reason.

Further if I manually set custprofileVO in prepare method it works but I have also applied validation using XML where my field name is custprofileVO.email ,its validation then custprofileVO.phone its validation.

When I try to validate on click of submit button validation runs but on screen I see messages for all fields as data in all textboxes gets blanked out.

Why is data getting removed ?

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Can't you simply post your code ? It's a pain to figure out what the code is by reading a whole page of (unformatted) sentences :| Please post ***the relevant parts of*** : 1) struts.xml 2) Action class to get a better help. Until then, I'll go with the most probable answer here: wrong stack. – Andrea Ligios Apr 07 '14 at 08:31

1 Answers1

0

You should not instantiate by yourself an object that is coming from JSP.

To get it in the prepare() method, run by the Prepare Interceptor before the Param Interceptor, you need to use a special stack: paramsPrepareParamsStack

 <!-- An example of the paramsPrepareParams trick. This stack
             is exactly the same as the defaultStack, except that it
             includes one extra interceptor before the prepare interceptor:
             the params interceptor.

             This is useful for when you wish to apply parameters directly
             to an object that you wish to load externally (such as a DAO
             or database or service layer), but can't load that object
             until at least the ID parameter has been loaded. By loading
             the parameters twice, you can retrieve the object in the
             prepare() method, allowing the second params interceptor to
             apply the values on the object. -->
        <interceptor-stack name="paramsPrepareParamsStack">
            <interceptor-ref name="exception"/>
            <interceptor-ref name="alias"/>
            <interceptor-ref name="i18n"/>
            <interceptor-ref name="checkbox"/>
            <interceptor-ref name="multiselect"/>
            <interceptor-ref name="params">
                <param name="excludeParams">^class\..*,^dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,^parameters\..*,^action:.*,^method:.*</param>
            </interceptor-ref>
            <interceptor-ref name="servletConfig"/>
            <interceptor-ref name="prepare"/>
            <interceptor-ref name="chain"/>
            <interceptor-ref name="modelDriven"/>
            <interceptor-ref name="fileUpload"/>
            <interceptor-ref name="staticParams"/>
            <interceptor-ref name="actionMappingParams"/>
            <interceptor-ref name="params">
                <param name="excludeParams">^class\..*,^dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,^parameters\..*,^action:.*,^method:.*</param>
            </interceptor-ref>
            <interceptor-ref name="conversionError"/>
            <interceptor-ref name="validation">
                <param name="excludeMethods">input,back,cancel,browse</param>
            </interceptor-ref>
            <interceptor-ref name="workflow">
                <param name="excludeMethods">input,back,cancel,browse</param>
            </interceptor-ref>
        </interceptor-stack>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • 1
    does `prepare()` get called before `validate()`? or after? – Govinda Sakhare May 19 '16 at 12:29
  • 2
    Since `prepare()` is run by **Prepare** Interceptor, while `validate()` is run by the **Validation** Interceptor, it depends on how you've configured them in your stack, but in the default Stack, in the paramsPrepareParamsStack, and basically in every stack not completely messed up, `prepare()` will always run **before** `validate()`. – Andrea Ligios May 19 '16 at 12:57