重温经典struts1之八种页面跳转或请求转发的方式

前言

今天来学习下,struts1框架中实现页面跳转或请求转发的八种方式。


页面跳转方式

1. request的Dispatcher方法

这种方式在学习servlet编程中,我们学习过,适合任何项目的手动页面跳转或请求转发

action代码如下

request.getRequestDispatcher(“login.jsp”).forward(request, response);

login.jsp内容,可以参看我之前的文章

https://blog.csdn.net/dy051107/article/details/134743425

这种方式struts1中不推荐使用,
struts1中常用的页面跳转或请求转发一般使用ActionForward类的findForward方法结合struts-config.xml文件中的action标签内的forward标签来配置,下面的几种方式我们会讲到
这里作为了解
action示例代码如下:

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 ForwardAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		System.err.println("request getRequestDispatcher forward");
		request.getRequestDispatcher("login.jsp").forward(request, response);
		return null;

	}
}

2. response的重定向

同样servlet中我们学习过,struts1中不推荐使用,作为了解
因为struts1框架中也为我们提供重定向功能,下面的几种方式中我们会介绍
代码如下

response.sendRedirect(“/login.jsp”);

使用request和response的页面跳转,
action的返回值是null,这样就违反了,struts1框架中action返回ActionForward的设计原则

action示例代码如下:

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 ForwardAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		System.err.println("response redirect");
		response.sendRedirect("/login.jsp");
		return null;

	}
}

struts-config.xml的配置

    <action
        path="/MultForwardLogin"
        type="xxx.xxx.action.ForwardAction">
    </action>

3. ActionForward类的findForward方法结合struts-config.xml的配置

这种方式是struts1框架中常用的页面跳转或请求转发的方式
action中掉用ActionForward类的findForward方法进行页面跳转或请求转发
跳转的页面或请求在struts-config.xml文件中,使用forward标签进行配置
struts-config.xml的配置

    <action
        path="/MultForwardLogin"
        type="xxx.xxx.action.ForwardAction">
        <forward name="success" path="/login.jsp"></forward>
    </action>

action示例代码如下:

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 ForwardAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return mapping.findForward("success");

	}
}

4. struts-config.xml文件中,action标签的forward属性

这种方式比较简洁,无需编写action类
只需在struts-config.xml文件中配置action标签的forward属性
struts-config.xml的代码如下

    <action
        path="/MultForwardLogin" forward="/login.jsp">
    </action>

5. struts-config.xml文件中,全局跳转

这种方式多用在多个页面在某种场合下都跳转同一页面时使用,
例如,每个页面都需用户login后,才能使用时,使用全局统一配置
这样就不需要在每个action中都配置跳转同一页面
redirect默认是false,页面跳转或请求转发
当设置成true时,进行重定向
struts-config.xml配置代码如下

	<global-forwards>
        <forward name="login" path="/login.jsp" redirect="true"></forward>
	</global-forwards>

6. ActionForward类的getInputForward方法结合struts-config.xml的配置

常用在当action中发生输入验证错误或业务错误时,将错误信息返回本页面时使用
例如,login页面,用户输入不正确的用户名和密码时,错误信息显示在本页面时使用
action中调用ActionForward类的getInputForward方法跳转
在struts-config.xml文件中,action标签的input属性配置跳转目标页面

    <action
        path="/login"
        type="xxx.xxx.action.LoginAction"
        name="loginForm"
        scope="request"
        input="/login.jsp">
        <forward name="success" path="/login_success.jsp"></forward>
    </action>

action代码如下

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;

import xxx.xxx.form.LoginForm;

public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		LoginForm lf = (LoginForm) form;
		String username = lf.getUsername();
		String password = lf.getPassword();

		if ("admin".equals(username) && "admin".equals(password)) {
			request.getSession().setAttribute("username", username);
			return mapping.findForward("success");
		} else {
			request.setAttribute("msg", "用户名和密码不正确");
			return mapping.getInputForward();
		}
	}
}

7. struts-config.xml文件中,action标签的unkown属性

unkown属性也是一个全局属性,当用户发送不存在的*.do的请求时,
struts1框架会为我们跳转到unkown属性配置的目标页面

struts-config.xml配置代码如下

    <action forward="/404.jsp" unknown="true">
    </action>

8. 动态跳转

根据页面输入的值,动态跳转到指定页面时,使用动态跳转,
在action中重新new ActionForward类,设定Path属性值,并返回。
下边的示例是,页面输入1…n,跳转到page1.jsp…pagen.jsp

action代码如下:

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 DynaForwardAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		ActionForward af = new ActionForward();
		af.setPath("/page" + request.getParameter("page") + ".jsp");
		return af;
	}
}

struts-config.xml文件中的action配置如下:

     <action
         path="/dyna_forward"
         type="xxx.xxx.action.DynaForwardAction">
     </action>

jsp代码如下

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<form action="dyna_forward.do" method="post">
		page:<input type="text" name ="page"><br>
		<input type="submit" value="forward">
	</form>

</body>
</html>

总结

以上就是struts1框架中的八种页面跳转或请求转发的方式总结,欢迎留言补充,下篇见。

  • 20
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值