该篇博客利用三个场景来介绍ModelAndView、Model及Map、SessionAttributes为参数的请求处理方法
ModelAndView
1、场景
获取当前时间(采用ModelAndView)
2、解决方案
利用ModelAndView
作用
- 对象被放置在Request对象中
- 设置转向地址
- 将底层获取的数据进行存储(或者封装)
最后将数据传递给View
package linjie.springmvc.handler; import java.util.Date; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** * @author 浅然 xulinjie0105@gmail.com * @version 创建时间:2018年5月19日 上午10:50:05 * ModelAndView */ @Controller @RequestMapping("/springmvc") public class SpringmvcTestModelAndView { /* * ModelAndView * now为key * new Date()为value */ @RequestMapping("TestModelAndView") public ModelAndView testmodelandview() { ModelAndView modelandview = new ModelAndView("hello"); modelandview.addObject("now",new Date()); return modelandview; } }
访问URL:http://localhost:8080/Springmvc_RequestMapping/springmvc/TestModelAndView
Model
- 对象被放置在Request对象中
1、场景
获取用户姓名年龄并在指定页面输出
2、解决方案
package linjie.springmvc.handler;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 浅然 xulinjie0105@gmail.com
* @version 创建时间:2018年5月19日 上午10:50:05
* Model
*/
@Controller
@RequestMapping("/springmvc")
public class SpringmvcTestModel {
/*
* model
* time为key
* new Date()为value
*/
@RequestMapping("testmodel")
public String testmodel(Model model,User user) {
model.addAttribute("time", new Date());
model.addAttribute("name", user.getUsername());
model.addAttribute("age",user.getAge());
return "hello";
}
}
User.java
package linjie.springmvc.handler;
/**
* @author 浅然 xulinjie0105@gmail.com
* @version 创建时间:2018年5月19日 上午11:46:28
* UserPOJO
*/
public class User {
private String username;
private int age;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "User [username=" + username + ", age=" + age + "]";
}
}
表单提交
<form action="springmvc/testmodel">
<input type="text" name="username"><br>
<input type="text" name="age"><br>
<input type="submit" value="submit">
</form>
显示数据页面
${requestScope.name}<br>
${requestScope.time }<br>
${requestScope.age }<br>
结果
源码接口
map用法与model类似
/*
* map
*/
@RequestMapping("testmap")
public String testmap(Map<String, Object> map,User user) {
map.put("name", user.getUsername());
return "hello";
}
@SessionAttributes注解
1、场景
因为以上对象都是放入request对象中,现在希望将Model中属性名为name的属性放到Session属性列表中,以便这个属性可以跨请求访问,那么就需要@SessionAttributes注解了
注意:@SessionAttributes这个注解只能放到类的上面
2、解决方案
package linjie.springmvc.handler;
import java.util.Date;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
/**
* @author 浅然 xulinjie0105@gmail.com
* @version 创建时间:2018年5月19日 上午10:50:05
* ModelAndView
*/
@Controller
@RequestMapping("/springmvc")
/*
* types:根据指定参数的类型,将模型中对应类型的参数存储到session中
* values:要保存到session中的参数名称
*/
@SessionAttributes(value= {"name"},types= {Integer.class})//将Model中属性名为name的属性放到Session属性列表中,以便这个属性可以跨请求访问
public class SpringmvcTestModelAndView {
/*
* SessionAttributes
*/
@RequestMapping("testattributes")
public String testattributes(Model model) {
model.addAttribute("name","linjie");
model.addAttribute("nameasasd","aslinjie");
model.addAttribute("age",23);
model.addAttribute("weight",120);
return "hello";
}
}
- types:根据指定参数的类型,将模型中对应类型的参数存储到session中
- values:要保存到session中的参数名称
types= {Integer.class}只要是int类型的就会存储到session中
结果
可以看出int类型的在request和session中都有,而其他类型如果没有指定的话session则没有
注意
1、@SessionAttributes只能在类上定义
2、其中的每个属性可存储多个key
– @SessionAttributes(value={“user1”, “user2”})
– @SessionAttributes(types={User.class, Dept.class})
– @SessionAttributes(value={“user1”, “user2”},types={Dept.class})