Struts 1 学习笔记-4-4(Struts中ActionForward的测试)

这次主要测试了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 >
    
< href ="mustlogin.do" > 访问受保护的界面 </ a >
    
    
< hr >
    
< li > 测试静态的ActionForward </ li >< br >
    
< 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;
    }


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值