消息 轉換器
將請求頭的數據轉換爲 action對應方法的參數
將方法返回值內容轉換成響應頭
必須使用 post方式提交
簡單的消息轉換例子
將一個 消息 如 0755-5451212 經過 處理 轉換成對應的參數
1:首先要 自定義消息轉換器
MyMessageConventor..java 繼承 AbstractHttpMessageConverter
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
/**
* 1:首先 繼承 AbstractHttpMessageConverter <>
* <> 泛型裏面放的是轉換消息對應的對象。
*
* @author Administrator
*
*/
public class MyMessageTransition extends AbstractHttpMessageConverter<PhoneNumber> {
/**
* 將請求消息 進行邏輯處理且傳入
*/
@Override
protected PhoneNumber readInternal(Class<? extends PhoneNumber> arg0,
HttpInputMessage msg) throws IOException,
HttpMessageNotReadableException {
//獲取 消息所在的請求正文
InputStream is= msg.getBody();
//讀取 消息
BufferedReader br=new BufferedReader(new InputStreamReader(is));
//截取需要的消息
String message=br.readLine().split("=")[1];
PhoneNumber phone = new PhoneNumber();
phone.setPhone(message.toString().split("-")[0]);
phone.setNumber(message.toString().split("-")[1]);
return phone;
}
/**
* 第一個進入的方法
* 是否支持當前對象的消息轉換
*/
@Override
protected boolean supports(Class<?> arg0) {
if(arg0==PhoneNumber.class){
//true 表示 支持 跳入readInternal 方法
return true;
}
return false;
}
/**
* 最後進入的方法
* 將返回的對象轉換成字符串顯示在網頁上
*/
@Override
protected void writeInternal(PhoneNumber phone, HttpOutputMessage msg)
throws IOException, HttpMessageNotWritableException {
//將對象屬性變爲需要的字符串
String phones=phone.getPhone()+"--"+phone.getNumber();
msg.getBody().write(phones.getBytes("utf-8"));
}
}
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
">
<context:component-scan base-package="cn"></context:component-scan>
<mvc:annotation-driven>
<mvc:message-converters>
<bean id="transition" class="cn.zj.mvc.lesson02.MyMessageTransition">
<!-- 在父類裏面有這個屬性 ,可以直接引入 -->
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8</value>
<!-- application 可以在任意 form 表單裏面 enctype 屬性默認找到 -->
<value>application/x-www-form-urlencoded</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
web.xml文件如下
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>utf</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>utf</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 加載 springmvc 文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
</beans>
3.action 文件
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MessageTransition {
/**
* 消息 轉換器
* 將請求頭的數據轉換爲 action對應方法的參數
* 將方法返回值內容轉換成響應頭
* 必須使用 post方式提交
*
* 要將消息轉換成對象用 @RequestBody
* 將對象返回給響應頭用 @ResponseBody 可以放在方法上面 也可以放在 類型前面
*
*
* 運行流程
* 1: 消息 通過 from post 方法傳入 到 一個action
* 如果 繼承了 AbstractHttpMessageConverter 類 且在 spring.xml文件中配置
*
* 2:進入supports 方法 返回true進入3
*
* 3:進入 readInternal方法
*
* 4:進入 action方法
*
* 5:進入 writeInternal方法
*
*
*/
@RequestMapping("/message")
public @ResponseBody PhoneNumber messageTran(@RequestBody PhoneNumber phone){
System.out.println(phone.getPhone()+"_"+phone.getNumber());
return phone;
}
}
4.jsp 界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'message.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/message.action" method="post">
<input type="hidden" name="phone" value="0731-0258444"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>