Ajax实现数据回显

文章目录

      • 1、在pom.xml中导入依赖
      • 2、在web.xml中配置文件
      • 3、在springmvc中配置文件
      • 4、实体类user
      • 5、在Controller类中编写代码,展示到前端页面
      • 6、jsp前端页面
      • 7、显示效果

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>
 
    <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>

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>

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>

4、实体类user

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {

    private int id;
    private String name;
    private String sex;
}

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;
   }
}

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>

7、显示效果

Ajax实现数据回显_第1张图片.

Ajax实现数据回显_第2张图片.

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

你可能感兴趣的:(Ajax,ajax,spring,java)