4.1.2 问题分析
团队多人开发,每人设置不同的请求路径,冲突问题该如何解决?
解决思路:为不同模块设置模块名作为请求路径前置
对于Book模块的save,将其访问路径设置
http://localhost/book/save
对于User模块的save,将其访问路径设置
http://localhost/user/save
这样在同一个模块中出现命名冲突的情况就比较少了。
将pom.xml中多余的内容删除掉,再添加SpringMVC需要的依赖
- "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.0modelVersion>
-
- <groupId>com.itheimagroupId>
- <artifactId>SpringMVCartifactId>
- <version>1.0-SNAPSHOTversion>
- <packaging>warpackaging>
-
- <name>SpringMVC 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>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.11version>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>3.1.0version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>5.2.1.RELEASEversion>
- dependency>
- dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.tomcat.mavengroupId>
- <artifactId>tomcat7-maven-pluginartifactId>
- <version>2.1version>
- <configuration>
- <port>80port>
- <path>/path>
- configuration>
- plugin>
- plugins>
- build>
-
- project>
- package com.itheima.config;
-
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration
- @ComponentScan("com.itheima.controller")
- public class SpringmvcConfig {
- }
- package com.itheima.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- //@Controller:Springmvc的bean标志
- @Controller
- public class UserController {
- @RequestMapping("/save")
- @ResponseBody
- public String save(){
- System.out.println("郭浩康第一个Sprintmvc项目创建成功");
- return "{spring1save()...}";
- }
- }
-
-
- @Controller
- public class BookController {
-
- @RequestMapping("/save")
- @ResponseBody
- public String save(){
- System.out.println("book save ...");
- return "{'module':'book save'}";
- }
- }
将web.xml删除,换成ServletContainersInitConfig
- package com.itheima.config;
-
- import org.springframework.web.context.WebApplicationContext;
- import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
- import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
-
- public class ServletConfig extends AbstractDispatcherServletInitializer {
- //加载Springmvc容器
- @Override
- protected WebApplicationContext createServletApplicationContext() {
- AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
- ctx.register(SpringmvcConfig.class);
- return ctx;
- }
- //设置请求处理
- @Override
- protected String[] getServletMappings() {
- return new String[]{"/"};
- }
- //加载Spring容器
- @Override
- protected WebApplicationContext createRootApplicationContext() {
- return null;
- }
- }