SpringMvc配置文件代码

对于定义的消息转换器 必须通过 mvc:annotation-driven进行注册 消息转换器 
会对请求的mini类型进行匹配 如果无法匹配 不会进行消息转换
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"><!-- 配置扫描 --><context:component-scan base-package="cn.et"></context:component-scan><!--配置自己定义的消息转换器 --><mvc:annotation-driven><mvc:message-converters><bean id="myMessageConventor" class="cn.et.day20170601.MyMessageConventor"><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>
</beans>

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

action代码

public class MyMessageConventor extends AbstractHttpMessageConverter<Phone>{/***解析请求的参数*/@Overrideprotected Phone readInternal(Class arg0, HttpInputMessage httpInputMessage)throws IOException, HttpMessageNotReadableException {InputStream is=httpInputMessage.getBody();BufferedReader br=new BufferedReader(new InputStreamReader(is));String str=br.readLine();String string=str.split("=")[1];Phone phone=new Phone();phone.setCode(string.split("-")[0]);phone.setNumber(string.split("-")[1]);return phone;}/*** 如果支持 true支持  会调用 readInternal 将http消息 转换成方法中被@RequestBody注解的参数*         会调用writeInternal 将被@ResponseBody注解的返回对象转换成数据字节响应给浏览器*/@Overrideprotected boolean supports(Class arg0) {if(arg0==Phone.class){return true;}return false;}/*** 响应给对象的参数* 将方法被@ResponseBody注解的返回对象转换成数据字节响应给浏览器*/@Overrideprotected void writeInternal(Phone phone, HttpOutputMessage httpOutputMessage)throws IOException, HttpMessageNotWritableException {OutputStream os=httpOutputMessage.getBody();String str=phone.getCode()+"-"+phone.getNumber();os.write(str.getBytes());}}

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

@Controller
@RequestMapping(value="/day0601")
public class MyMessageAction {@RequestMapping(value="/myMessage.action")//告诉他响应的内容@ResponseBody//用@RequestBody这个接收自定义的请求体消息public Phone myMessage(@RequestBody Phone phone){System.out.println(phone.getCode()+"-"+phone.getNumber());return phone;}
}

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

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 'phone.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}/day0601/myMessage.action" method="post" enctype="application/x-www-form-urlencoded"><input type="hidden" name="phone" value="0755-123456"/><input type="submit" value="提交"/></form></body>
</html>

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"><!-- spring自带的解决乱码的过滤器 --><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><filter><!-- 配置这个selevlet来加载sprinmvc的配置文件 --><filter-name>hidden</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter>    <filter-mapping><filter-name>hidden</filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 通过这个参数去找配置文件 不加这个参数默认在 /WEB-INF/找spservlet-name-servlet.xml这个文件--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:/springmvc.xml</param-value></init-param><!-- 启动tomcat的时候就加载 --><load-on-startup>0</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><!-- /拦截所有以.action --><url-pattern>*.action</url-pattern><!-- /拦截所有servlet --><url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>

SpringMvc自定义消息转换器相关推荐

  1. SpringMVC消息转换器

    SpringMVC自定义消息转换器 1. 目的 本篇主要是解决Long类型数据长度大于17位在传输到前段JS中精度丢失问题 2. 原因分析 2.1 数据库 通过下图可以看到数据库中姓名为:test用户 ...

  2. SpringBoot___自定义消息转换器、MVC配置

    2019独角兽企业重金招聘Python工程师标准>>> 1. 自动配置的消息转换器   在SptingBoot的源码中的spring-boot-autoconfig的Jar包下,我们 ...

  3. spring boot处理请求返回值的格式(自定义消息转换器)

    springboot 将对象转化成json对象返回给前端,是通过多个消息转换器配合完成的 但是有些时候,默认的转化格式未必符合我们的要求,这个时候就需要进行自定义消息转换器 只需要在@Configur ...

  4. springMVC消息转换器HttpMessageConverter

    前言 为何需要消息转换器 HttpMessageConverter是用来处理request和response里的数据的. 请求和响应都有对应的body,而这个body就是需要关注的主要数据. 请求体的 ...

  5. spring 自定义消息转换器

    消息转换器,顾名思义就是对返回的消息,进行转换.下面常见的例子如下: Spring MVC框架中,将HTTP请求信息转换为一个对象(@RequestBody注解),将对象输出为HTTP响应信息(@Re ...

  6. SpringBoot添加自定义消息转换器

    首先我们需要明白一个概念:springboot中很多配置都是使用了条件注解进行判断一个配置或者引入的类是否在容器中存在,如果存在会如何,如果不存在会如何. 也就是说,有些配置会在springboot中 ...

  7. springboot自定义消息转换器HttpMessageConverter

    在SpringMVC中,可以使用@RequestBody和@ResponseBody两个注解,分别完成请求报文到对象和对象到响应报文的转换,底层这种灵活的消息转换机制就是利用HttpMessageCo ...

  8. SpringMVC自定义配置消息转换器踩坑总结

    问题描述 最近在开发时候碰到一个问题,springmvc页面向后台传数据的时候,通常我是这样处理的,在前台把数据打成一个json,在后台接口中使用@requestbody定义一个对象来接收,但是这次数 ...

  9. 深入学习SpringMVC以及学习总结

    2019独角兽企业重金招聘Python工程师标准>>> 一.优点: 1.SpringMVC简化web程序开发; 2.SpringMVC效率很好(单例模式): 3.SpringMVC提 ...

最新文章

  1. 坑系列 --- 高可用架构的银弹
  2. eclipse的安装和用VS进行单元测试
  3. 玩转Autorun.inf
  4. Java异常处理和设计
  5. python中的np where_python – np.where在我的熊猫中不起作用
  6. 停车管理系统汽车到达汽车离去c语言,停车场管理系统 C语言实现
  7. java 6 基础教程_Java小白入门教程(6)——循环语句
  8. Flex布局新旧混合写法详解
  9. mysql 海量数据库的查询优化及分页算法方案_mysql 海量数据库的查询优化及分页算法方案...
  10. Java基础-反射机制
  11. oracle grid安装看不到config,【图片】【rac11g安装问题】出了问题特来请教各位大侠【oracle吧】_百度贴吧...
  12. 关于利用np.contour画出logistic模型决策边界(plot_decision_regions)的一点感想
  13. (转)在Winform程序中设置管理员权限及为用户组添加写入权限
  14. ddm模型公式_cfa讲义-估值中的折现方法-DDM模型(2)
  15. python充分理解def语句
  16. 计算程序中flag是什么意思,python中flag什么意思
  17. 转 - DataGuard中如何配置LOG_ARCHIVE_DEST_n参数
  18. 【3D建模】Solidworks 3D建模及PrusaSlicer切片打印学习笔记
  19. NLP微信小程序聊天机器人
  20. picpick文字竖排了怎么变成横排

热门文章

  1. 统计cassandra单表数据量
  2. 开源Zip文件压缩算法 ICSharpCode.SharpZLib
  3. 网摘:一位网友《塑造阳光心态》的学习心得
  4. 2023年最有前景的行业
  5. 写一个自己的前端手脚架(1)
  6. tushare使用教程(附代码)
  7. 手把手教你pfx证书转pem
  8. 华为mate40RS能升级鸿蒙,华为mate40rs快速开箱
  9. NVIDIA CloudXR 和 Autodesk VRED 已在 AWS 上线
  10. NAS网络存储是什么