长话短说,用 DispatchAction的好就是将若干个处理方法放到一个action处理类中.根据用户表中传过来的定义的方法名称来选择action的调用.
struts-config.xml文件的配置如下
<action
attribute="testAddForm"
input="/test/testAdd.jsp"
name="testAddForm"
parameter="p" //在这里定义我们用来选择action方法的表单变量名称.
path="/testAdd"
scope="request"
type="com.braveboy.struts.action.TestAddAction"
validate="false">
<forward
name="add"
path="add.jsp"
redirect="true" />
<forward
name="list"
path="list.jsp"
redirect="true" />
</action>
如下所示为示例TestAddActon类,包括两个方法list还有add,将会在处理完毕之后转到list.jsp和add.jsp页面.
public class TestAddAction extends DispatchAction {
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
TestAddForm f=(TestAddForm)form;
//UserLoginForm f=(UserLoginForm)form;
request.setAttribute("Info",f.getUserName());
String path=mapping.findForward("add").getPath();
RequestDispatcher rpath=request.getRequestDispatcher(path);
try
{
rpath.forward(request, response);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
return null;
}
public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
TestAddForm f=(TestAddForm)form;
//UserLoginForm f=(UserLoginForm)form;
System.out.println(f.getUserName());
request.setAttribute("Info",f.getUserName());
String path=mapping.findForward("list").getPath();
RequestDispatcher rpath=request.getRequestDispatcher(path);
try
{
rpath.forward(request, response);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
return null;
}
/*public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
TestAddForm t = (TestAddForm) form;// TODO Auto-generated method stub
return mapping.findForward("list");
}
*/
}
并使用了表单类为:
public class TestAddForm extends ActionForm {
/*
* Generated Methods
*/
private String password;
private String userName;
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
可以看到这里只是处理了userName和password,(注意!)要处理的表单变量名称首字母一定要小写,具体为什么我还没有去查文档.还有,有一点值得提一下的是如果采用html表单,表单提供的表单内容可以不和该表单类不一样.但是使用jsp的标签库则会严格判断所提交页面的内容是不是和表单的严格一格.从上面我没有提供p的选择字段的表单类属性应该看到我选择的是html的表单,之所以不选择标签类是因为一来自己几乎不懂,而且不准备去懂,另一来感觉比较自由.如下所示:
<body>
<form action="testAdd.do" method="post">
<input type="text" name="p" value="add" />
<input type="text" name="userName" />
<input type="text" name="password" />
<input type="submit" value="ok" />
</form></body>
注意到action用的是testAdd.do而不是testAdd了吗?看一下web.xml的servlet映射就知道struts将所有的.do映射交给了servlet action,
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
明白了吗??