对于定义的消息转换器 必须通过 mvc:annotation-driven进行注册 消息转换器
会对请求的mini类型进行匹配 如果无法匹配 不会进行消息转换
配置springMvc.xml:
<mvc:annotation-driven>
<mvc:message-converters>
<bean id="myConvetor" class="cn.et.springmvc.lesson02.message.MyMessageConvertor">
<property name="supportedMediaTypes">
<list>
<!-- 设置响应支持的类型 -->
<value>text/html;charset=UTF-8</value>
<!-- 设置请求body支持的类型 -->
<value>application/x-www-form-urlencoded</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
定义一个消息转换器处理类,该类必须继承AbstractHttpMessageConverter<?>
消息转换器处理类:
package cn.et.springmvc.lesson02.message;
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;
public class MyMessageConvertor extends AbstractHttpMessageConverter<Phone> {
/**
* 将请求头数据转换成Phone
*/
@Override
protected Phone readInternal(Class<? extends Phone> arg0,
HttpInputMessage msg) throws IOException,
HttpMessageNotReadableException {
InputStream is = msg.getBody();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String param = br.readLine();
Phone phone = new Phone();
param = param.split("=")[1];
phone.setQno(param.split("-")[0]);
phone.setNumber(param.split("-")[1]);
return phone;
}
/**
* 当前的转换器支持转换的类
*/
@Override
protected boolean supports(Class<?> arg0) {
if (arg0 == Phone.class) {
return true;
}
return false;
}
/**
* 用于将返回对象转换成字符串显示网页上 该方法可以实现Action的返回值不一定为String
*/
@Override
protected void writeInternal(Phone phone, HttpOutputMessage arg1)
throws IOException, HttpMessageNotWritableException {
String p = phone.getQno() + "-" + phone.getNumber();
arg1.getBody().write(p.getBytes("utf-8"));
}
}
Action类的需要用到@RequestBody和@ResponseBody注解关联到MyMessageConvertor
Action类:
package cn.et.springmvc.lesson02.message;
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;
/**
* 消息转换器 将请求头的数据转换成 action方法的参数 将方法的返回值的内容转换成响应头
*
* @author Administrator
*
*/
@Controller
public class MessageAction {
@RequestMapping(value = "/user")
public @ResponseBody
Phone user(@RequestBody Phone phone) {
System.out.println(phone.getQno() + phone.getNumber());
return phone;
}
}
编写jsp文件跳转的动作只能是form表单,并且添加属性enctype="application/x-www-form-urlencoded"
jsp入口:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'index.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="${public String _elExpressionpageContext.getRequest().getContextPath()}/user" method="post"
enctype="application/x-www-form-urlencoded">
<input type="hidden" name="phone" value="0755-123456789" />
<input type="submit" />
</form>
</body>
</html>