我理解的SpringMVC(二)页面传值

一、读取请求参数

JSP登录页面代码

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录</title>
</head>
<body>
	<form action="toIndex" method="post">
		账号:<input type="text" name="username" >
		密码:<input type="password" name="password" >
		<input type="submit" value="登录">
	</form>
</body>
</html>

通过请求访问该页面,再通过点击登录按钮,将账号和密码传给后台。

方式一:request对象

这里的request对象,其实是HttpServletRequest,将它作为方法的入参。获取时使用getParameter(),参数为获取参数的实参名即可。

	@RequestMapping("toIndex")
	public String toIndex(HttpServletRequest request){
		System.out.println("账号:"+request.getParameter("username"));
		System.out.println("密码:"+request.getParameter("password"));
		return "index";
	}

方式二:使用@RequestParam注解

使用该注解前需要了解到,针对如下方式,是可以成功接收到参数的,但是不是绝对,在Eclipse中是可以接收,但是在实际运用中,Java反射机制就不一定能读取到形参名了。如果无法读取到,该参数就会出现获取不到参数的情况。因此,此时推荐使用@RequstParam注解。该注解会告诉反射机制,该形参与哪一个实参对应。避免获取不到的情况。

	@RequestMapping("toIndex")
	public String toIndex(String username,String password){
		System.out.println("账号:"+username);
		System.out.println("密码:"+password);
		return "index";
	}

@RequestParam注解的参数是实参名,因此,方法的形参名和实参名可以不一致。

	@RequestMapping("toIndex")
	public String toIndex(@RequestParam("username") String username,@RequestParam("password") String password){
		System.out.println("账号:"+username);
		System.out.println("密码:"+password);
		return "index";
	}

方式三:使用JavaBean封装参数

我们可以使用一个类来封装传入的参数,也就是一个类中只要有参入的所有参数,就可以用来接收参数。
使用@RequestParam注解的方式还是不太常用,因为要给每一个参数加一个注解,还是非常不方便的,当参数数量很多的时候,会非常耗时耗力,因此,使用JavaBean的方式是最常用的。

	@RequestMapping("toIndex")
	public String toIndex(User user){
		System.out.println("账号:"+user.getUsername());
		System.out.println("密码:"+user.getPassword());
		return "index";
	}

User类:属性名需要与实参名一致。

package try01.entity;

public class User {
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public String getPassword() {
		return password;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + "]";
	}
	
}

二、向页面传值

jsp使用el表达式获取后端传来的值。

${username} 登陆成功

方式一:使用request

将数据通过request.setAttribute()绑定到request上,将数据转发给页面

	@RequestMapping("toIndex")
	public String toIndex(User user,HttpServletRequest request){
		request.setAttribute("username", user.getUsername());
		return "index";
	}

方式二:使用ModelAndView

将数据封装到ModelAndView对象中,返回将该对象作为方法的返回值返回。

	@RequestMapping("toIndex")
	public ModelAndView toIndex(User user){
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", user.getUsername());
		return new ModelAndView("index",map);
	}

方式三:使用ModelMap

将ModelMap作为形参,然后将数据封装到ModelMap中

	@RequestMapping("toIndex")
	public String toIndex(User user,ModelMap mm){
		mm.addAttribute("username",user.getUsername());
		return "index";
	}

方式四:使用Session

将数据封装到session中,转发给页面

	@RequestMapping("toIndex")
	public String toIndex(User user,HttpSession session){
		session.setAttribute("username",user.getUsername());
		return "index";
	}

三、重定向

SpringMVC默认是采用转发,如果需要重定向,需要修改一定代码。

1. 如果方法返回值是字符串

在返回字符串前加“redirect:”

	@RequestMapping("toIndex")
	public String toIndex(User user){
		return "redirect:toLogin2";
	}

2. 如果方法返回值是ModelAndView

先构造一个RedirectView对象,在将该对象给ModelAndView

	@RequestMapping("toIndex")
	public ModelAndView toIndex(User user){
		RedirectView rv = new RedirectView("toLogin2");
		return new ModelAndView(rv);
	}

参考

哔哩哔哩视频(51p开始):https://www.bilibili.com/video/BV1WZ4y1P7Bp?p=51

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值