uc浏览器版本有要求吗

springMVC ModelAndView 作用与功能解析 【转】 - 南开小巷 - 博客园
随笔 - 148
Spring mvc视图机制
所有的web应用的mvc框架都有它定位视图的方式。Spring提供了视图解析器供你在浏览器中显示模型数据,而不必被拘束在特定的视图技术上。
Spring的控制器Controller会返回一个ModelAndView的实例。Spring根据ModelAndView实例中的View和Model把信息反馈给用户。Spring中的视图是以名字为标识的,ViewResolver是通过名字来解析view的。Spring提供了多种视图和视图解析器。
A、ModelAndView
org.springframework.web.servlet.ModelAndView
public class ModelAndView extends Object
ModelAndView如其名称所示,它代表了Spring Web MVC中呈现画面时所使用的Model与View,由于Java一次只能返回一个物件,所以ModelAndView的作用封装这两个物件,以方便您一次返回Model与View这两个物件。
ModelAndView(String viewName)
Convenient constructor when there is no model data to expose.
最简单的ModelAndView是只有View的名称,之后View名称被View resolver,也就是org.springframework.web.servlet.View的实例解析,例如&InternalResourceView或JstlView等等。
ModelAndView(String viewName, Map model)
Creates new ModelAndView given a view name and a model.
如果您要返回呈现画面时所需的Model资料,则可以使用Map来收集呈现View时所需的资料,然后在建构ModelAndView作为建构时的参数。
ModelAndView(String viewName, String modelName, Object modelObject)
Convenient constructor to take a single model object.
返回单个model时使用。
B、ViewResolver(视图解析器)
org.springframework.web.servlet.ViewResolver
public interface ViewResolver
现在我们有了view名称,也有了显示时需要的model资料,那么我们如何显示view了。这就需要用到ViewResolver,它提供了从视图名称到实际视图的映射。
(例如我们得到的view名称为test,通过ViewResolver我们把它映射到/WEB-INF/jsp/test.jsp的资源上,当然也可以把test映射到test.pdf的资源上,这部分工作由ViewResolver来完成,但是具体如何显示test.jsp或test.pdf,就需要View来实现了)。springmvc中Model ModelAndView ModelMap Map 的理解 [转] - 疯码牛 - 博客园
posts - 2, comments - 0, trackbacks - 0, articles - 30
1.ModelAndView的用法 个人认为它不能直接放在方法的参数中,而是需要在方法中通过new来创建。如下所示
可以在方法中直接
ModelAndView &mv = new&ModelAndView &() ;
将页面需要获取的值放入mv中 。直接return mv &&&指向那个页面,通过 &类名上的注解名称/ 方法上的注解的名称,找到对应的页面。
它找页面是通过类上的注解名 和 &该方法的上的注解名 &两者拼接找页面
2.Model &的用法。。他不需要new 直接放在方法的参数中。&个人认为ModelMap跟model的用法一模一样。不需要new直接放在方法的参数中 如
&Model & &和 &&&ModelMap && & &是通过return的值去找页面。如: &&&return "home/login" & 直接去找home下的login这个页面
public String createOrder(
@RequestParam(value = "productInfoId", required = true) String proId,
@RequestParam(value = "buyNum", required = true) Integer buyNum,Model
& & & & & return "home/login" ;
& & & & //return "redirect:../account/banks";
& &页面跳转
如果上面2中 return
null &也就是为null
& springmvc也会帮我们找页面。也是通过&类上的注解名 和 &该方法的上的注解名 &两者拼接找页面
我们把需要的值全部放入model中。页面根据返回的字符串去找对应的页面。这样可以选在自己想要返回的页面。。
就会从webapp/home下面 找login开头的页面。
modelandview
import org.springframework.web.servlet.ModelAndView (正确应该引入这个)&
improt org.springframework.web.portlet.ModelAndView (这个是错误的)
异步请求到springmvc
的controller
因为Model是直接放在方法的参数中,我们不需要去new
,我们把我们需要的值直接
model.addAttribute("flag", 1);我们不需要去return model 直接在页面通过EL表达式就可以获取。这种适用于值放在jsp页面中
第二方式,@RequestMapping(value
= "/getuserinfo")
&& &public @ResponseBody ModelMap getUser(
&& &&& &&& &@RequestParam(value = "entId",
required = true) String entId) {}& 这里我们使用的是返回ModelMap对象,
ModelMap map = new ModelMap();
map.addAttribute("flag", 1)
这里需要return
map这样在页面才能够获取到值(这里return
的值比较适用于在js中)。 这种适用于
&& &&& &&& &type : 'post',
&& &&& &&& &url : serviceUrl,
&& &&& &&& &data : {
&& &&& &&& &&&
&"entId" : entId
&& &&& &&& &},
&& &&& &&& &success :
function(data) {
&& &&& &&& &&&
&$("#uuid").val(data.enterprise.uuid);
&& &&& &&& &}
&& &&& &});springmvc 拦截器中ModelAndView为null
首先在controller方法是这样的:
public void methodName(ModelMap model, ...............){
model.put(XXX,XXX);
有两种情况:
1.controller方法参数中带HttpServletResponse response时,方法处理完之后,
到了Interceptor 中(extends HandlerInterceptorAdapter)
postHandle方法的modelAndView参数为null
这样如果需要在Interceptor 中使用modelAndView只能在controller方法返回ModelAndView
public ModelAndView methodName(HttpServletResponse response, ...............){
ModelAndView model = new&ModelAndView ();
model.addObject(XXX,XXX);
2.controller方法参数中不带HttpServletResponse response时,方法处理完之后,
到了Interceptor 中(extends HandlerInterceptorAdapter)
postHandle方法的modelAndView参数是有值的
——————————————————————————————————————————
有人知道是什么原因吗,难道方法参数中带response会使spring从response中去设置modelAndView?
还真是这种情况,不过平时很少用postHandle,用preHandle比较多。。
--- 共有 1 条评论 ---
目前发现的就是,response和modelmap不要同时存在controller方法参数中,不然用modelmap设置model的话返回时会有问题。spring debug日志显示Null ModelAndView returned ....
http://www.rowkey.me/blog//spring-mvc-httpservletresponse/spring MVC之构造ModelAndView对象 - fhd001 - ITeye博客
博客分类:
spring MVC之构造ModelAndView对象
----------
构造ModelAndView对象
当控制器处理完请求时,通常会将包含视图名称或视图对象以及一些模型属性的ModelAndView对象返回到DispatcherServlet。因此,经常需要在控制器中构造ModelAndView对象。ModelAndView类提供了几个重载的构造器和一些方便的方法,让你可以根据自己的喜好来构造ModelAndView对象。这些构造器和方法以类似的方式支持视图名称和视图对象。
当你只有一个模型属性要返回时,可以在构造器中指定该属性来构造ModelAndView对象:
package com.apress.springrecipes.court.
import org.springframework.web.servlet.ModelAndV
import org.springframework.web.servlet.mvc.AbstractC
public class WelcomeController extends AbstractController{
public ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response)throws Exception{
Date today = new Date();
return new ModelAndView("welcome","today",today);
如果有不止一个属性要返回,可以先将它们传递到一个Map中再来构造ModelAndView对象。
package com.apress.springrecipes.court.
import org.springframework.web.servlet.ModelAndV
import org. springframework.web.servlet.mvc.AbstractC
public class ReservationQueryController extends AbstractController{
public ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response)throws Exception{
Map&String,Object& model = new HashMap&String,Object&();
if(courtName != null){
model.put("courtName",courtName);
model.put("reservations",reservationService.query(courtName));
return new ModelAndView("reservationQuery",model);
Spring也提供了ModelMap,这是java.util.Map实现,可以根据模型属性的具体类型自动生成模型属性的名称。
package com.apress.springrecipes.court.
import org.springframework.ui.ModelM
import org.springframework.web.servlet.ModelAndV
import org.springframework.web.servlet.mvc.AbstractC
public class ReservationQueryController extends AbstractController{
public ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response)throws Exception{
ModelMap model = new ModelMap();
if(courtName != null){
model.addAttribute("courtName",courtName);
model.addAttribute("reservations",reservationService.query(courtName));
return new ModelAndView("reservationQuery",model);
由于这两个模型属性的类型为String和List&Reservation&,ModelMap会为它们生成默认的名称----string和reservationList。如果你不喜欢这些名称,可以显式地指定它们。
构造完ModelAndView对象之后,仍然可以利用addobject()方法为它添加模型属性。这个方法返回ModelAndView对象
本身,因此可以在一个语句中构造ModelAndView对象。请注意,你也可以省略addObject()方法的属性名称。在这种情况下,这个方法会与ModeMap生成相同的属性名称。
package com.apress.springrecipes.court.
import org.springframework.web.servlet.ModelAndV
import org.springframework.web.servlet.mvc.AbstractC
public class ReservationQueryController extends AbstractController{
public ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response)throws Exception{
List&Reservation& reservations =
if(courtName != null){
reservations = reservationService.query(courtName);
return new ModelAndView("reservationQuery","courtName",courtName)
.addObject("reservations",reservations);
事实上,返回的模型和视图都是可选的。在有些情况下,你只返回视图,模型中没有任何属性。或者只返回模型,让Spring MVC根据请求URL来决定视图。有时候,如果让控制器直接处理HttpServletResponse对象,甚至可以返回null,例如在将二进制文件返回给用户的时候。
浏览 55505
浏览: 718401 次
来自: 杭州
不错,谢谢!
讲得非常好
我这样做的时候传中文怎么是乱码那?主题信息(必填)
主题描述(最多限制在50个字符)
申请人信息(必填)
申请信息已提交审核,请注意查收邮件,我们会尽快给您反馈。
如有疑问,请联系
傻丫头和高科技产物小心翼翼的初恋
CSDN &《程序员》编辑/记者,投稿&纠错等事宜请致邮
个人大数据技术博客:
人生得意须尽欢,莫使金樽空对月。

我要回帖

更多关于 浏览器下载安装 的文章

 

随机推荐