Tuesday, July 19, 2011

Struts - set-property in Actions tag

My understanding on <set-property> is that it is setting a property on the ActionMapping and NOT on the Action. In order to use it, you need to subclass ActionMapping and declare that subclass using the type attribute of the action mapping.

struts-config.xml

<action-mappings
      type="org.apache.struts.webapp.examples.CustomActionMapping">

    <action path="/welcome" forward="/welcome.jsp" >
        <set-property property="example" value="EXAMPLE" />
    </action>
</action-mappings> 

Action Class

public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {

        CustomActionMapping cam = (CustomActionMapping) mapping;
        String example = cam.getExample();
        ...

}

No comments:

Post a Comment