LookupDispatchAction extends the functionality of DispatchAction class that support internationalization. LookupDispatchAction uses the value of the request parameter to reverse-map to a property in the Struts resource bundle file. ApplicationResources.properties. That is, the value of the request parameter is compared against the values of properties in the resource bundle until a match is found. The key for the matching property is then used as a key to another map that maps to a method in
your LookupDispatchAction subclass that will be executed.
To use LookupDispatchAction, you must create a subclass from it and provide a set of methods that will be called to process requests. The subclass must also include a getKeyMethodMap( ) method that maps methods in the class to keys in the Struts resource bundle file.
Following is an example UserAction class that extends DispatchAction:
package pack.nk;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
public class UserAction extends LookupDispatchAction
{
protected Map getKeyMethodMap()
{
HashMap map = new HashMap();
map.put("button.add", "add");
map.put("button.update", "update");
map.put("button.remove", "remove");
return map;
}
public ActionForward add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Add user.
…
return new ActionForward("success");
}
public ActionForward update(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Update user.
…
return new ActionForward("success");
}
public ActionForward remove(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Remove user.
…
return new ActionForward("success");
}
}
<action-mappings>
<action path="/User"
type="pack.nk.UserAction"
parameter="function"/>
</action-mappings>
The value specified with the parameter attribute of the action tag will be used as the name of a request parameter that will contain the value of a key in the Struts resource bundle shown here:
button.add=Add User
button.update=Update User
button.remove=Remove User
No comments:
Post a Comment