SpringMVC 环境搭建:
1、创建 webapp 的模型的 Maven 项目
2、在 Maven 文件中导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
3、在 web.xml 文件中配置前置处理器(DispatcherServlet),并且配置 springmvc.xml 文件
<servlet>
<!-- servlet-name 名随便取 -->
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- init-param 加载 springmvc 配置文件 -->
<init-param>
<!-- param-name 名称固定 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<!-- servlet-mapping 前置控制器,处理路径 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置后将不会拦截 js 文件 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
4、创建 springmvc.xml 配置文件, 配置视图解析器(ViewResolver),配置包扫描,消息转换器…
<?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: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-3.2.xsd">
<mvc:annotation-driven>
<!-- 消息转换器 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"></property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<context:component-scan base-package="com.cl"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
5、编写业务接口
@Controller
public class HelloHandler {
@RequestMapping(value = "/index", method = RequestMethod.GET, params = {"name","id"})
public String index(@RequestParam("name") String name,@RequestParam("id") int id){
System.out.println("执行了 index 方法...");
System.out.println("name: "+name+" id: "+id);
return "index";
}
// restful风格请求
@RequestMapping(value = "/restful/{name}/{id}",method = RequestMethod.GET)
public String indexRest(@PathVariable("name") String name,@PathVariable("id") int id){
System.out.println("执行了 indexRest 方法...");
System.out.println("name: "+name+" id: "+id);
return "index";
}
@RequestMapping("/json")
public User json(@RequestBody User user, HttpServletResponse response){
response.setCharacterEncoding("text/json;charset=UTF-8");
System.out.println(user);
user.setId(6L);
user.setName("张六");
return user;
}
}
6、浏览器访问接口测试