使用maven中tomcat插件运行web应用程序
是这样的,在我学习springmvc的时候,第一次按照教程使用maven中的tomcat插件运行web应用程序,然后就遇到下面类似的问题:
他这里一直在run,搞得很难受(一直认为时运行失败导致的),经过查询https://blog.csdn.net/Spy10086/article/details/111189001
后才发现他就是这样(运行成功的样子)。
点击蓝色的 http://localhost:80/即可进入。
补充:
第一次使用SpringMvc(及其方便)步骤总结:
(1)导入springmvc坐标和servlet坐标:
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.1</version> </dependency>
(2)创建springmvc的控制器(相当于servlet)
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class UserController { @RequestMapping("/save") @ResponseBody public String save(){ System.out.println("user was saved ..."); return "{'module':'springmvc'}"; } }
(3)创建SpringMvcConfig配置类,在spring环境中加载控制器对应的bean
package com.rsh.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan("com.rsh.controller") public class SpringMvcConfig { }
package com.rsh.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class UserController { @RequestMapping("/save") @ResponseBody public String save(){ System.out.println("user was saved ..."); return "{'module':'springmvc'}"; } }
(4)初始化servlet容器,加载springmvc的环境,设置springmvc的处理请求
package com.rsh.config; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer; public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer { protected WebApplicationContext createServletApplicationContext() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(SpringMvcConfig.class); return ctx; } protected String[] getServletMappings() { return new String[]{"/"}; } protected WebApplicationContext createRootApplicationContext() { return null; } }
(5)配置maven的运行环境。
在Pom.xml中加载tomcat插件
<plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <port>80</port> <path>/</path> </configuration> </plugin> </plugins>
配置maven
运行即可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构