自定義視圖
阿新 • • 發佈:2018-03-02
title enc UC javax -m ren 視圖 使用 註意點
一:結構
1.新建一個包
這個包用來放自定義的view
二:程序
1.新建一個view類
註意要寫@Component
1 package com.spring.view; 2 3 import java.util.Date; 4 import java.util.Map; 5 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 import org.springframework.stereotype.Component;10 import org.springframework.web.servlet.View; 11 12 @Component 13 public class HelloView implements View{ 14 15 @Override 16 public String getContentType() { 17 return "text/html"; 18 } 19 20 @Override 21 public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throwsException { 22 response.setHeader("Content-type", "text/html;charset=UTF-8"); 23 response.setCharacterEncoding("UTF-8"); 24 response.getWriter().print("hello view,time "+new Date()); 25 } 26 }
2.註冊視圖解析器BeanNameViewResolver
InternalResourceViewResolver解析器是最後一個解析器,如果有其他的會先執行其他的,因為order越小,優先級越高。
註意點:重新配置掃描包,這個掃描包是view的掃描包,不然view不會被加載到。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.0.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 12 <!-- 配置自定義掃描的包 --> 13 <context:component-scan base-package="com.spring.it" ></context:component-scan> 14 15 <!-- 配置視圖解析器 --> 16 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 17 <property name="prefix" value="/WEB-INF/views/" /> 18 <property name="suffix" value=".jsp" /> 19 </bean> 20 21 <!-- 讓spring掃描你註解的包 --> 22 <context:component-scan base-package="com.spring.view"></context:component-scan> 23 <!-- 配置視圖BeanNameViewResolver解析器 --> 24 <!-- 使用視圖的名字進行解析 --> 25 <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> 26 <property name="order" value="100"></property> 27 </bean> 28 29 30 31 32 33 <!-- 配置國際化資源文件 --> 34 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 35 <property name="basename" value="i18n"></property> 36 </bean> 37 38 <!-- 配置直接轉發的頁面 --> 39 <!-- 可以直接相應轉發的頁面, 而無需再經過 Handler 的方法. --> 40 <mvc:view-controller path="/success" view-name="success"/> 41 42 <!-- 在實際開發中通常都需配置 mvc:annotation-driven 標簽 --> 43 <mvc:annotation-driven></mvc:annotation-driven> 44 45 </beans>
3.index
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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> <a href="BeanNameTest">Test BeanNameTest</a> <br><br> </body> </html>
4.concroller
1 package com.spring.it; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @Controller 7 public class BeanNameTest { 8 @RequestMapping("/BeanNameTest") 9 public String hello() { 10 System.out.println("ha ha"); 11 //剛寫的view,但是第一個字母小寫 12 return "helloView"; 13 } 14 }
5.效果、
自定義視圖