Spring MVC示例及常用注解(二)

纸上得来终觉浅

1.Spring MVC方法的参数

1)可以使用POJO对象绑定请求参数值


2)使用Servlet API作为入参,可以使用如下类型的参数:


2.处理模型数据

1)ModelAndView,设置方法的返回值类型为ModelAndView,示例如下:

HelloWorld.java:

public class HelloWorld {
	@RequestMapping("/helloMVC")
	public ModelAndView helloWorld(){
		System.out.println("HelloWorld.helloWorld()");
		ModelAndView mv = new ModelAndView();
		
		User user = new User();
		user.setAge("10");
		user.setName("zhang");
		
		mv.setViewName("view");
		mv.addObject("nihao",user);
		return mv;
	}
}

view.jsp:

${requestScope.nihao}

注:放入mv中的对象的作用域是request;

2)方法的入参为Map:

HelloWorld.java:

package roadArchitectWeb.Test;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class HelloWorld {
	@RequestMapping("/helloMVC")
	public String helloWorld(Map<String,Object> map){
		User user = new User();
		user.setAge("10");
		user.setName("li");
		map.put("nihao", user);
		
		return "view";
	}
}
view.jsp文件不变

注:放入map中的作用域也是request;

3)@SessionAttributes



HelloWorld.java:

package roadArchitectWeb.Test;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
//使用SessionAttribute注解后,session域中保存了相关的对象
@SessionAttributes(value={"nihao"},types={Student.class})
@Controller
public class HelloWorld {
	@RequestMapping("/helloMVC")
	public String helloWorld(Map<String,Object> map){
		User user = new User();
		user.setAge("10");
		user.setName("li");
		/*user既在request中,也在session中保存*/
		map.put("nihao", user);
		
		Student student = new Student();
		student.setHigh(100);
		student.setName("zhou");
		/*这个对象只在session域中保存*/
		map.put("nihao2",student);
		
		return "view";
	}
}
view.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${requestScope.nihao}
<br><br>
${sessionScope.nihao}
<br><br>
${sessionScope.nihao2}
<br><br>
</body>
</html>
4)@ModelAttribute注解


HelloWorld.java:

package roadArchitectWeb.Test;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
//使用SessionAttribute注解后,session域中保存了相关的对象
@SessionAttributes(value={"nihao"})
@Controller
public class HelloWorld {
	@ModelAttribute
	public void Attr(@RequestParam(value="id",required=false) Integer id,
			Map<String, Object> map){
		User user = new User();
		user.setAge("12");
		user.setName("zhao");
		map.put("key",user);
	}
	
	@RequestMapping("/helloMVC")
	public void helloWorld(@ModelAttribute("key") User user){
		System.out.println("HelloWorld.helloWorld():"+user);
	}
	
	@RequestMapping("/hello")
	public String index(){
		return "view";
	}
}
view.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="/roadArchitectWeb/helloMVC">
age:<input type="text" name="age"></input>
<input type="submit" value="submit"></input>
</form>
</body>
</html>
Student.java:

package roadArchitectWeb.Test;

public class Student {
	private String name;
	private Integer high;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getHigh() {
		return high;
	}
	public void setHigh(Integer high) {
		this.high = high;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", high=" + high + "]";
	}
}
注:@ModelAttribute解决了这样一个问题,一般来说,当需要对数据库中的一天记录进行修改时,有两种不是很好的方法:

A)把数据取出来,放入jsp页面中,包括密码等,然后修改后再从jsp返回,这样的做法不安全;

B)把需要修改的数据放入jsp中,从jsp返回到后台时,根据ID重新查找这条记录,然后将修改的数据覆盖原来的数据;这样每次都要查询,代码重用率较低,可以把重新查找写成一个方法,这就是@ModelAttribute的作用;

3.@SessionAttributes引发的异常

紧跟这上面的代码,如果我们把He

	@RequestMapping("/helloMVC")
	public void helloWorld(@ModelAttribute("nihao") User user){
		System.out.println("HelloWorld.helloWorld():"+user);
	}
@ModelAttribute的值由“key”改为”nihao“,会引发这样一个异常:

org.springframework.web.HttpSessionRequiredException: Session attribute 'nihao' required - not found in session
这就引出了一个问题,给参数进行@ModelAttribute注解时,查找“key”的顺序:

1)首先会查找@ModelAttribute修饰的方法,找到方法中的map(map是方法的参数)中存放的key;没有查找下一条

2)查找@SessionAttributes的value,如果有这样一个value,则会按照1)进行查找,这个时候一定是查找不到的,所以会报异常; 没有查找下一条

3)这时会用发射的方法新建一个对象;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值