SpringMVC是web层的框架,主要的作用是接收请求、接收数据、响应结果,分成四部分内容:
本次介绍的是设置请求映射路径。
创建一个Web的Maven项目
pom.xml添加Spring依赖
<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.0modelVersion>
<groupId>com.dcxuexigroupId>
<artifactId>springmvc_03_request_mappingartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>springmvc_03_request_mapping Maven Webappname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.15.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.1version>
<configuration>
<port>80port>
<path>springmvcpath>
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服务器,后台会报错:
从错误信息可以看出:
http://localhost/springmvc/select
http://localhost/springmvc/select
http://localhost/springmvc/select
的时候,到底是访问UserController还是CityController?团队多人开发,每人设置不同的请求路径,冲突问题该如何解决?
解决思路:为不同模块设置模块名作为请求路径前置
对于UserController模块的select,将其访问路径设置http://localhost/springmvc/user/select
对于CityController模块的select,将其访问路径设置http://localhost/springmvc/city/select
这样在同一个模块中出现命名冲突的情况就比较少了。
@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后期发生变化,所有的方法都需要改,耦合度太高。
优化方案:
@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值相加匹配才能访问到。/
都可以扩展小知识:
对于PostMan如何觉得字小不好看,可以使用ctrl+=
调大,ctrl+-
调小。