**
********** ————吾亦无他,唯手熟尔,谦卑若愚,好学若饥————-************
处理器的方法我们之前做过,返回值为String的,返回值为ModelAndView的,我们这个讲的这个返回值void的做什么的?
答:最主要的是ajax的时候用的
案例如下:
俩个类:实体类UserInfo和处理器类:
UserInfo类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 1package cn.dawn.day12return;
2
3/**
4 * Created by Dawn on 2018/3/26.
5 */
6public class UserInfo {
7 private String username;
8 private String password;
9
10 public UserInfo() {
11 }
12
13 public UserInfo(String username, String password) {
14 this.username = username;
15 this.password = password;
16 }
17
18 public String getUsername() {
19 return username;
20 }
21
22 public void setUsername(String username) {
23 this.username = username;
24 }
25
26 public String getPassword() {
27 return password;
28 }
29
30 public void setPassword(String password) {
31 this.password = password;
32 }
33}
34
自定义的处理器类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 1package cn.dawn.day12return;
2
3import com.alibaba.fastjson.JSON;
4import org.springframework.stereotype.Controller;
5import org.springframework.web.bind.annotation.RequestMapping;
6
7import javax.servlet.http.HttpServletResponse;
8import java.io.IOException;
9import java.util.ArrayList;
10import java.util.List;
11
12/**
13 * Created by Dawn on 2018/3/26.
14 */
15@Controller
16public class MyReturnDemoController {
17 @RequestMapping("/doreturnVoidAjax")
18 public void doreturnVoidAjax(HttpServletResponse response) throws IOException {
19 List<UserInfo> lists=new ArrayList<UserInfo>();
20 UserInfo u1=new UserInfo("M","123");
21 UserInfo u2=new UserInfo("6","123");
22
23 lists.add(u1);
24 lists.add(u2);
25
26 String json = JSON.toJSONString(lists);
27 response.getWriter().write(json);
28 }
29}
30
此处用到了阿里的fastjson的jar包,自已引一个即可,不说这个jar了
自己的xml配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 1package cn.dawn.day12return;
2
3import com.alibaba.fastjson.JSON;
4import org.springframework.stereotype.Controller;
5import org.springframework.web.bind.annotation.RequestMapping;
6
7import javax.servlet.http.HttpServletResponse;
8import java.io.IOException;
9import java.util.ArrayList;
10import java.util.List;
11
12/**
13 * Created by Dawn on 2018/3/26.
14 */
15@Controller
16public class MyReturnDemoController {
17 @RequestMapping("/doreturnVoidAjax")
18 public void doreturnVoidAjax(HttpServletResponse response) throws IOException {
19 List<UserInfo> lists=new ArrayList<UserInfo>();
20 UserInfo u1=new UserInfo("M","123");
21 UserInfo u2=new UserInfo("6","123");
22
23 lists.add(u1);
24 lists.add(u2);
25
26 String json = JSON.toJSONString(lists);
27 response.getWriter().write(json);
28 }
29}
30
在web.xml中把中央调度器调度到自己当前写的这个xml配置文件,再解决乱码,再解决静态资源js不能引用的问题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 1<!DOCTYPE web-app PUBLIC
2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3 "http://java.sun.com/dtd/web-app_2_3.dtd" >
4
5<web-app>
6 <display-name>Archetype Created Web Application</display-name>
7
8 <!--过滤器处理乱码-->
9 <filter>
10 <filter-name>CharacterEncoding</filter-name>
11 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
12 <!--请求-->
13 <init-param>
14 <param-name>encoding</param-name>
15 <param-value>utf-8</param-value>
16 </init-param>
17 <!--响应-->
18 <init-param>
19 <param-name>forceEncoding</param-name>
20 <param-value>true</param-value>
21 </init-param>
22 </filter>
23
24 <filter-mapping>
25 <filter-name>CharacterEncoding</filter-name>
26 <url-pattern>/*</url-pattern>
27 </filter-mapping>
28
29
30
31
32
33 <!--中央调度器-->
34 <servlet>
35 <servlet-name>springmvc</servlet-name>
36 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
37 <init-param>
38 <param-name>contextConfigLocation</param-name>
39 <param-value>classpath:ApplicationContext-day12return.xml</param-value>
40 </init-param>
41 <load-on-startup>1</load-on-startup>
42 </servlet>
43 <servlet-mapping>
44 <servlet-name>springmvc</servlet-name>
45 <url-pattern>/</url-pattern>
46 </servlet-mapping>
47
48
49 <!--第一种方式--><!--解决静态资源不能引用-->
50 <servlet-mapping>
51 <servlet-name>default</servlet-name>
52 <url-pattern>*.js</url-pattern>
53 </servlet-mapping>
54 <servlet-mapping>
55 <servlet-name>default</servlet-name>
56 <url-pattern>*.css</url-pattern>
57 </servlet-mapping>
58 <servlet-mapping>
59 <servlet-name>default</servlet-name>
60 <url-pattern>*.jpg</url-pattern>
61 </servlet-mapping>
62 <servlet-mapping>
63 <servlet-name>default</servlet-name>
64 <url-pattern>*.png</url-pattern>
65 </servlet-mapping>
66 <servlet-mapping>
67 <servlet-name>default</servlet-name>
68 <url-pattern>*.gif</url-pattern>
69 </servlet-mapping>
70
71</web-app>
72
jsp页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 1<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %>
2<html>
3<head>
4 <title>Ajax</title>
5 <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.min.js"></script>
6 <script type="text/javascript">
7
8 $(function () {
9
10 $("input").on("click",function () {
11 $.ajax({
12 url:"${pageContext.request.contextPath}/doreturnVoidAjax",
13 type:"POST",
14 data:{},
15 success:function (data) { //data:String json串 data:Object json对象
16
17 //用户名
18 $.each(eval("("+data+")"),function (i,dom) {
19 alert(dom.username);
20 });
21 }
22 });
23 });
24 });
25
26 </script>
27</head>
28
29<body>
30<%--<img src="image/1.jpg">--%>
31<input type="button" value="想发送Ajax?,点我不要赔的喔!!!呵呵哒~~~~~~"/>
32</body>
33</html>
34
案例完毕