一、导入相jar包

主要包括spring相关jar包和fastjson jar包,具体步骤略。

二、配置相关文件

1.配置web.xml文件

<?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>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring MVC配置文件的位置和名称 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 表示容器在启动时立即加载dispatcherServlet -->
<load-on-startup>1</load-on-startup>
</servlet> <!-- 让Spring MVC控制器拦截前端所有请求 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

2.配置Spring MVC配置文件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>

三、编写请求控制类

通过注解配置映射请求的URL和返回内容类型等。

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

注:如果访问html页面报错404,可参考文章:spring mvc访问html页面404报错解决

最新文章

  1. RMAN-03002, RMAN-06059, ORA-19625 and ORA-27037 When Running RMAN Backup of Archivelogs
  2. zookeeper 应用
  3. [winserver]设置Server2008R2远程桌面允许每个用户运行多个会话
  4. Hibernate的关系配置
  5. vyos (一) 基础配置
  6. POj3268 Silver Cow Party
  7. 宁波Uber优步司机奖励政策(1月25日~1月31日)
  8. [Android]BaseExpandableListAdapter实现可折叠列表
  9. Nodejs的运行原理-科普篇
  10. 【Android】定位与解决anr错误记录
  11. route路由的顺序问题了数据包的转发流程
  12. 苹果绿RGB值
  13. 潭州课堂25班:Ph201805201 并发 第九课 (课堂笔记)
  14. 洛谷P4198 楼房重建 单调栈+线段树
  15. mysql将某数据库的全部权限赋给某用户,提示1044错误Access denied
  16. netty ChannelOption
  17. NOI 2015 Bless All!
  18. CakePHP调用model类和foreach循环
  19. Eclipse - JAR包制作
  20. DRF(4) - 认证、权限组件

热门文章

  1. Android Thermal HAL 降龙十八掌
  2. java meil
  3. 菜鸡的Java笔记 笔记
  4. ubuntu更換清華軟件源
  5. [JS高程]JavaScript中的RegExp对象
  6. 从零开始学Kotlin第七课
  7. Python之99乘法表代码
  8. FJD1T1
  9. Codeforces 891E - Lust(生成函数)
  10. python-django-查询详解