Erik Weber wrote:

I found the answer. It is to use the struts-el html tag library instead of the standard html tag library, and then to refer to the variable using the expression language syntx:

<c:set var="action" value="/foo/update"/>

. . .

<html:form action="${action}" . . . />

Erik are you uisng one of the DispatchActions (regular DispatchAction, LookUpDipsatcchAction, in 1.2 MappingDispatchAction)? In these cases you could reuse ONE mapping to accomplish your different common tasks and won't need to change the form Action name.


For example, say you were dealing with updating, inderting, viewing Employee information. I would make one DispatchAction called "EmployeeDispatchAction" and in there you'd you have your different methods:

update(..), retrieve(..), maybe even getListOfEmployees(..), etc.

Then your form mapping would simply be something like 'employeeAction', which would result in

<html:form action="employeeAction"> <--- no need for dynamically changing the action attribute

What method gets called inisde of EmployeeDispatchAction is based on the parameter you decide to use (I like to call mine "userAction"). So if using the LookupDispatchAction I have a hidden variable which is set to this variable:

<html:hidden property="userAction"/>

The nice thing is you also shouldn't need to do any logic on JSP either for this, since you can set the "userAction" BEFORE you get to the page in the Action you are coming from (always go through an action:). So for example you click on a menu link called "update employee" - before bringing you to the update employee page you'd first go through a SetUp method in an action and since you know you are then proceding on to the "update" page you simply set the form variable...

myForm.setUserAction(Constants.UPDATE) or
myForm.setUserAction("update") or if DynaForm:
myForm.set("userAction","update"); or use a Constants.UPDATE

Now I think I remember an earlier discussion where different validations (using the same form) were an issue. In that case having separate action names could be helpful. But even there I'd key the action value based of the parameter for your dispatch action name (no need to do any set or loigic):

<html:form action="${myFormBean.userAction}">
...
</html:form>


And another approach I've used before is a little awkward but it can come in handy and your JSP would even avoid the el above. You'd have one action name (ie action="employeeAction") and of course a dispatch parameter on the page somewhere and which is defined in the action mapping (ie parameter="userAction"). Then you can simply call your form bean's validate method manually. Your formBean would have an over-ridden validate method and it can call the appropriate validator based on the dispatch parameter. It came in handy for a case where I had different submit buttons on one form and depending on which one was selected I had to validate differently. I found it pretty clean. If you ever go this route here's an example...


validation.xml:

<form name="displayFromIndiIds">
            <field property="cardStartId"
                depends="required,integer">
                <arg0 key="gc.cardStartId.displayname"/>
            </field>
...

FormBean with over-riden validate:

validate(..) {
ServletContext application = getServlet().getServletContext();
ActionErrors errors = new ActionErrors();
String parameter = request.getParameter( mapping.getParameter() );
//parameter would be your dispatch parameter ie displayFromIndiIds
Validator validator = Resources.initValidator( parameter, this, application, request, errors, page);
try {
validatorResults = validator.validate();
} catch (ValidatorException e) {
log.error(e.getMessage(), e);
}
return errors;
}








--
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to