SpringMVC之重定向与转发


显式指定重定向:redirect)

一、重定向

1、登录页面

<%@ page language="java" contentType="text/html; charset=Utf-8"
    pageEncoding="Utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<form action="${pageContext.request.contextPath}/aa/login5.action" method="post">
		用户名:<input type="text" name="username">
		密码:<input type="password" name="password">
		<input type="submit" name="登录">
	</form>
</body>
</html>

在这里插入图片描述

2、登录成功页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	欢迎${param.username},登陆成功
</body>
</html>

在这里插入图片描述

3、servlet控制层

package com.java.demo1.controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.java.demo1.domain.User;

//普通类被用Controller注释就变成了servlet
@Controller
@RequestMapping(value="/aa")
@Scope("prototype") //控制器一般都要使用多例,单例会影响效率
public class ControllerDemo1 {
	
	@RequestMapping(value = "/login5.action")
	public String login5(String username,String password) {
		if("haorenren".equals(username) && "123456".equals(password)) {
			//重定向(地址会发生变化;redirect),相对于工程
			//return "redirect:/page/success.jsp";
			//重定向加参数带数据
			return "redirect:/page/success.jsp?username="+username;
		}else {
			return "redirect:/page/login.jsp";
		}
	}
	
}

二、转发

1)、转发

注意:放在web-inf的页面没有办法直接访问,可以通过servlet转发才能访问
在这里插入图片描述

1、登录页面

<%@ page language="java" contentType="text/html; charset=Utf-8"
    pageEncoding="Utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<form action="${pageContext.request.contextPath}/aa/login4.action" method="post">
		用户名:<input type="text" name="username">
		密码:<input type="password" name="password">
		<input type="submit" name="登录">
	</form>
</body>
</html>

在这里插入图片描述

2、登录成功要显示的页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	欢迎${username},登陆成功
</body>
</html>

在这里插入图片描述

3、servlet控制层

package com.java.demo1.controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.java.demo1.domain.User;

//普通类被用Controller注释就变成了servlet
@Controller
@RequestMapping(value="/aa")
@Scope("prototype") //控制器一般都要使用多例,单例会影响效率
public class ControllerDemo1 {
	
	@RequestMapping(value = "/login4.action")
	public String login4(String username,String password) {
		if("haoren".equals(username) && "123".equals(password)) {
			//转发(地址不会发生变化)
			return "/page/success.jsp";
		}else {
			return "/page/login.jsp";
		}
	}
	
}

2)、使用视图解析器的转发

1、配置视图解析器
prefix:前缀;value里写路径
suffix:后缀;value写后缀名
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	
	<!-- 自动扫描Spring的注解 只扫描控制层 -->
	<context:component-scan base-package="com.java.demo1.controller"></context:component-scan>
	<!-- 扫描springmvc中的注解 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 视图解析器(只对转发有效,重定向无效) -->
	<bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/page/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

2、servlet控制层

package com.java.demo1.controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.java.demo1.domain.User;

//普通类被用Controller注释就变成了servlet
@Controller
@RequestMapping(value="/aa")
@Scope("prototype") //控制器一般都要使用多例,单例会影响效率
public class ControllerDemo1 {
	
	@RequestMapping(value = "/login4.action")
	public String login4(String username,String password) {
		if("haoren".equals(username) && "123".equals(password)) {
			//转发(地址不会发生变化)
			//return "/page/success.jsp";
			return "success";
		}else {
			//return "/page/login.jsp";
			return "login";
		}
	}
	
}

3)、springMVC带数据给jsp页面的三种方式

3.1、request带值

@RequestMapping(value = "/login6.action")
	public String login6(String username,String password,HttpServletRequest request) {
		if("haoren".equals(username) && "123".equals(password)) {
			//转发从后台往前台转递参数
			request.setAttribute("username", username);
			//转发(地址不会发生变化)
			return "success";
		}else {
			//return "/page/login.jsp";
			return "login";
		}
	}

3.2、model的方式(默认存放到request中)

@RequestMapping(value = "/login7.action")
	public String login7(String username,String password,Model model) {
		if("haoren".equals(username) && "123".equals(password)) {
			//转发从后台往前台转递参数
			model.addAttribute("username", username);
			//转发(地址不会发生变化)
			return "success";
		}else {
			//return "/page/login.jsp";
			return "login";
		}
	}

3.3、map集合带数据

@RequestMapping(value = "/login8.action")
	public String login8(String username,String password,Map<String,Object> map) {
		if("haoren".equals(username) && "123".equals(password)) {
			//转发从后台往前台转递参数
			map.put("username", username);
			//转发(地址不会发生变化)
			return "success";
		}else {
			//return "/page/login.jsp";
			return "login";
		}
	}

扩:对象带数据

1、User

package com.java.demo1.domain;

import java.util.Date;

public class User {
	private String username;
	private String password;
	private String hobby;
	private Date birthday;//支持这样的格式:1988/09/09;一般可以采用String来进行接收
	private double score;//基础数据类型可以自动转换
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getHobby() {
		return hobby;
	}
	public void setHobby(String hobby) {
		this.hobby = hobby;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	
}

2、servlet控制层

	//对象带
	@RequestMapping(value = "/login9.action")
	public String login9(Model model) {
		User user = new User();
		user.setUsername("张三");
		user.setPassword("123456");
		user.setHobby("吃,睡");
		try {
			//字符串转时间
			String birthday = "2021/2/12";
			Date date = new SimpleDateFormat("yyyy/MM/dd").parse(birthday);
//			//转成我们常见的日期格式
//			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
//			String a = simpleDateFormat.format(date);
//			System.out.println(a);
			user.setBirthday(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		model.addAttribute("user",user);
		return "success";
	}

3、jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	账户:${user.username }
	密码:${user.password }
	爱好:${user.hobby }
	出生:${user.birthday }
</body>
</html>

在这里插入图片描述

扩:集合带数据

1、User

package com.java.demo1.domain;

import java.util.Date;

public class User {
	private String username;
	private String password;
	private String hobby;
	private Date birthday;//支持这样的格式:1988/09/09;一般可以采用String来进行接收
	private double score;//基础数据类型可以自动转换
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getHobby() {
		return hobby;
	}
	public void setHobby(String hobby) {
		this.hobby = hobby;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	
}

2、servlet控制层

//使用集合带
	@RequestMapping(value = "/login10.action")
	public String login10(Model model) {
		User user1 = new User();
		user1.setUsername("张三");
		user1.setPassword("123456");
		user1.setHobby("吃,睡");
		try {
			//字符串转时间
			String birthday = "2021/2/12";
			Date date = new SimpleDateFormat("yyyy/MM/dd").parse(birthday);
			user1.setBirthday(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		User user2 = new User();
		user2.setUsername("李四");
		user2.setPassword("78910");
		user2.setHobby("老,大");
		try {
			//字符串转时间
			String birthday = "2021/2/16";
			Date date = new SimpleDateFormat("yyyy/MM/dd").parse(birthday);
			user2.setBirthday(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//集合
		List<User> list = new ArrayList<User>();
		Collections.addAll(list, user1,user2);
		
		model.addAttribute("list", list);
		return "success";
	}

3、Jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>	
	<c:forEach items="${list }" var="user">
		账户:${user.username }
		密码:${user.password }
		爱好:${user.hobby }
		出生:${user.birthday }
	</c:forEach>
</body>
</html>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值