- 静态资源的映射
- Springmvc完成ajax功能
- SpringMVC返回中文到ajax乱码问题解决方式
1.静态资源映射
对于之前web.xml配置文件中的
先做出如下更改,不可写“/*”因为有区别
“/”代表所有请求都需要经过此拦截器(之前学习中的list.do也可以写成list即可),但我们发现即使设置了此拦截,通过后却依然不能显示图片等等,即.jsp/.png等后缀的静态资源文件
解决此问题的方法需要在spring配置文件中加入有着 释放静态资源 作用的代码 <mvc:default-servlet-handler/>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> 9 10 <!-- 1.包扫描:扫描注解所在的包,controller类所在的包 --> 11 <context:component-scan base-package="com.zhiyou100.zhl.controller"></context:component-scan> 12 13 <!-- 2.开启注解驱动AnnotationHandlerMapping --> 14 <mvc:annotation-driven/> 15 16 <!-- 释放静态资源 --> 17 <mvc:default-servlet-handler/> 18 19 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 20 <property name="prefix" value="/WEB-INF/views/"></property> 21 <property name="suffix" value=".jsp"></property> 22 </bean> 23 24 </beans>
释放完成后
2.Springmvc完成ajax功能
此部分需要三个jackson的jar包,目的是把java对象转化为json对象,所以需要jackson的jar包
[点击下载https://github.com/suyirulan/putao/tree/master/jackson_jar]
导包完成后即可编写
@Controller @RequestMapping("ajax") /*@RequestMapping(value="ajax",produces = {"text/html;charset=utf-8"})*/ public class AjaxController { @RequestMapping("Ajax1") @ResponseBody //把"hello"作为内容返回给调用函数,而不会成为hellow.jsp,把中文"俺啊倒萨打算爱"返回给调用函数 public String ajax1(String name) { System.out.println(name); return "俺啊倒萨打算爱"; } @RequestMapping("Ajax2") @ResponseBody //把java对象转化为json对象,需要jackson的jar包 public Users ajax2(String name) { Users users=new Users("dsf","fdsg",32,"sdf","sdf"); return users; } @RequestMapping("Ajax3") @ResponseBody public List<Users> ajax3(String name) { Users users=new Users("阿三打撒ds1f","fdsg",32,"sdf","sdf"); Users users2=new Users("撒旦ds2f","fdsg",32,"sdf","sdf"); Users users3=new Users("ds3f","fdsg",32,"sdf","sdf"); Users users4=new Users("撒旦ds4f","fdsg",32,"sdf","sdf"); List<Users> list=new ArrayList<Users>(); list.add(users); list.add(users2); list.add(users3); list.add(users4); return list; } }
jsp页面进行接收即可
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="/SpringMVC09_03/js/jquery-3.2.1.min.js"></script> <script type="text/javascript"> $.post("ajax/Ajax2",{"name":"zs"},function(data){ alert(data.uname); alert(data.password); alert(data.age); alert(data.sex); alert(data.address); }); $.post("ajax/Ajax1",{"name":"zs"},function(data){ alert(data); }); $.post("ajax/Ajax3",{"name":"zs"},function(data){ $.each(data,function(i,n){ alert(n.uname); } ) }); </script> </head> <body> </body> </html>
Tips:SpringMVC返回中文到ajax乱码问题解决方式
1.编写注解@RequestMapping(value="ajax",produces = {"text/html;charset=utf-8"})
@RequestMapping(value="ajax",produces = {"text/html;charset=utf-8"})
2.修改配置文件,在驱动中加入代码如下
参考步骤(通过org.springframework.web.servlet.view.InternalResourceViewResolver的有参构造函数进行设置编码):
原文地址:https://www.cnblogs.com/murmansk/p/11456374.html
时间: 2024-10-11 05:23:48