SpringMVC项目请求(请求映射路径)
SpringMVC是web层的框架,主要的作用是接收请求、接收数据、响应结果,分成四部分内容:
- 请求映射路径
- 请求参数
- 日期类型参数传递
- 响应json数据
本次介绍的是设置请求映射路径。
1. 环境准备
-
创建一个Web的Maven项目
-
pom.xml添加Spring依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dcxuexi</groupId> <artifactId>springmvc_03_request_mapping</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>springmvc_03_request_mapping Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.15.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <port>80</port> <path>springmvc</path> </configuration> </plugin> </plugins> </build> </project>
-
创建对应的配置类SpringMvcConfig和ServletContainersInitConfig
@Configuration @ComponentScan("com.dcxuexi.controller") public class SpringMvcConfig { }
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[]{SpringMvcConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[0]; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } }
-
编写UserController和CityController
@Controller public class UserController { @RequestMapping("/select") @ResponseBody public String select(){ System.out.println("UserController select ,running ....."); return "{\"username\":\"xiaochuang\"}"; } @RequestMapping("/delete") @ResponseBody public String delele(){ System.out.println("UserController delele ,running ....."); return "user xiaochuang is deleted."; } }
@Controller public class CityController { @RequestMapping("/select") @ResponseBody public String select(){ System.out.println("CityController select ,running ....."); return "{\"cityName\":\"shanghai\"}"; } @RequestMapping("/delete") @ResponseBody public String delele(){ System.out.println("CityController delele ,running ....."); return "city shanghai is deleted."; } }
最终创建好的项目结构如下:
把环境准备好后,启动Tomcat服务器,后台会报错:
从错误信息可以看出:
- UserController有一个select方法,访问路径为
http://localhost/springmvc/select
- CityController也有一个select方法,访问路径为
http://localhost/springmvc/select
- 当访问
http://localhost/springmvc/select
的时候,到底是访问UserController还是CityController?
2. 问题分析
团队多人开发,每人设置不同的请求路径,冲突问题该如何解决?
解决思路:为不同模块设置模块名作为请求路径前置
对于UserController模块的select,将其访问路径设置http://localhost/springmvc/user/select
对于CityController模块的select,将其访问路径设置http://localhost/springmvc/city/select
这样在同一个模块中出现命名冲突的情况就比较少了。
3. 设置映射路径
步骤1:修改Controller
@Controller
public class UserController {
@RequestMapping("/user/select")
@ResponseBody
public String select(){
System.out.println("UserController select ,running .....");
return "{\"username\":\"xiaochuang\"}";
}
@RequestMapping("/user/delete")
@ResponseBody
public String delele(){
System.out.println("UserController delele ,running .....");
return "user xiaochuang is deleted.";
}
}
@Controller
public class CityController {
@RequestMapping("/city/select")
@ResponseBody
public String select(){
System.out.println("CityController select ,running .....");
return "{\"cityName\":\"shanghai\"}";
}
@RequestMapping("/city/delete")
@ResponseBody
public String delele(){
System.out.println("CityController delele ,running .....");
return "city shanghai is deleted.";
}
}
启动Tomcat服务器:
问题是解决了,但是每个方法前面都需要进行修改,写起来比较麻烦而且还有很多重复代码,如果/user后期发生变化,所有的方法都需要改,耦合度太高。
步骤2:优化路径配置
优化方案:
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/select")
@ResponseBody
public String select(){
System.out.println("UserController select ,running .....");
return "{\"username\":\"xiaochuang\"}";
}
@RequestMapping("/delete")
@ResponseBody
public String delele(){
System.out.println("UserController delele ,running .....");
return "user xiaochuang is deleted.";
}
}
@Controller
@RequestMapping("/city")
public class CityController {
@RequestMapping("/select")
@ResponseBody
public String select(){
System.out.println("CityController select ,running .....");
return "{\"cityName\":\"shanghai\"}";
}
@RequestMapping("/delete")
@ResponseBody
public String delele(){
System.out.println("CityController delele ,running .....");
return "city shanghai is deleted.";
}
}
注意:
- 当类上和方法上都添加了
@RequestMapping
注解,前端发送请求的时候,要和两个注解的value值相加匹配才能访问到。 - @RequestMapping注解value属性前面加不加
/
都可以
扩展小知识:
对于PostMan如何觉得字小不好看,可以使用ctrl+=
调大,ctrl+-
调小。
项目代码
-
gitee 代码下载
-
github 代码下载