Eclipse部署SpringMVC+Maven项目
无奈,我之前的工作项目,让我接触过SpringBoot+maven,还有SpringMVC无maven,这两种我都还记得套路。然而SpringMVC+Maven,这个组合,着实让我就疼了几天。而这一切,可能归结于我的基础甚差。
下面,我将图贴出来,以防后患。
这是我项目路径,是包含有前端静态资源的:
下面就是我的部署相关配置:
Windows,Preference,Java,Installed JREs,也就是下图的配置点,虽然说是配的JRE,但还是选择JDK那个文件夹。
右击项目名,Properties,Java Build Path,Source和Libraries需要注意配置:
下面就是我认为我最没注意的一点:Properties,Deployment Assembly:
如果没有那个Maven依赖,部署到Tomcat时,就导不出项目依赖的jar包。
下面贴出我的pom文件:
<?xml version="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.0</modelVersion>
<groupId>com.yuxx.springmvc</groupId>
<artifactId>spittr</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!--使用utf-8编码-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--web-->
<spring.version>4.3.14.RELEASE</spring.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 注意tomcat7此处的url -->
<url>http://localhost:8080/manager/text</url>
<server>tomcat7</server> <!-- 此处的名字必须和setting.xml中配置的ID一致-->
<path>/spittr</path> <!-- 此处的名字是项目发布的工程名-->
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!--<scope>test</scope>-->
</dependency>
<!--<dependency>-->
<!--<groupId>log4j</groupId>-->
<!--<artifactId>log4j</artifactId>-->
<!--<version>1.2.17</version>-->
<!--</dependency>-->
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!--<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!--<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>-->
<!--
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</dependency>
<!--system-rules-->
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.16.0</version>
<!--<scope>test</scope>-->
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Embedded Database-->
<!--<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>-->
</dependencies>
</project>
如此,就可以像普通的web项目一样,添加到Servers(Tomcat)中,正常启动了。
——————————————————————————————————————————————————————
再补充个Junit单元测试Demo:
package com.yuxx.spittr.web;import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.yuxx.spittr.config.RootConfig;
import com.yuxx.spittr.config.WebConfig;
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {RootConfig.class,WebConfig.class})
public class HomeControllerTest {
@Autowired
private HomeController homeController;
@Test
public void homeTest() {
assertEquals("home", homeController.home());
}
}
对于SpringMVC的Junit4单元测试,@WebAppConfiguration这个注解非常重要。没有这个注解会报错,提示无法加载上下文。
以上是 Eclipse部署SpringMVC+Maven项目 的全部内容, 来源链接: utcz.com/z/513846.html