Struts中DispatchAction的使用.

长话短说,用 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>

明白了吗??

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值