6.9. Submitting a Form from Localized Form Controls
Problem
You want to use a single Action class to process
related operations where each button, which has a localized value, on
a form corresponds to a specific method in the
Action class.
Solution
Extend the Struts pre-built
LookupDispatchAction
with your own class. Provide methods for each operation you wish to
be called. Each method should have the same signature as the
execute( ) method. Implement the
getKeyMethodMap() method, mapping the button label resource bundle key to
the corresponding method name to call. The Action
class shown in Example 6-6 provides three related
operations in one class: create( ),
update( ), and delete( ). The
getKeyMethodMap( ) method returns a map where the
map key is a MessageResources key for the button
label, and the map value is the corresponding method name.
Example 6-6. LookupDispatchAction for related operations
package com.oreilly.strutsckbk.ch06; import java.util.HashMap; import java.util.Map; 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.LookupDispatchAction; public class MyLookupDispatchAction extends LookupDispatchAction { public MyLookupDispatchAction( ) { keyMethodMap = new HashMap( ); keyMethodMap.put("button.add", "create"); keyMethodMap.put("button.edit", "update"); ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access