页面数据类型为json,后端接受json数据

项目结构

页面数据类型为json,后端接受json数据_第1张图片

依赖pom.xml


      org.springframework
      spring-context
      5.2.8.RELEASE
    
    
      org.springframework
      spring-webmvc
      5.2.8.RELEASE
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.5
    
    
      junit
      junit
      4.12
      test
    

web.xml





  
    dispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:spring.xml
    
    1
  
  
    dispatcherServlet
    /
  


  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
  
  
    encodingFilter
    /*
  



addStudent.jsp

取得input 的输入值然后编写json数据,JSON.stringify(student) 将student 转化为json对象


<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title
    
    








Student实体类,get set toString方法

页面数据类型为json,后端接受json数据_第2张图片

StudentController代码

package com.tmg.controller;

import com.tmg.domain.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class StudentController {

    List students=new ArrayList<>();
    @ResponseBody
    @RequestMapping("testAjax")
    public List testAjax(@RequestBody Student student){
        students.add(student);
        return students;
    }

    @RequestMapping("hello")
    public String toIndex(){
        System.out.println("hello");
        return "addStudent";
    }
}

resources下spring.xml




    


    
        
        
    


    


    



你可能感兴趣的:(springmvc,java,json,json)