struts之lookupDispatchAction的使用

lookupDispatchAction用途之一:当jsp中有多个相同名字的按钮时
jsp:
<body>
		<html:form action="/my">
			age : <html:text property="age"/><html:errors property="age"/><br/>
			name : <html:text property="name"/><html:errors property="name"/><br/>
			<input type="submit" name="method" value="save"/> 
			<input type="submit" name="method" value="delete"/> 
			<input type="submit" name="method" value="search"/> 
		</html:form>
	</body>



action:
public class MyAction extends LookupDispatchAction {
...
	public ActionForward saveAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		MyForm myForm = (MyForm) form;// TODO Auto-generated method stub
		System.out.println("MyAction...save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)");
		return mapping.findForward("succ");
	}
	public ActionForward deleteAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		MyForm myForm = (MyForm) form;// TODO Auto-generated method stub
		System.out.println("MyAction...delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)");
		return mapping.findForward("succ");
	}
	public ActionForward searchAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		MyForm myForm = (MyForm) form;// TODO Auto-generated method stub
		System.out.println("MyAction...search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)");
		return mapping.findForward("succ");
	}

	/**
	 * 必须覆盖
	 */
	protected Map getKeyMethodMap() {
		// TODO Auto-generated method stub
		/**
		 * key对应ApplicationResources.properties中=前面
		 * value对应action中的方法名
		 * 
		 * ApplicationResources.properties中=后面对应jsp按钮value
		 */
		Map<String, String> m = new HashMap<String, String>();
		m.put("button.save", "saveAction");
		m.put("button.delete", "deleteAction");
		m.put("button.search", "searchAction");
		return m;
	}


Struts-config.xml
记得加上
parameter="method"


ApplicationResources.properties:
button.save=save
button.delete=delete
button.search=search



有一点可以看到,地址栏里面没有显示方法名../../my.do隐藏掉了,呵。

你可能感兴趣的:(java,xml,jsp,struts)