Ajax asynchronous request for JSON format data implemented in SpringMVC environment

亚连
Release: 2018-05-24 10:27:33
Original
1405 people have browsed it

This article mainly introduces the relevant information of Ajax asynchronous request for JSON format data implemented in the SpringMVC environment. Friends who need it can refer to it

One environment construction

The first is the conventional spring mvc environment setup. Needless to say, it should be noted that the jackson related jar package needs to be introduced here, and then the json parsing related information is added to the spring configuration file "springmvc-servlet.xml" Configuration, my complete code here is as follows:

      text/html;charset=UTF-8 application/json;charset=UTF-8                               atom=application/atom+xml html=text/html json=application/json xml=application/xml *=*/*                
Copy after login

Project structure:

##Two test examples

(1) Create a new index.jsp file in the WEB-INF/jsp directory, which contains a simple jQuery ajax request. The format of the request data is JSON. The specific code is as follows:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %>           jQuery i18n  

Copy after login

(2) A simple model class User, the code is as follows:

package cn.zifangsky.controller; public class User { private String username; private int age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Copy after login

(3) Controller class TestController.java:

package cn.zifangsky.controller; import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.springframework.context.annotation.Scope; 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.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; @Controller @Scope("prototype") public class TestController { /** * 转到页面 */ @RequestMapping(value = "/hello.html") public ModelAndView list() { ModelAndView view = new ModelAndView("index"); return view; } /** * ajax异步请求, 请求格式是json */ @RequestMapping(value = "/hello.json", method = { RequestMethod.POST }) @ResponseBody public Map hello(@RequestBody User user) { // 返回数据的Map集合 Map result = new HashMap(); Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 返回请求的username result.put("username", user.getUsername()); // 返回年龄 result.put("age", String.valueOf(user.getAge())); // 返回当前时间 result.put("time", format.format(new Date())); return result; } }
Copy after login
Let me briefly talk about the specific execution steps:

i) After the project is started, visit: http://localhost:8089/SpringDemo/hello.html in the browser, then it will go to the list method in the controller, and then go to /WEB-INF/jsp /index.jsp (PS: The logical view returned in the controller is the real path of the file after being spliced with the path prefix and suffix defined in the springmvc-servlet.xml file)

ii) In index Enter text on the .jsp page and click the button, which will trigger an ajax request. This request will get the data in the input box and the default "age" parameter, splice it into a json format string, and finally submit it to the "hello.json" request, that is Execute the hello method in the controller

iii) After the hello method is executed, a series of data will be returned and finally displayed on the page

(4) The effect is as follows:

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

A brief discussion on ajax request technology

Ajax loading chrysanthemum loding effect

Very practical ajax user registration module

The above is the detailed content of Ajax asynchronous request for JSON format data implemented in SpringMVC environment. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!