• Ajax实现数据回显


    1、在pom.xml中导入依赖

    <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>3.8.1version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>5.3.22version>
            dependency>
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>servlet-apiartifactId>
                <version>2.5version>
            dependency>
            <dependency>
                <groupId>javax.servlet.jspgroupId>
                <artifactId>jsp-apiartifactId>
                <version>2.2version>
            dependency>
            <dependency>
                <groupId>jstlgroupId>
                <artifactId>jstlartifactId>
                <version>1.2version>
            dependency>
    
     		<dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>1.18.24version>
            dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-databindartifactId>
                <version>2.13.3version>
            dependency>
    
    • 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
     
        <build>
            <resources>
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>falsefiltering>
                resource>
                <resource>
                    <directory>src/main/resourcesdirectory>
                    <includes>
                        <include>**/*.propertiesinclude>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>falsefiltering>
                resource>
            resources>
        build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    2、在web.xml中配置文件

    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    
        
        <servlet>
            <servlet-name>springmvcservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:springmvc-servlet.xmlparam-value>
            init-param>
            
            <load-on-startup>1load-on-startup>
        servlet>
        <servlet-mapping>
            <servlet-name>springmvcservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    
        
        <filter>
            <filter-name>encodingfilter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
            <init-param>
                <param-name>encodingparam-name>
                <param-value>utf-8param-value>
            init-param>
        filter>
        <filter-mapping>
            <filter-name>encodingfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
    web-app>
    
    • 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

    3、在springmvc中配置文件

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           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.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
    ">
        
        <context:component-scan base-package="com.jin.controller"/>
        
        
        <mvc:default-servlet-handler/>
        
        
         <mvc:annotation-driven>
            
            <mvc:message-converters register-defaults="true">
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <constructor-arg value="UTF-8"/>
                bean>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="objectMapper">
                        <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                            <property name="failOnEmptyBeans" value="false"/>
                        bean>
                    property>
                bean>
            mvc:message-converters>
        mvc:annotation-driven>
    
    beans>
    
    • 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

    4、实体类user

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class User {
    
        private int id;
        private String name;
        private String sex;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    5、在Controller类中编写代码,展示到前端页面

    @RestController
    public class AjaxController {
    
       @RequestMapping("/a1")
       public List<User> test() {
    
          ArrayList<User> users = new ArrayList<>();
          users.add(new User(1, "酷小亚", "男"));
          users.add(new User(2, "张三", "男"));
          users.add(new User(3, "李四", "男"));
          users.add(new User(4, "王五", "男"));
          return users;
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    6、jsp前端页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>酷小亚title>
        <script src="https://code.jquery.com/jquery-3.1.1.js">script>
    head>
    
    <script>
        $(function(){
            $("#btn").click(function(){
                $.post("${pageContext.request.contextPath}/a1",function (data){
                    // console.log(data);
                    var html="";
    
                    for (let i = 0; i < data.length; i++) {
                        html+=""+
                            ""+data[i].id+""+
                            ""+data[i].name+""+
                            ""+data[i].sex+""+
                            ""
                    }
                    $("#content").html(html);
                })
            })
        });
    script>
    
    <body>
    <input type="button" value="加载数据" id="btn">
    <table>
        <tr>
            <td>姓名td>
            <td>年龄td>
            <td>性别td>
        tr>
        <tbody id="content">
        <%--数据:后台--%>
        tbody>
    table>
    body>
    html>
    
    
    • 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

    7、显示效果

    在这里插入图片描述.

    在这里插入图片描述.

    恭喜你,成功实现了数据回显!!

  • 相关阅读:
    Flutter笔记:拖拽手势
    c#动态保留小数位数的数值格式化方法实例----从小数点后非零数字保留两位进行四舍五入
    统一身份认证登录详细介绍-identity4
    Leetcode1-两数之和
    【C++】AVL树(平衡搜索二叉树)
    LNK2001 __GSHandlerCheck【error】
    [开题报告]flask框架行走的历史文博课(python+程序+论文)
    【MATLAB教程案例52】SVM支持向量机学习——使用matlab实现基于SVM的数据二分类
    Word格式处理控件Aspose.Words for .NET教程——更新和删除字段
    【LeetCode练习】19. 删除链表的倒数第 N 个结点(中等|JS|快慢指针)
  • 原文地址:https://blog.csdn.net/weixin_45737330/article/details/126792323