java

springmvc-ajax

rzk · 5月1日 · 2020年本文共2360个字 · 预计阅读8分钟108次已读

先配置web.xml

 1 "1.0" encoding="UTF-8"?>
 2 "http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns_xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi_schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5          version="4.0">
 6     
 7         DispatcherServlet
 8         class>org.springframework.web.servlet.DispatcherServletclass>
 9         
10             contextConfigLocation
11             classpath:applicationContext.xml
12         
13         1
14     
15 
16     
17         DispatcherServlet
18         /
19     
20 
21 
22     
23     
24         encoding
25         class>org.springframework.web.filter.CharacterEncodingFilterclass>
26         
27             encoding
28             UTF-8
29         
30     
31     
32         encoding
33         /*
34     
35 

m睿共享aven依赖

这里选择spring的4.2.2可以选择5.2

 1 "1.0" encoding="UTF-8"?>
 2 "http://maven.apache.org/POM/4.0.0"
 3          xmlns_xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi_schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     
 6         SpringMVC
 7         com.rzk
 8         1.0-SNAPSHOT
 9     
10     4.0.0
11 
12     sppringmvc-06-ajax01
13     
14     
15         
16             junit
17             junit
18             4.12
19         
20         
21             org.springframework
22             spring-webmvc
23             4.2.2.RELEASE
24         
25         
26             javax.servlet.jsp
27             jsp-api
28             2.2
29         
30         
31             javax.servlet
32             jstl
33             1.2
34         
35         
36             javax.servlet
37             javax.servlet-api
38             4.0.1
39         
40         
41             org.projectlombok
42             lombok
43             1.18.12
44         
45     
46     资源过滤
47     
48         
49             
50                 src/main/resources
51                 
52                     **/*.properties
53                     **/*.xml
54                 
55                 false
56             
57             
58                 src/main/java
59                 
60                     **/*.properties
61                     **/*.xml
62                 
63                 false
64             
65         
66 
67     
68 
69 

applicationContext.xml配置文件

 1 "1.0" encoding="UTF-8"?>
 2 "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/beans
 7        http://www.springframework.org/schema/beans/spring-beans.xsd
 8        http://www.springframework.org/schema/context
 9        http://www.springframework.org/schema/context/spring-context.xsd
10        http://www.springframework.org/schema/mvc
11        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
12     
13     base-package="com.rzk.controller"/>
14     
15     
16     
17     default-servlet-handler/>
18     
19     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20         "prefix" value="/"/>
21         "suffix" value=".jsp"/>
22     
23 

接下来新建controller包

 1 @RestController
 2 public class AjaxController {
 3 
 4     @RequestMapping("/a3")
 5     public String 睿共享test3(String name,String pwd){
 6         String msg = null;
 7         if (name !=null){
 8             if ("admin".equals(name)){
 9                 msg = "OK";
10             }else{
11                 msg ="No";
12             }
13         }
14         if (pwd !=null){
15             if ("admin".equals(pwd)){
16                 msg = "OK";
17             }else{
18                 msg ="No";
19             }
20         }
21         return msg;
22     }
23 
24 }

login.jsp

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 
 3 
 4     Title
 5     
 6     
33 
34 
35 
36 账号:"text" id="name" name="name" onblur="a1()">
37 "userInfo">
38 密码:"text" id="pwd" name="pwd" onblur="a2()">
39 "pwdInfo">
40 
41 

springmvc-ajax

下面使用ajax获取后端数据显示到前台

新建实体类,导入lombok

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String name;
    private int age;
    private String sex;
}

controller

 1 @RestController
 2 public class AjaxController {
 3  @RequestMapping("/a2")
 4     public List test2(){
 5      睿共享   ArrayList list = new ArrayList<>();
 6         list.add(new User("李四",1,""));
 7         list.add(new User("李五",2,""));
 8         list.add(new User("李六",3,""));
 9         return list;
10     }
11 }

前端ajax页面

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 
 3 
 4     Title
 5     
 6     
25 
26 
27 "button" value="加载数据" id="a">
28     293031323334"content">
353637
姓名 年龄 性别
38 39

springmvc-ajax

0 条回应