【SpringMVC】视图和解析器
来源:互联网 发布:虚拟centos7网络配置 编辑:程序博客网 时间:2024/06/05 13:34
请求处理方法执行完成后,最终返回一个ModelAndView对象,对于返回String,View或ModelMap等类型的处理
方法,SpringMvc也会在内部将它们装配成一个ModelAndView对象的,它包含了逻辑名和模型对象的视图。
SpringMvc借助视图解析器(ViewResolver)得到最终的视图对象(View),最终的视图可能是JSP,也可能是
Excel等各种表现形式的视图。对于最终采取何种视图对象对模型数据进行渲染,处理器并不关心,处理器的工作重
点聚焦在生产模型数据的工作上,从而实现MVC的充分解耦。
视图(View)的作用是渲染模型数据,将模型数据里的数据以某种形式呈现给客户,视图对象由视图解析器负责
实例化,由于视图是无状态的,所以它们不会有线程安全的问题。
一、JstlView
导入包
新建国际化资源文件
springmvc.xml中配置
<!-- 配置国际化资源文件 -->
<beanid="messageSource"class="org.springframework.context.support.ResourceBundleMessageSource">
<propertyname="basename"value="i18n"></property>
</bean>
success.jsp
<fmt:messagekey="i18n.username"></fmt:message>
<fmt:messagekey="i18n.password"></fmt:message>
结果:
二、访问静态资源
在springmvc.xml中配置标签
<mvc:view-controller path="/success" view-name="success"/>
此时访问http://localhost:8080/springmvc-1/success可以直接转发到success.jsp页面
可是问题上来了,点击其他正常的请求,结果导致404
解决方案:
添加一个注解标签
<mvc:annotation-driven></mvc:annotation-driven>
三、自定义视图
在springmvc中配置视图解析器
两个视图解析器,使用时,根据优先级来决定优先使用
在InternalResourceViewResolver中,默认order为最大,所以优先级最低
jsp:
<ahref="springmvc/testView">Test View</a>
<br>
控制器:
@RequestMapping("/testView")
public String testView(){
System.out.println("testView");
return"helloView";
}
视图:
@Component
publicclass HelloViewimplements View{
@Override
public String getContentType() {
return"text/html";
}
@Override
publicvoid render(Map<String, ?>model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 在这里输出视图内容
response.getWriter().print("hello view,time:"+new Date());
}
}
四、关于重定向
一般,控制器方法返回字符串类型的值会被当成逻辑视图处理
如果返回的字符串中带有forward:或者redirect:前缀时,SpringMVC会对他们进行特殊处理:将forward:和redirect:当成指示符,其后的字符串作为URL来处理
jsp:
<a href="springmvc/testRedirect">Test Redirect</a>
控制器:
@RequestMapping("/testRedirect")
public String testRedirect(){
System.out.println("testRedirect");
return"redirect:/index.jsp";
}
结果:回到index.jsp
- 【SpringMVC】视图和解析器
- springmvc之视图和视图解析器
- SpringMVC中的视图和视图解析器
- SpringMVC中的视图和视图解析器
- springmvc之视图和视图解析器
- SpringMVC-5 视图和视图解析器
- SpringMVC的视图和视图解析器
- SpringMVC视图解析器
- SpringMVC视图解析器
- SpringMVC视图解析器
- SpringMVC视图解析器
- SpringMVC视图解析器
- SpringMVC视图解析器
- SpringMVC视图解析器
- SpringMVC视图解析器
- springMVC视图解析器
- SpringMVC视图解析器
- SpringMVC视图解析器
- Java设置session超时(失效)的三种方式
- android NDK的安装与配置(ubuntu)
- Perl的system后使用单引号和双引号以及其中包含变量的区别
- HDU 4888 Redraw Beautiful Drawings 最大流(唯一性)
- php在一个页面中使用另一个页面中的信息
- 【SpringMVC】视图和解析器
- 基于2-channel network的图片相似度判别
- Oracle基础教程-第四章 OEM与SQL*Plus
- 我所不知道的TCP Socket编程(六)-高级内容简介
- Servlet基础知识1
- SpringBoot 入门与配置
- 远程共享工具USB Network Gate v8.0.1828发布,支持Windows Server 2016
- table实时编辑函数封装
- linux内核队列实现