Springmvc3.2注解 基本教程
2015-05-18 00:00
381 查看
摘要: Springmvc3.2注解 基本教程
导入准备好的jar
下面会放出地址
在web.xml配置文件下面写这些内容
在使用springmvc3.2的json的时候 会出现乱码 这边小小的解决一下
查看一下springmvc的源码
然后定义一下UTF8StringHttpMessageConverter这个类 继承 StringHttpMessageConverter这个类
然后在springmvc-servlet.xml配置文件 下面增加
导入准备好的jar
下面会放出地址
在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"> <context-param> <param-name>contextConfigLocatioin</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config> <!-- 编码转换器 --> <filter> <filter-name>setcharacter</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>setcharacter</filter-name> <url-pattern>*.htm</url-pattern> </filter-mapping> </web-app>
在使用springmvc3.2的json的时候 会出现乱码 这边小小的解决一下
查看一下springmvc的源码
然后定义一下UTF8StringHttpMessageConverter这个类 继承 StringHttpMessageConverter这个类
package me.note.spring.framework; import java.io.IOException; import java.io.OutputStreamWriter; import java.nio.charset.Charset; import java.util.Arrays; import java.util.List; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.util.FileCopyUtils; public class UTF8StringHttpMessageConverter extends StringHttpMessageConverter { private static final MediaType utf8 = new MediaType("text", "plain", Charset.forName("UTF-8")); private boolean writeAcceptCharset = true; @Override protected MediaType getDefaultContentType(String dumy) { return utf8; } protected List<Charset> getAcceptedCharsets() { return Arrays.asList(utf8.getCharSet()); } protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException { if (this.writeAcceptCharset) { outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets()); } Charset charset = utf8.getCharSet(); FileCopyUtils.copy(s, new OutputStreamWriter(outputMessage.getBody(), charset)); } public boolean isWriteAcceptCharset() { return writeAcceptCharset; } public void setWriteAcceptCharset(boolean writeAcceptCharset) { this.writeAcceptCharset = writeAcceptCharset; } }
然后在springmvc-servlet.xml配置文件 下面增加
<?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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <!-- 配置包名下的配置文件 --> <context:component-scan base-package="me.note.pro"></context:component-scan> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean id="utf8StringHttpMessageConverter" class="me.note.spring.framework.UTF8StringHttpMessageConverter" /> <ref bean="mappingJacksonHttpMessageConverter"/> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </beans>
相关文章推荐
- (基本不使用这种)springMVC注解
- SpringMVC的四个基本注解annotation(控制层,业务层,持久层) -- @Component、@Repository @Service、@Controller
- SpringMVC中四个基本注解
- (三)springmvc之注解的基本使用
- SpringMvc——进行注解开发的基本配置
- Python基础教程代码与注释P44 3.1 基本字符串操作 3.2 字符串格式化:精简版
- SpringMVC中四个基本注解
- SpringMvc——进行注解开发的基本配置
- Spring学习笔记-springMVC基于注解的控制器(基本概念)
- SpringMVC中四个基本注解
- 最基本的springMVC注解配置详解
- SpringMVC的四个基本注解annotation
- Junit 实例精讲基础教程(一) 认识Junit基本注解@Before、@After、@Test、@BeforeClass、@AfterClass
- SpringMVC的四个基本注解annotation(控制层,业务层,持久层) -- @Component、@Repository @Service、@Controller
- 通过SpringMVC整合基于注解的JPA的实例教程代码下载
- 【转】Spring 注解学习手札(超好的springmvc注解教程)
- SpringMVC 基本注解[@Resource,@Autowired,@Component]
- springMVC基于注解配置教程一
- SpringMVC的四个基本注解annotation(控制层,业务层,持久层) -- @Component、@Repository @Service、@Controller
- SpringMVC教程(二)hibernate关联关系注解