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

环境:
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
	 */
	@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;

@RestController
public 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  访问controller
http://127.0.0.1:9090/static/list.html  访问静态资源




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值