接上篇http://blog.csdn.net/javastudyr/article/details/52585770
這篇開始來配置springmvc,並測試
思路:
- 在web.xml配置servlet
- 創建springmvc配置文件
- 創建TestController
- 創建測試頁面
- 基本測試
- 亂碼測試
- 時間測試
(1)web.xml配置springmvc
<!-- springmvc servlet配置 -->
<servlet>
<servlet-name>front</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>front</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
(2)創建spring-mvc.xml 文件
springmvc 配置掃描,自動裝配,JSP解析
<context:component-scan base-package="com.study" use-default-filters="false">
<!--掃描 cn.itcast下的 所有類中 註解是 Controller的類 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--自動裝配 -->
<context:annotation-config/>
<!-- Jsp的視圖解析器 -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
(3)創建TestController
package com.study.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.study.bean.TestTb;
Controller
public class TestController {
@RequestMapping("/test/springmvc/testTbAdd")
public String test_testtb(TestTb tb){
System.out.println(tb.toString());
return "success";
}
}
(4)創建測試頁面和success頁面,src/main/webapp/下創建index.jsp ,注意修改編碼
<%@ 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>
<form action="test/springmvc/testTbAdd.do" method="POST">
名稱:<input type="text" name="name"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
在webapp/WEB-INF/view/下創建success.jsp
<%@ 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>
添加成功!
</body>
</html>
(5)開始基本測試
訪問http://localhost:8080/boot-study-ssmm-xml/
基本測試結果:
(6)在TestController中添加方法,開始中文測試
@RequestMapping(value="/test/unicode",produces = "plain/html; charset=UTF-8")
public @ResponseBody String test_unicode(){
return "成功";
}
訪問:
http://localhost:8080/boot-study-ssmm-xml/test/unicode.do
中文測試返回結果:
解決辦法
1. 在web.xml 中添加字符攔截器,注意加載到所有servlet,filter的前面
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
中文測試繼續測試
這是爲什麼喃,我們加載了字符攔截器,好像還是不行,但是在訪問http://localhost:8080/boot-study-ssmm-xml/ 這個接口的時候輸入中文,後臺是可以接收了
繼續解決我們直接返回中文字符串的問題,這裏是因爲@ResponseBody 註解,這個註解返回的String,springmvc會直接用org.springframework.http.converter.StringHttpMessageConverter 來進行轉發,這個默然的編碼是:ISO-8859-1
明白了,現在我們可以在springmvc中配置這個StringHttpMessageConverter 的編碼
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="messageConverters">
<list>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
中文測試繼續測試2
結果:
(7)時間測試
index.jsp修改如下
<%@ 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>
<form action="test/springmvc/testTbAdd.do" method="POST">
名稱:<input type="text" name="name"/><br/>
生日:<input type="text" name="birthday"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
時間測試:
400,錯誤,估計就是時間類型不匹配,我們在TestController中綁定一個時間解析來試試
/**
* 註冊日期格式
* @param request
* @param binder
*/
@InitBinder
public void InitBinder(HttpServletRequest request ,ServletRequestDataBinder binder){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
}
時間測試1
OK,到此所有的都大功告成
補充一下:時間轉換器也可以全區配置,在springmvc中,配置如下:
<!-- 全局時間配置 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<!-- 自定義事件處理器 -->
<bean class= "com.study.util.BindindInitializer" />
</property>
</bean>
package com.study.util;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
public class BindindInitializer implements WebBindingInitializer{
public void initBinder(WebDataBinder binder, WebRequest request) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
}
}
下篇就正式進入springboot的開發了