(三)sturts之DispactherAction的用法

首先我们来考虑一个问题,像在struts的第一篇文章中介写到的,struts-config.xml中配置一个路径那么就得匹配一个Action,如果增删改查多个路径呢:比如http://ip:host/app/xx.do?method=opt ,如果这个opt分别为add,delete,update,query 。是否就要配置四个action在struts-config.xml中,并且还得写四个对应的Action处理类?这种做法显然有点笨拙的,针对Action只能执行excute方法,也就是说你用Action的话,方法就必须得是excute

而DsispactherAction就不一样了,使用它的话,首先在struts-config.xml中配置一个action即可,加上属性parameter

如:<action path="/student"

type="com.javacrazyer.web.action.StudentOptAction"

parameter="method">

那么,现在我们只需要写上一个StudentOptAction类,添加你想要的方法即可,方法名字都可以自定义的

不过页面上提交或链接的URL至少要出现method这个参数,method等于什么,那么就在Action中写上什么方法。


<action>的parameter属性是给DispatchAction使用的,你的类要继承DispatchAction类,而不是普通的Action,Action只会执行execute方法,DispatchAction会根据parameter的值执行特定的方法,注意parameter的值不要设置为execute,也不要覆盖DispatchAction中的execute(),因为DispatchAction继承于Action,它的execute会首先执行,在execute()方法中取出parameter的值,通过java反射调用指定的方法。


WEB-INF/struts-config.xml

Java代码   收藏代码
  1. <span style="font-size: large;"><?xml version="1.0" encoding="ISO-8859-1" ?>  
  2.   
  3. <!DOCTYPE struts-config PUBLIC  
  4.           "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"  
  5.           "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">  
  6.   
  7. <struts-config>  
  8.     <form-beans>  
  9.     </form-beans>  
  10.   
  11.     <action-mappings>  
  12.         <action path="/forward"   
  13.             type="org.apache.struts.actions.ForwardAction"  
  14.             parameter="/index.jsp">  
  15.         </action>  
  16.           
  17.         <action path="/student"  
  18.             type="com.javacrazyer.web.action.StudentOptAction"  
  19.             parameter="method">  
  20.             <forward name="toAdd" path="/stu_toadd.jsp"/>  
  21.             <forward name="list" path="/stu_list.jsp"/>  
  22.             <forward name="update" path="/stu_update.jsp"/>  
  23.             <forward name="delete" path="/stu_delete.jsp"/>  
  24.         </action>  
  25.     </action-mappings>  
  26. </struts-config>  
  27. </span>  

 index.jsp

Java代码   收藏代码
  1. <span style="font-size: large;"><%@ page pageEncoding="UTF-8"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4.   <head>  
  5.     <title>Struts中DispatchAction的使用</title>  
  6.   </head>  
  7.     
  8.   <body>  
  9.     <h3>Struts应用:DispatchAction的使用</h3><hr/>  
  10.     <a href="student.do?method=list">查询学员列表</a><br/><br/>  
  11.     <a href="student.do?method=toAdd">添加学员</a><br/><br/>  
  12.     <a href="student.do?method=delete">删除学员</a><br/><br/>  
  13.       
  14.   </body>  
  15. </html>  
  16. </span>  

 StudentOptAction.java

Java代码   收藏代码
  1. <span style="font-size: large;">package com.javacrazyer.web.action;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5.   
  6. import org.apache.struts.action.ActionForm;  
  7. import org.apache.struts.action.ActionForward;  
  8. import org.apache.struts.action.ActionMapping;  
  9. import org.apache.struts.actions.DispatchAction;  
  10.   
  11. public class StudentOptAction extends DispatchAction {  
  12.   
  13.     public ActionForward list(ActionMapping mapping, ActionForm form,  
  14.             HttpServletRequest request, HttpServletResponse response)  
  15.             throws Exception {  
  16.         //  
  17.         System.out.println("到达Action中的list()方法了");  
  18.           
  19.         return mapping.findForward("list");  
  20.     }  
  21.       
  22.     public ActionForward toAdd(ActionMapping mapping, ActionForm form,  
  23.             HttpServletRequest request, HttpServletResponse response)  
  24.             throws Exception {  
  25.         System.out.println("到达Action中的toAdd()方法了");  
  26.           
  27.         return mapping.findForward("toAdd");  
  28.     }  
  29.       
  30.     public ActionForward delete(ActionMapping mapping, ActionForm form,  
  31.             HttpServletRequest request, HttpServletResponse response)  
  32.             throws Exception {  
  33.         System.out.println("到达Action中的delete()方法了");  
  34.           
  35.         return mapping.findForward("delete");  
  36.     }  
  37.       
  38.     public ActionForward update(ActionMapping mapping, ActionForm form,  
  39.             HttpServletRequest request, HttpServletResponse response)  
  40.             throws Exception {  
  41.         System.out.println("到达Action中的update()方法了");  
  42.           
  43.         return mapping.findForward("update");  
  44.     }  
  45. }  
  46. </span>  
 

stu_toadd.jsp

Java代码   收藏代码
  1. <span style="font-size: large;"><%@ page pageEncoding="UTF-8"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4.   <head>  
  5.     <title>添加一个学员</title>  
  6.   </head>  
  7.     
  8.   <body>  
  9.     <h3>添加一个学员</h3><hr/>  
  10.   </body>  
  11. </html>  
  12. </span>  

 stu_delete.jsp

Java代码   收藏代码
  1. <span style="font-size: large;"><%@ page pageEncoding="UTF-8"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4.   <head>  
  5.     <title>删除一个学员</title>  
  6.   </head>  
  7.     
  8.   <body>  
  9.     <h3>删除一个学员</h3><hr/>  
  10.   </body>  
  11. </html>  
  12. </span>  

 stu_update.jsp

Java代码   收藏代码
  1. <span style="font-size: large;"><%@ page pageEncoding="UTF-8"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4.   <head>  
  5.     <title>更新一个学员的信息</title>  
  6.   </head>  
  7.     
  8.   <body>  
  9.     <h3>更新一个学员的信息</h3><hr/>  
  10.   </body>  
  11. </html>  
  12. </span>  

 stu_list.jsp

Java代码   收藏代码
  1. <span style="font-size: large;"><%@ page pageEncoding="UTF-8"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4.   <head>  
  5.     <title>显示所有的学员列表</title>  
  6.   </head>  
  7.     
  8.   <body>  
  9.     <h3>显示所有的学员列表</h3><hr/>  
  10.   </body>  
  11. </html>  
  12. </span>  

 web.xml还是配置ActionServlet

Java代码   收藏代码
  1. <span style="font-size: large;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"   
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.   <servlet>  
  8.     <servlet-name>action</servlet-name>  
  9.     <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>  
  10.     <init-param>  
  11.         <param-name>config</param-name>  
  12.         <param-value>/WEB-INF/struts-config.xml</param-value>  
  13.     </init-param>  
  14.     <load-on-startup>2</load-on-startup>  
  15.   </servlet>  
  16.   
  17.   <servlet-mapping>  
  18.     <servlet-name>action</servlet-name>  
  19.     <url-pattern>*.do</url-pattern>  
  20.   </servlet-mapping>  
  21.       
  22.   <welcome-file-list>  
  23.     <welcome-file>index.jsp</welcome-file>  
  24.   </welcome-file-list>  
  25. </web-app>  
  26. </span>  
 
分享到:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值