LookupDispatchAction使用示例
当表单中出现两个或以上的同名按钮时,可以通过LookupDispatchAction查询properties文件实现定位到不同方法的操作。
本文以简单的加减乘除为例:
一、编写properties文件
的作用是通过该key值去properties文件中获得value值作为submit按钮的value属性的值。
三、编写CalcAction类
加减乘除这四种逻辑都在CalcAction类中定义,该类继承自LookupDispatchAction,而且必须重写getKeyMethodMap()方法。
假设按下<input type="submit" name="meo" value="加">,LookupDispatchAction会根据value值(加),在属性文件中找到对应的key值,然后根据该Key值与getKeyMethodMap()得知要执行CalcAction类中的add()。
四、配置stuts-config.xml
本文以简单的加减乘除为例:
一、编写properties文件
add=加
sub=减
mutiply=乘
div=除
二、编写jsp页面sub=减
mutiply=乘
div=除
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for CalcForm form</title>
</head>
<body>
<html:form action="/calc">
操作数1 : <html:text property="num01"/><html:errors property="num01"/><br/>
操作数2 : <html:text property="num02"/><html:errors property="num02"/><br/>
<html:submit property="meo">
<bean:message key="add"/>
</html:submit>
<html:submit property="meo">
<bean:message key="sub"/>
</html:submit>
<html:submit property="meo">
<bean:message key="mutiply"/>
</html:submit>
<html:submit property="meo">
<bean:message key="div"/>
</html:submit>
</html:form>
<br>结果:${result}
</body>
</html>
查看客户端源代码可得<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for CalcForm form</title>
</head>
<body>
<html:form action="/calc">
操作数1 : <html:text property="num01"/><html:errors property="num01"/><br/>
操作数2 : <html:text property="num02"/><html:errors property="num02"/><br/>
<html:submit property="meo">
<bean:message key="add"/>
</html:submit>
<html:submit property="meo">
<bean:message key="sub"/>
</html:submit>
<html:submit property="meo">
<bean:message key="mutiply"/>
</html:submit>
<html:submit property="meo">
<bean:message key="div"/>
</html:submit>
</html:form>
<br>结果:${result}
</body>
</html>
<html>
<head>
<title>JSP for CalcForm form</title>
</head>
<body>
<form name="calcForm" method="post" action="/lookup/calc.do">
操作数1 : <input type="text" name="num01" value="5"><br/>
操作数2 : <input type="text" name="num02" value="7"><br/>
<input type="submit" name="meo" value="加">
<input type="submit" name="meo" value="减">
<input type="submit" name="meo" value="乘">
<input type="submit" name="meo" value="除">
</form>
<br>结果:12.0 </body>
</html>
可见<html:submit property="meo">中的property="meo"被转换为submit按钮的name="meo"而<bean:message key="add"/><head>
<title>JSP for CalcForm form</title>
</head>
<body>
<form name="calcForm" method="post" action="/lookup/calc.do">
操作数1 : <input type="text" name="num01" value="5"><br/>
操作数2 : <input type="text" name="num02" value="7"><br/>
<input type="submit" name="meo" value="加">
<input type="submit" name="meo" value="减">
<input type="submit" name="meo" value="乘">
<input type="submit" name="meo" value="除">
</form>
<br>结果:12.0 </body>
</html>
的作用是通过该key值去properties文件中获得value值作为submit按钮的value属性的值。
三、编写CalcAction类
加减乘除这四种逻辑都在CalcAction类中定义,该类继承自LookupDispatchAction,而且必须重写getKeyMethodMap()方法。
假设按下<input type="submit" name="meo" value="加">,LookupDispatchAction会根据value值(加),在属性文件中找到对应的key值,然后根据该Key值与getKeyMethodMap()得知要执行CalcAction类中的add()。
public class CalcAction extends LookupDispatchAction {
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
CalcForm calcForm = (CalcForm) form;
double result = Double.parseDouble(calcForm.getNum01())
+ Double.parseDouble(calcForm.getNum02());
request.setAttribute("result", result);
return mapping.findForward("bi");
}
public ActionForward sub(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
CalcForm calcForm = (CalcForm) form;
double result = Double.parseDouble(calcForm.getNum01())
- Double.parseDouble(calcForm.getNum02());
request.setAttribute("result", result);
return mapping.findForward("bi");
}
public ActionForward mutiply(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
CalcForm calcForm = (CalcForm) form;
double result = Double.parseDouble(calcForm.getNum01())
* Double.parseDouble(calcForm.getNum02());
request.setAttribute("result", result);
return mapping.findForward("bi");
}
public ActionForward div(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
CalcForm calcForm = (CalcForm) form;
double result = Double.parseDouble(calcForm.getNum01())
/ Double.parseDouble(calcForm.getNum02());
request.setAttribute("result", result);
return mapping.findForward("bi");
}
@Override
protected Map getKeyMethodMap() {
//map中的key与资源文件中的key相同,value为方法名
Map map = new HashMap();
map.put("add", "add");
map.put("sub", "sub");
map.put("mutiply", "mutiply");
map.put("div", "div");
return map;
}
}
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
CalcForm calcForm = (CalcForm) form;
double result = Double.parseDouble(calcForm.getNum01())
+ Double.parseDouble(calcForm.getNum02());
request.setAttribute("result", result);
return mapping.findForward("bi");
}
public ActionForward sub(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
CalcForm calcForm = (CalcForm) form;
double result = Double.parseDouble(calcForm.getNum01())
- Double.parseDouble(calcForm.getNum02());
request.setAttribute("result", result);
return mapping.findForward("bi");
}
public ActionForward mutiply(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
CalcForm calcForm = (CalcForm) form;
double result = Double.parseDouble(calcForm.getNum01())
* Double.parseDouble(calcForm.getNum02());
request.setAttribute("result", result);
return mapping.findForward("bi");
}
public ActionForward div(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
CalcForm calcForm = (CalcForm) form;
double result = Double.parseDouble(calcForm.getNum01())
/ Double.parseDouble(calcForm.getNum02());
request.setAttribute("result", result);
return mapping.findForward("bi");
}
@Override
protected Map getKeyMethodMap() {
//map中的key与资源文件中的key相同,value为方法名
Map map = new HashMap();
map.put("add", "add");
map.put("sub", "sub");
map.put("mutiply", "mutiply");
map.put("div", "div");
return map;
}
}
四、配置stuts-config.xml
<action-mappings>
<action attribute="calcForm"
input="/calc.jsp"
name="calcForm"
parameter="meo" //该值为页面中按钮的名字
path="/calc"
scope="request"
type="com.fskjb.struts.action.CalcAction">
<forward name="bi" path="/calc.jsp" />
</action>
</action-mappings>
<action attribute="calcForm"
input="/calc.jsp"
name="calcForm"
parameter="meo" //该值为页面中按钮的名字
path="/calc"
scope="request"
type="com.fskjb.struts.action.CalcAction">
<forward name="bi" path="/calc.jsp" />
</action>
</action-mappings>