首页 > 学院 > 开发设计 > 正文

maven+spring mvc环境搭建xml版(无web.xml,maven jetty插件运行)

2019-11-06 07:17:10
字体:
来源:转载
供稿:网友

环境:SPRing Framework 4.3.7.RELEASEServlet 3.1.0JDK 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	 */	@Override	protected WebApplicationContext createServletApplicationContext() {		XmlWebApplicationContext ctx = new XmlWebApplicationContext();		ctx.setConfigLocation("classpath:applicationContext.xml");		return ctx;	}	/**	 * 设置DispatcherServlet的拦截路径	 */	@Override	protected String[] getServletMappings() {		return new String[]{"/"};	}	@Override	protected 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.xsd	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd	http://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  访问controllerhttp://127.0.0.1:9090/static/list.html  访问静态资源


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表