这次主要测试了ActionForward的redirect 属性以及静态和动态的ActinForward
1.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-bean
name
="loginActionForm"
type
="com.ycringfinger.struts.LoginActionForm"
></
form-bean
>
</
form-beans
>
<
global-forwards
>
<
forward
name
="login"
path
="/login.jsp"
redirect
="true"
></
forward
>
</
global-forwards
>
<
action-mappings
>
<
action
path
="/login"
type
="com.ycringfinger.struts.LoginAction"
name
="loginActionForm"
scope
="request"
>
<
forward
name
="success"
path
="/login_success.jsp"
></
forward
>
<
forward
name
="error"
path
="/login_error.jsp"
></
forward
>
</
action
>
<
action
path
="/mustlogin"
type
="com.ycringfinger.struts.MustLoginAction"
scope
="request"
>
<
forward
name
="success"
path
="/mustlogin.jsp"
></
forward
>
</
action
>
<
action
path
="/staticactionforward"
type
="com.ycringfinger.struts.StaticActionForwardTestAction"
scope
="request"
>
<
forward
name
="success"
path
="/staticactionforward.jsp"
></
forward
>
</
action
>
<
action
path
="/dynamicactionforward"
type
="com.ycringfinger.struts.DynamicActionForwardTestAction"
scope
="request"
>
</
action
>
</
action-mappings
>
</
struts-config
>

2.索引界面index.jsp:
<%
...
@ page language="java" import="java.util.*" pageEncoding="GB18030"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<
html
>
<
head
>
</
head
>
<
body
>
<
h1
>
测试ActionForward
</
h1
>
<
hr
>
<
li
>
测试ActionForward的redirect属性
</
li
><
br
>
<
a
href
="mustlogin.do"
>
访问受保护的界面
</
a
>
<
hr
>
<
li
>
测试静态的ActionForward
</
li
><
br
>
<
a
href
="staticactionforward.do"
>
这是一个静态的ActionForward
</
a
>
<
hr
>
<
li
>
测试动态的ActionForward
</
li
><
br
>
<
form
action
="dynamicactionforward.do"
>
页面:
<
input
type
="text"
name
="page"
>
<
input
type
="submit"
value
="browse"
>
</
form
>
</
body
>
</
html
>
3.StaticActionForwardTestAction.java:
package
com.ycringfinger.struts;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.Action;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;

public
class
StaticActionForwardTestAction
extends
Action
...
{

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
ActionForward af = mapping.findForward("success");
//af.setRedirect(true); 不能在这里修改ActionForward的属性,否则将会出错
return af;
}
}
4.DynamicActionForwardTestAction.java:
package
com.ycringfinger.struts;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.Action;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;

public
class
DynamicActionForwardTestAction
extends
Action
...
{

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
String page = request.getParameter("page");
ActionForward af = new ActionForward("/" + page + ".jsp", true);
return af;
}
}
本文介绍了Struts框架中ActionForward的使用方法,包括redirect属性的作用及如何实现静态和动态的ActionForward。通过实例展示了配置文件与不同类型的ActionForward实现方式。
1410

被折叠的 条评论
为什么被折叠?



