配置文件:
<!-- spring mvc ajax 配置SpringMVC @ResponseBody和@RequestBody注解 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter" /> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <!-- 配置支持返回的数据类型 --> <bean id="stringHttpMessageConverter" class="com.anysoft.common.util.converter.UTF8StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> <value>application/xml;charset=UTF-8</value> </list> </property> </bean> <!-- 配置支持提交的数据类型 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- spring mvc ajax 配置SpringMVC @ResponseBody和@RequestBody注解 -->
UTF8StringHttpMessageConverter代码:
package com.framework.common.util.spring.converter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.AbstractHttpMessageConverter; import org.springframework.util.FileCopyUtils; /** * 解决@responseBody乱码问题 * UTF8StringHttpMessageConverter * @author dongxh * */ public class UTF8StringHttpMessageConverter extends AbstractHttpMessageConverter<String> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private final Charset defaultCharset; private final List<Charset> availableCharsets; private boolean writeAcceptCharset = true; /** * A default constructor that uses {@code "ISO-8859-1"} as the default charset. * @see #StringHttpMessageConverter(Charset) */ public UTF8StringHttpMessageConverter() { this(DEFAULT_CHARSET); } /** * A constructor accepting a default charset to use if the requested content * type does not specify one. */ public UTF8StringHttpMessageConverter(Charset defaultCharset) { super(new MediaType("text", "plain", defaultCharset), MediaType.ALL); this.defaultCharset = defaultCharset; this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values()); } /** * Indicates whether the {@code Accept-Charset} should be written to any outgoing request. * <p>Default is {@code true}. */ public void setWriteAcceptCharset(boolean writeAcceptCharset) { this.writeAcceptCharset = writeAcceptCharset; } @Override public boolean supports(Class<?> clazz) { return String.class.equals(clazz); } @Override protected String readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException { Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType()); return FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset)); } @Override protected Long getContentLength(String s, MediaType contentType) { Charset charset = getContentTypeCharset(contentType); try { return (long) s.getBytes(charset.name()).length; } catch (UnsupportedEncodingException ex) { // should not occur throw new IllegalStateException(ex); } } @Override protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException { if (this.writeAcceptCharset) { outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets()); } Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType()); FileCopyUtils.copy(s, new OutputStreamWriter(outputMessage.getBody(), charset)); } /** * Return the list of supported {@link Charset}. * <p>By default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses. * @return the list of accepted charsets */ protected List<Charset> getAcceptedCharsets() { return this.availableCharsets; } private Charset getContentTypeCharset(MediaType contentType) { if (contentType != null && contentType.getCharSet() != null) { return contentType.getCharSet(); } else { return this.defaultCharset; } } }
action代码:
//ajax操作 @RequestMapping("/ajax") @ResponseBody public Map<String, Object> ajaxWebsite(@RequestBody CmsWebsite website){ System.out.println("==================****************================:"+website.getSiteUrl()); Map<String, Object> modelMap = new HashMap<String, Object>(); modelMap.put("d", "d"); return modelMap; } //ajax操作 @RequestMapping("/ajax1") @ResponseBody public CmsWebsite ajaxWebsite1(@RequestBody CmsWebsite website){ System.out.println("==================****************================:"+website.getSiteUrl()); Map<String, Object> modelMap = new HashMap<String, Object>(); modelMap.put("d", "d董晓黑"); return website; } //ajax操作 @RequestMapping("/ajax2") @ResponseBody public Map<String, Object> ajaxWebsite2(@RequestBody CmsWebsite website){ System.out.println("==================****************================:"+website.getSiteUrl()); Map<String, Object> modelMap1 = new HashMap<String, Object>(); List<CmsWebsite> l = new ArrayList<CmsWebsite>(); CmsWebsite c = new CmsWebsite(); c.setSiteUrl("1111111111111"); c.setSiteIcp("2222222222222"); l.add(c); CmsWebsite c1 = new CmsWebsite(); c1.setSiteUrl("333333333333"); c1.setSiteIcp("4444444444444"); l.add(c1); modelMap1.put("data",l); modelMap1.put("zs","200"); return modelMap1; } //ajax操作 @RequestMapping("/ajax3") @ResponseBody public String ajaxWebsite4(@RequestBody CmsWebsite website){ System.out.println("==================****************================:"+website.getSiteUrl()); return "chenggong"; }
相关推荐
基于spring mvc注解开发的ajax项目实例,通过对注解@ResponseBody的应用,实现ajax局部刷新功能,java代码端返回Map,Integer,User,List等对象,在jsp页面通过$.each(data,function(index,entity) { });...
NULL 博文链接:https://xkorey.iteye.com/blog/726146
Spring + Hibernate + Spring mvc +Ajax 整合 框架,一个简单的添加删除。
十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八、spring mvc 多视图控制器 十九、 <mvc:annotation-driven /> 到底做了什么工作 二十、...
十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八、spring mvc 多视图控制器 十九、 <mvc:annotation-driven /> 到底做了什么工作
Ajax With Spring 3. Eclipse 工程,包含Web所需要的 所有jar包。 1> ajax 请求。 2> spring 3注解使用 3> mvc:annotation
文件上传demo:前台ajax,后台spring mvc,效率好于传统表单上传。
Spring MVC与Ajax交互源码,详细解读参考 http://blog.csdn.net/quincylk/article/details/51423297
01. 采用后台及前台的 Spring + Spring mvc + Hibernate + Bootstrap 02. 后台全注解式的开发(除了必要的spring和hibernate的xml配置以外) 03. 后台通过自定义注解结合一个访问拦截器实现整个系统的权限控制 04...
SpringMVC+Ajax异步文件上传+短视频背景+a标签绑定文件域
NULL 博文链接:https://1197581932.iteye.com/blog/2330457
十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八、spring mvc 多视图控制器 十九、 <mvc:annotation-driven /> 到底做了什么工作 二十...
spring使用ajax的方法 还有mvc的上传下载
一个实用了spring mvc和ajax异步请求的例子
Table of Contents Configuring a Spring Development Environment Spring ...AJAX Resolving and Implementing Views Testing Spring MVC Applications Spring Web Flow Building Applications with Spring Web Flow ...
Spring MVC 3实例,包含上传下载,还有Spring mvc jsr303表单验证技术,还有一个spring mvc ajax json等 欢迎下载 自己研究,简单易懂 如果有注解不懂,可以看看...
ajax导出xml调用浏览器下载功能; ajax导入文件说明及注意事项
三期答辩课程设计,人事管理系统Mybatis+Spring+Spring MVC框架整合AJAX+JQuery实现登录、注册以及验证码和验证注册用户信息,包括数据库备份,课设说明,答辩PPT,Windows下sts(eclipse) 。
spring example 用ajax来做得spring 添加账号功能,在spring官网上有
bootstrap,ajax,spring mvc,spring,hibernate 项目实战源码