Spring-MVC 里面的ModelAndView
2024-09-05 19:09:07
作用:
由于本身http是无状态的并不会保存什么请求信息。 但是目标页面可能又需要一些信息。这时候可以用ModelAndView存放一些业务数据之类等。然后返回给页面
用法:
比较重要的方法:
往对象里面加入变量
/**
* Add an attribute to the model.
* @param attributeName name of the object to add to the model (never {@code null})
* @param attributeValue object to add to the model (can be {@code null})
* @see ModelMap#addAttribute(String, Object)
* @see #getModelMap()
*/
public ModelAndView addObject(String attributeName, @Nullable Object attributeValue) {
getModelMap().addAttribute(attributeName, attributeValue);
return this;
}
设置需要放回的页面
/**
* Set a view name for this ModelAndView, to be resolved by the
* DispatcherServlet via a ViewResolver. Will override any
* pre-existing view name or View.
*/
public void setViewName(@Nullable String viewName) {
this.view = viewName;
}
使用:
@RequestMapping("/findAll")
public ModelAndView accFindAll(){
ModelAndView mv = new ModelAndView();
System.out.println("springmvcdone1");
Account account = accountService.findAll();
mv.addObject("at",account);
mv.addObject("atmoney",account.getMoney());
mv.setViewName("list");
System.out.println("springmvcdone2");
return mv;
}
jsp:
<html>
<head>
<title>findAll</title>
</head>
<body>
${requestScope.atmoney}
${requestScope.at.toString()}
${requestScope.get("atmoney")}
</body>
</html>
如果上面的jsp没有获取到变量的值
可能的原因
${resquestScope.get(“param_name”)}
${resquestScope.atmoney} 访问结果是字符串,没有访问到变量
在于老的JSP 1.2 声明.
JSP 1.2
EL是默认关闭的必须手动打开。<%@ page isELIgnored="false" %>
但如果是JSP2.0版就可以直接用。
只需要在编译的JSP文件中头文件中加入:<%@ page isELIgnored="false" %>
最新文章
- Excel导入导出组件的设计
- Silverlight中将WriteableBitmap互转byte数组
- 理解Java接口
- C#判断一个string是否为数字
- PowerDesigner更改数据库类型
- POJ 1041问题描述
- Linux下常用的shell命令记录1
- mvc4 http错误403.14 forbidden
- Arduino报错
- 《openstack 和hadoop的区别是什么?》
- html向servlet传乱码解决办法
- elasticsearch的javaAPI之query
- VC 为静态控件添加事件
- CCLayer在Touch事件(Standard Touch Delegate和Targeted Touch Delegate)
- swift设置textfield边框颜色
- EclipseIDE--使用整理
- 201771010126 王燕《面向对象程序设计(Java)》第十三周学习总结
- vue+vuex 回退定位到初始位置
- day 36 网络编程终结内容
- openssl版本升级操作记录