Struts温习(3)--DispactherAction的用法

本文介绍了Struts框架中DispatchAction的使用方法,通过配置一个Action映射并利用parameter属性,实现不同方法的调用,简化了代码结构,提高了开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先我们来考虑一个问题,像在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

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
	<form-beans>
	</form-beans>

	<action-mappings>
		<action path="/forward" 
			type="org.apache.struts.actions.ForwardAction"
			parameter="/index.jsp">
		</action>
		
		<action path="/student"
			type="com.javacrazyer.web.action.StudentOptAction"
			parameter="method">
			<forward name="toAdd" path="/stu_toadd.jsp"/>
			<forward name="list" path="/stu_list.jsp"/>
			<forward name="update" path="/stu_update.jsp"/>
			<forward name="delete" path="/stu_delete.jsp"/>
		</action>
	</action-mappings>
</struts-config>

 index.jsp

<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Struts中DispatchAction的使用</title>
  </head>
  
  <body>
    <h3>Struts应用:DispatchAction的使用</h3><hr/>
    <a href="student.do?method=list">查询学员列表</a><br/><br/>
    <a href="student.do?method=toAdd">添加学员</a><br/><br/>
    <a href="student.do?method=delete">删除学员</a><br/><br/>
    
  </body>
</html>

 StudentOptAction.java

package com.javacrazyer.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class StudentOptAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//
		System.out.println("到达Action中的list()方法了");
		
		return mapping.findForward("list");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("到达Action中的toAdd()方法了");
		
		return mapping.findForward("toAdd");
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("到达Action中的delete()方法了");
		
		return mapping.findForward("delete");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("到达Action中的update()方法了");
		
		return mapping.findForward("update");
	}
}
 

stu_toadd.jsp

<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>添加一个学员</title>
  </head>
  
  <body>
    <h3>添加一个学员</h3><hr/>
  </body>
</html>

 stu_delete.jsp

<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>删除一个学员</title>
  </head>
  
  <body>
    <h3>删除一个学员</h3><hr/>
  </body>
</html>

 stu_update.jsp

<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>更新一个学员的信息</title>
  </head>
  
  <body>
    <h3>更新一个学员的信息</h3><hr/>
  </body>
</html>

 stu_list.jsp

<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>显示所有的学员列表</title>
  </head>
  
  <body>
    <h3>显示所有的学员列表</h3><hr/>
  </body>
</html>

 web.xml还是配置ActionServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
    	<param-name>config</param-name>
    	<param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值