Struts Action深入

ForwardAction

在Struts中通过org.apache.struts.actions.ForwardAction类可以将一个JSP页面作为一个Action进行映射,通过配置struts-config.xml文件即可

定义一个跳转页----hello.jsp

<%@ page language="java" pageEncoding="GBK"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<html:html lang="true">
<head>
	<html:base />
	<title>hello.jsp</title>
</head>
<body>
	<h2>HELLO MLDN !!!</h2>
</body>
</html:html>
配置struts-config.xml

	<action-mappings>
		<action path="/hello"
			type="org.apache.struts.actions.ForwardAction"
			parameter="/hello.jsp" />
	</action-mappings>
通过超链接访问hello.do----forindex.jsp

<%@ page language="java" pageEncoding="GBK"%>
<html>
<head>
       <title></title>
</head>
<body>
       <h2><a href="hello.do">连接到hello.do页面</a></h2>
</body>
</html>

在标准的MVC设计模式中,所有的JSP都应该通过Servlet(Struts中为Action)跳转到另外一个JSP页面,所以在Struts中为了提倡这种概念,而引入了ForwardAction做一个中间跳转的Action。


IncludeAction

在Struts中也为引入资源提供了一个org.apache.struts.actions.IncludeAction类,通过此Action的配置,可以将一个页面进行导入。此Action也需要通过配置完成,配置如下:

将hello.jsp配置到IncludeAction中----修改struts-config.xml

<action-mappings>
	<action path="/inc" type="org.apache.struts.actions.IncludeAction"
		parameter="/hello.jsp" />
</action-mappings>
包含配置的IncludeAction----incindex.jsp

<%@ page language="java" pageEncoding="GBK"%>
<html>
<head>
       <title></title>
</head>
<body>
<span style="white-space:pre">	</span></span><h2>你好,小明</h2>
<span style="white-space:pre">	</span></span><jsp:include page="inc.do" />
</body>
</html>



DispatchAction

在原始的Struts开发中,一个Action中只包含一个execute()方法,但是如果此时的项目很大,则会有多个业务类似的Action出现,会造成后期的维护困难,所以在Struts中为了解决这样的问题,专门增加了一个DispatchAction类,此类集成Action类,并且完成分发的处理操作。

分发Action的使用与普通的Action类似,仍然要被一个类所继承,并且根据要求覆写方法,唯一不同的是,此时的方法可以有多个,而且着多个方法分别表示着不同的操作。

定义一个分发Action----EmpAction.java

package org.lxh.struts.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;
import org.lxh.struts.form.EmpForm;

public class EmpAction extends DispatchAction {

	public ActionForward insert(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmpForm empForm = (EmpForm) form;// TODO Auto-generated method stub
		System.out.println("*** 增加操作") ;
		return null;
	} 
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmpForm empForm = (EmpForm) form;// TODO Auto-generated method stub
		System.out.println("*** 更新操作") ;
		return null;
	}
}

配置分发Action----修改struts-config.xml

<action-mappings>  
<action
      attribute="empForm"
      input="/form/emp.jsp"
      name="empForm"
      <strong>parameter="status"</strong>
      path="/emp"
      scope="request"
      type="org.lxh.struts.action.EmpAction" />
</action-mappings>
在分发Action操作中一个决定性的参数就是parameter,以后再传递是根据status的内容会调用EmpAction中的指定方法,

例如:
调用insert()方法:status=insert

调用update()方法:status=update

分发Action不能编写exectue()方法。

如果一个类集成了DispatchAction类,则在此类中一定不能编写execute()方法,否则像insert()和update()方法都将无法正确调用。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值