SpringMvc自定义消息转换器

SpringMvc配置文件代码

对于定义的消息转换器 必须通过 mvc:annotation-driven进行注册 消息转换器 
会对请求的mini类型进行匹配 如果无法匹配 不会进行消息转换

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans  
  4.     xmlns="http://www.springframework.org/schema/beans"  
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.     xmlns:p="http://www.springframework.org/schema/p"  
  7.     xmlns:context="http://www.springframework.org/schema/context"  
  8.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  9.       
  10.     xsi:schemaLocation="  
  11.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  12.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  13.     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd  
  14.     ">  
  15.     <!-- 配置扫描 -->  
  16.     <context:component-scan base-package="cn.et"></context:component-scan>  
  17.    <!--配置自己定义的消息转换器 -->  
  18.    <mvc:annotation-driven>  
  19.         <mvc:message-converters>  
  20.             <bean id="myMessageConventor" class="cn.et.day20170601.MyMessageConventor">  
  21.                 <property name="supportedMediaTypes">  
  22.                     <list>  
  23.                     <!-- 
  24.                      设置响应的支持的响应类型  
  25.                         -->  
  26.                         <value>text/html;charset=utf-8</value>  
  27.                         <!-- 设置请求body的支持类型-->  
  28.                         <value>application/x-www-form-urlencoded</value>  
  29.                     </list>  
  30.                 </property>  
  31.             </bean>  
  32.         </mvc:message-converters>  
  33.    </mvc:annotation-driven>  
  34. </beans>  


定义一个消息转换器处理类,该类必须继承AbstractHttpMessageConverter<?>

action代码

[java]  view plain  copy
  1. public class MyMessageConventor extends AbstractHttpMessageConverter<Phone>{  
  2.     /** 
  3.      *解析请求的参数 
  4.      */  
  5.     @Override  
  6.     protected Phone readInternal(Class arg0, HttpInputMessage httpInputMessage)  
  7.             throws IOException, HttpMessageNotReadableException {  
  8.          InputStream is=httpInputMessage.getBody();  
  9.          BufferedReader br=new BufferedReader(new InputStreamReader(is));  
  10.          String str=br.readLine();  
  11.          String string=str.split("=")[1];  
  12.          Phone phone=new Phone();  
  13.          phone.setCode(string.split("-")[0]);  
  14.          phone.setNumber(string.split("-")[1]);  
  15.         return phone;  
  16.     }  
  17.     /** 
  18.      * 如果支持 true支持  会调用 readInternal 将http消息 转换成方法中被@RequestBody注解的参数 
  19.      *         会调用writeInternal 将被@ResponseBody注解的返回对象转换成数据字节响应给浏览器 
  20.      */  
  21.     @Override  
  22.     protected boolean supports(Class arg0) {  
  23.         if(arg0==Phone.class){  
  24.             return true;  
  25.         }  
  26.         return false;  
  27.     }  
  28.     /** 
  29.      * 响应给对象的参数 
  30.      * 将方法被@ResponseBody注解的返回对象转换成数据字节响应给浏览器 
  31.      */  
  32.     @Override  
  33.     protected void writeInternal(Phone phone, HttpOutputMessage httpOutputMessage)  
  34.             throws IOException, HttpMessageNotWritableException {  
  35.             OutputStream os=httpOutputMessage.getBody();  
  36.             String str=phone.getCode()+"-"+phone.getNumber();  
  37.             os.write(str.getBytes());  
  38.     }  
  39.   
  40. }  


Action类的需要用到@RequestBody和@ResponseBody注解关联到MyMessageConvertor

[java]  view plain  copy
  1. @Controller  
  2. @RequestMapping(value="/day0601")  
  3. public class MyMessageAction {  
  4.     @RequestMapping(value="/myMessage.action")  
  5.     //告诉他响应的内容  
  6.     @ResponseBody  
  7.     //用@RequestBody这个接收自定义的请求体消息  
  8.     public Phone myMessage(@RequestBody Phone phone){  
  9.         System.out.println(phone.getCode()+"-"+phone.getNumber());  
  10.         return phone;  
  11.     }  
  12. }  


编写jsp文件跳转的动作只能是form表单,并且添加属性enctype="application/x-www-form-urlencoded" 必需要是post提交

jsp代码

[html]  view plain  copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'phone.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.      <form action="${pageContext.request.contextPath}/day0601/myMessage.action" method="post" enctype="application/x-www-form-urlencoded">  
  27.         <input type="hidden" name="phone" value="0755-123456"/>  
  28.         <input type="submit" value="提交"/>  
  29.     </form>  
  30.   </body>  
  31. </html>  


web.xml配置

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.     <!-- spring自带的解决乱码的过滤器 -->  
  8.         <filter>  
  9.         <filter-name>utf</filter-name>  
  10.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  11.         <init-param>  
  12.             <param-name>encoding</param-name>  
  13.             <param-value>UTF-8</param-value>  
  14.         </init-param>  
  15.     </filter>  
  16.     <filter-mapping>  
  17.         <filter-name>utf</filter-name>  
  18.         <url-pattern>/*</url-pattern>  
  19.     </filter-mapping>  
  20.     <filter>  
  21.     <!-- 配置这个selevlet来加载sprinmvc的配置文件 -->  
  22.     <filter-name>hidden</filter-name>  
  23.     <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
  24.   </filter>      
  25.   <filter-mapping>  
  26.     <filter-name>hidden</filter-name>  
  27.     <url-pattern>/*</url-pattern>  
  28.   </filter-mapping>  
  29.     <servlet>  
  30.         <servlet-name>springmvc</servlet-name>  
  31.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  32.         <!-- 通过这个参数去找配置文件 不加这个参数默认在 /WEB-INF/找spservlet-name-servlet.xml这个文件-->  
  33.         <init-param>  
  34.             <param-name>contextConfigLocation</param-name>  
  35.             <param-value>classpath:/springmvc.xml</param-value>  
  36.         </init-param>  
  37.         <!-- 启动tomcat的时候就加载 -->  
  38.         <load-on-startup>0</load-on-startup>  
  39.     </servlet>  
  40.     <servlet-mapping>  
  41.         <servlet-name>springmvc</servlet-name>  
  42.         <!-- /拦截所有以.action -->  
  43.         <url-pattern>*.action</url-pattern>  
  44.         <!-- /拦截所有servlet -->  
  45.         <url-pattern>/</url-pattern>  
  46.     </servlet-mapping>  
  47.   <welcome-file-list>  
  48.     <welcome-file>index.jsp</welcome-file>  
  49.   </welcome-file-list>  
  50. </web-app> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值