我们知道SpringMVC做的主要其实是表现层的,所以这里我们就要动web.xml文件了。其实动的也很简单
- 加上设定字符集的增强
- 配置核心DispatchServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<!--与普通的Servlet不同,这里我们使用的是DispatcherServlet-->
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--我们这里要告诉他如何初始化我们的DispatcherServlet,就是去读取spring-mvc.xml的文件,该文件确保了哪些类可以作为Bean被扫描到-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<!--“/”意味着就是任何路径都会触发这个Servlet-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
注意!这里配置核心DispatchServlet的时候,我们需要告诉他,他是根据什么配置来创建的,配置中包括比如哪些类(Bean)是需要被他管理的,所以有了这句<param-value>classpath*:spring-mvc.xml</param-value>
,因此我们就需要再创建一个spring-mvc.xml
的配置文件。配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--注意!这里我们只扫描controller包下的,所以我们这里可以去Spring的applicationContext文件里把他要扫描的包修改一下-->
<context:component-scan base-package="com.itheima.controller"/>
<!--开启springmvc注解驱动,对@ResponseBody的注解进行格式增强,追加其类型转换的功能,具体实现由MappingJackson2HttpMessageConverter进行-->
<mvc:annotation-driven/>
</beans>
我们修改一下Spring配置文件applicationContext.xml,让他不要扫描SpringMVC的Bean,也就是Controller。
<!--开启扫描组件,但是我们不扫描SpringMVC的Bean,所以设置Exclude File-->
<context:component-scan base-package="com.itheima">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--同时启动MVC的注解驱动-->
<mvc:annotation-driven/>
到这里SpringMVC就算配好了,我们来写一下Rest风格的Controller:
@RestController//这个相当于下面两个的结合体,这是Rest风格的
//@Controller
//@ResponseBody
@RequestMapping("/user")//相当于你这个Servlet的路径
public class UserController {
//http://localhost/user?userName=SunLL&password=6745&realName=Five&gender=1&birthday=1996/11/05
@PostMapping//这是提交(产生新的数据)
public boolean save(User user){
System.out.println("Save... "+user);
return true;
}
@PutMapping
public boolean update(User user){
System.out.println("Update... "+user);
return true;
}
@DeleteMapping("/{uuid}")
public boolean delete(@PathVariable Integer uuid){
System.out.println("Delete... "+uuid);
return true;
}
//因为我们知道Rest的话,你是不在URL里面显示属性名的,所以我们直接拿值
//而我们的参数名是需要与@GetMapping里面的对应的
@GetMapping("/{uuid}")
public User getUser(@PathVariable Integer uuid){
System.out.println("Get "+uuid);
return null;
}
//http://localhost/user/1/2
@GetMapping("/{page}/{size}")
public List getAll(@PathVariable Integer page,@PathVariable Integer size){
System.out.println("Get page "+page+" size "+size);
return null;
}
//http://localhost/user/login?userName=sunll&password=123
@PostMapping("/login")//和另一个post方法做一个区分
public User login(String userName, String password){
System.out.println(userName+" "+password);
return null;
}
}
基本上你看注释就明白了,首先我们先给整体配了个/user的路径,然后剩下的配参数,有的是基本数据类型有的则是引用类型,你参看注释就知道了。值得注意的就是save和login使用的都是@PostMapping,save好理解,login用post是因为为了确保账号密码安全。
如果使用PostMan发送请求都能有对应的输出的话,就代表测试通过,恭喜你SpringMVC表现层也搭建完成了!!!