An introduced phase jar package
Including the spring package and related FASTJSON jar jar package, specific steps are omitted.
Second, the configuration documents
1. Configure web.xml file
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SpringProject</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>dispatcherServlet</servlet-name> < The servlet-class > org.springframework.web.servlet.DispatcherServlet </ the servlet-class > <-! Configure Spring MVC configuration file location and name -> < the init-param > < param-name > contextConfigLocation </ param -name > < param-value > CLASSPATH: springmvc.xml </ param-value > </ the init-param > ! <- represents a container is loaded at startup immediately dispatcherServlet -> < load-oN-startup > . 1 </ Startup-ON-Load > </the servlet > <-! Let Spring MVC controller distal intercept all requests -> < the servlet-Mapping > < the servlet-name > DispatcherServlet </ the servlet-name > < URL-pattern > / </ URL-pattern > </ the servlet -mapping > </ Web-App >
2. Configure Spring MVC configuration file springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 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-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <context:component-scan base-package="com.springmvc.controller" /> <!-- 解决访问html资源404,非常重要 --> <mvc:default-servlet-handler/> <mvc:annotation-driven> </mvc:annotation-driven> <bean name="/hello2" class="com.springmvc.controller.HelloController2"></bean> </beans>
Third, the write request control class
By the configuration URL mapping annotation and return the requested content type.
package com.springmvc.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;import com.alibaba.fastjson.JSON;import java.util.HashMap; import java.util.Map; import org.springframework.stereotype.Controller; @Controller public class HelloController { @RequestMapping(value="/hello",produces = "application/json; charset=utf-8") @ResponseBody public String hello() { // TODO Auto-generated method stub Map<String, String> map = new HashMap<String, String>(); map.put("key1", "键值1"); System.out.println(JSON.toJSONString(map)); return JSON.toJSONString(map); } }
Note: If you access error 404 html page, you can refer to the article: the Spring MVC access html page 404 error solve