[SpringMVC]处理模型数据

传参解决了,但是怎么把需要的数据传回来显示到页面上?

ModelAndView

是放进request域对象里。在页面用EL表达式取request域的东西即可

	@RequestMapping("/ModelAndView")
	public ModelAndView testModelAndView() {

		String viewName = SUCCESS;
		ModelAndView modelAndView = new ModelAndView(viewName);
		modelAndView.addObject("time","skjlkajglkagdsg");
		// modelAndView.addAllObjects(modelMap);
		// modelAndView.addObject(attributeValue);

		System.out.println("ModelAndView");
		return modelAndView;

	}
<a href="springModel/ModelAndView">ModelAndView</a>
ModelAndView-->:time:${requestScope.time}

Map和Model

同样是是放进request域对象里

@RequestMapping("/testMap")
	public String testMap(Map<String, Object> map) {
		System.out.println(map.getClass().getName());
		map.put("names", Arrays.asList("yiki", "tiffany", "light"));
		return SUCCESS;

	}
<a href="springModel/testMap">testMap</a>
   names-->:names:${requestScope.names}

@SessionAttribute

返回的数据放进session里,注意用法是在类名上添加,指定属性即可

【value={"map里的键名,是一个实体类"}】

【types={String.class}】在map里放String类型的东西


@SessionAttributes(value={"user"},types={String.class})//注意是和testSession里map的键名一致,是一个数组,可以放很多哦~
@RequestMapping("/springModel")
@Controller
public class Model {

	private static final String SUCCESS = "success";

	@RequestMapping("/ModelAndView")
	public ModelAndView testModelAndView() {

		String viewName = SUCCESS;
		ModelAndView modelAndView = new ModelAndView(viewName);
		modelAndView.addObject("time", "skjlkajglkagdsg");
		// modelAndView.addAllObjects(modelMap);
		// modelAndView.addObject(attributeValue);

		System.out.println("ModelAndView");
		return modelAndView;

	}
	@RequestMapping("/testSession")
	public String testSession(Map<String, Object> map) {
		System.out.println(map.getClass().getName());
		User user = new User("yiki");
		map.put("user",user);
		map.put("string", "llllllaaaa");
		return SUCCESS;
	}
<a href="springModel/testSession">testSession</a><br>
    session-->:user:${sessionScope.user}<br>
    session-->:string:${sessionScope.string}<br>




猜你喜欢

转载自blog.csdn.net/qq_38277033/article/details/80718100