maven+spring mvc环境搭建xml版(无web.xml,maven jetty插件运行)
来源:互联网 发布:java分布式开发是什么 编辑:程序博客网 时间:2024/06/05 06:47
环境:
Spring Framework 4.3.7.RELEASE
Servlet 3.1.0
JDK 1.8
这里的xml版,是指spring的配置使用xml
创建maven webapp项目:maven-spring-webmvc-xml 项目结构如下:
各文件代码如下:
pom.xml
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.pp</groupId><artifactId>maven-spring-webmvc-xml</artifactId><packaging>war</packaging><version>1.0.0</version><name>maven-spring-webmvc-xml</name><url>http://maven.apache.org</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>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>4.3.7.RELEASE</version></dependency></dependencies><build><plugins><!-- jetty的maven插件,配置这个插件之后,就可以直接在项目的跟目录执行mvn jetty:run来运行项目。 而无需把项目打成war扔到web容器中 --><plugin><groupId>org.eclipse.jetty</groupId><artifactId>jetty-maven-plugin</artifactId><version>9.4.2.v20170220</version><configuration><stopKey>foo</stopKey><stopPort>9999</stopPort><httpConnector><!-- jetty端口 --><port>9090</port></httpConnector><webApp><contextPath>/</contextPath></webApp></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.0.0</version><configuration><!-- 因为本例子没有用到web.xml,也没有创建web.xml,所以这里必须配置这个插件。 否则使用maven打包的时候会有问题 --><failOnMissingWebXml>false</failOnMissingWebXml></configuration></plugin></plugins></build></project>
package com.pp.web;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.XmlWebApplicationContext;import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;/** * 系统初始化入口 */public class AppDispatcherServletInitializer extends AbstractDispatcherServletInitializer {/** * 默认的spring文件为:[servlet-name]-servlet.xml * 我这里指定为:applicationContext.xml */@Overrideprotected WebApplicationContext createServletApplicationContext() {XmlWebApplicationContext ctx = new XmlWebApplicationContext();ctx.setConfigLocation("classpath:applicationContext.xml");return ctx;}/** * 设置DispatcherServlet的拦截路径 */@Overrideprotected String[] getServletMappings() {return new String[]{"/"};}@Overrideprotected WebApplicationContext createRootApplicationContext() {return null;}}
package com.pp.web.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class IndexController {@GetMapping("/index")public String index(){return "hello index";}}
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- controller等mvc的注解生效 --> <mvc:annotation-driven /> <!-- 静态资源由WEB服务器默认的Servlet来处理 --> <mvc:default-servlet-handler /> <!-- spring component组件扫描包 --><context:component-scan base-package="com.pp.web" /></beans>
最后,在项目的跟目录执行mvn clean jetty:run 运行项目
访问
http://127.0.0.1:9090/index 访问controller
http://127.0.0.1:9090/static/list.html 访问静态资源
2 0
- maven+spring mvc环境搭建xml版(无web.xml,maven jetty插件运行)
- maven+spring mvc环境搭建注解版(无web.xml,maven jetty插件运行)
- maven 插件 运行 jetty
- Spring MVC +Mybatis + Maven 配置之web.xml配置
- maven pom.xml jetty 配置
- maven jetty pom.xml配置
- 如何在maven的pom.xml中配置jetty插件?
- 如何通过Maven的Jetty插件运行Web工程
- 如何通过Maven的Jetty插件运行Web工程
- SpringMvc+Maven+Jetty 环境搭建
- eclipse创建简单maven+spring项目(无xml)
- Spring MVC Maven 环境搭建与部署
- Intellij IDEA + Spring MVC + Maven 环境搭建
- maven+ibatis+spring mvc环境搭建
- maven搭建spring mvc web项目
- maven tomcat插件 配置web.xml
- Maven搭建Spring+Struts2+Mybatis项目(二) web.xml设置
- spring4、hibernate4、spring mvc搭建环境(maven)
- Hibernate中,mappedBy和注解@JoinColumn的对比
- 卷积神经网络(CNN)前向传播算法
- __elv_add_request
- MySQL数据库数据操作
- 机器学习之问题描述——聚类Clustering
- maven+spring mvc环境搭建xml版(无web.xml,maven jetty插件运行)
- 《谍影重重5》中的黑科技
- Twitter的分布式自增ID算法snowflake (Java版)
- MyCat分表分库规则实现
- casperjs使用说明-使用命令行
- Serverless架构的演进
- Map内部类Entry
- Codeforces 364 A Matrix 题解(矩形构造)
- 学习笔记之----Adapter的封装(RecyclerView)