视图解析器
JSTLView视图
若项目中使用了JSTL,则SpringMVC会自动把视图由InternalResourceView转为JSTLView视图
若使用JSTL的fmt标签则需要在SpringMVC的配置文件中配置国际化资源文件
i18n.username=UserName i18n.password=Password i18n.username=用户名 i18n.password=密码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 配置自定义扫描包 --> <context:component-scan base-package="com.hous.springmvc.controller" /> <!-- 配置视图解析器:把handler方法返回值解析到实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置国际化资源 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="i18n"></property> </bean> </beans>
页面设置(记住,这个页面一定要是经过SpringMVC转换过的页面,即JSTLView解析过的页面)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>Hello shanshanbox.com</h1> <h2>time: ${requestScope.time}</h2> <h2>names: ${requestScope.names}</h2> <h2>request user: ${requestScope.user}</h2> <h2>session user: ${sessionScope.user}</h2> <h2>request web: ${requestScope.web}</h2> <h2>session web: ${sessionScope.web}</h2> <fmt:message key="i18n.username" /> <fmt:message key="i18n.password" /> <fmt:formatNumber value="12" type="percent"/><br/> </body> </html>
自定义视图
package com.hous.springmvc.view; import java.util.Date; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; import org.springframework.web.servlet.View; @Component public class HelloView implements View { @Override public String getContentType() { return "text/html"; } @Override public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception { response.getWriter().print("Hello World, now time is: " + new Date()); } }
<!-- 配置BeanNameViewResolver视图解析器,使用视图的名字解析视图 --> <!-- 通过order属性定义视图解析器的优先级,order值越小优先级越高 --> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="100"></property> </bean>
重定向
一般情况下,控制器方法返回的字符串类型的值会被当成逻辑视图名处理
如果返回的字符串中带有forward:或redirect:前缀时,SpringMVC会对他们进行特殊处理:
将forward:和redirect:当成指示符,其后的字符串作为URL来处理
-redirect:success.jsp:会完成一个到success.jsp的重定向操作
-forward:success.jsp:会完成一个到success.jsp的转发操作
package com.hous.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class JSTLController { @RequestMapping("/testRedirect") public String testRedirect() { System.out.println("testRedirect"); return "redirect:/index.jsp"; } }