【发布时间】:2014-03-12 02:04:13
【问题描述】:
直截了当,我遇到的问题是,当我使用 Spring Boot 运行我的 Spring Web 应用程序时,我在访问 localhost:8080 时收到以下消息
2014-03-11 18:56:31.614 WARN 7640 --- [nio-8080-exec-3] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/WEB-INF/views/index.jsp] in DispatcherServlet with name 'dispatcherServlet'
我认为问题可能是我正在尝试使用 eclipse 运行我的 Maven 项目,但 webapp 文件夹和 Maven 依赖项似乎没有正确部署到目标文件夹。
我通过执行以下操作创建了项目:文件 > 新建 > Maven 项目 > maven-archetype-quickstart
创建时目录的外观如下:
src
main
java
site
site
App.java
test
java
site
site
AppTest.java
target
pom.xml
经过一些修改,更新pom.xml,运行命令
mvn eclipse:eclipse -Dwtpversion=2.0
我的项目结构如下:
src
main
java
net
site
config
Application.java
CoreConfig.java
MVCConfig.java
SecurityConfig.java
controller
IndexController.java
resources
webapp
WEB-INF
views
index.jsp
login.jsp
test
java
resources
target
m2e-wtp
web-resources
META-INF
MANIFEST.MF
maven
site
site
pom.properties
pom.xml
.classpath
.project
pom.xml
编译运行后得到的目标目录结构如下:
target
classes
net
site
config
Application.class
CoreConfig.class
MVCConfig.class
SecurityConfig.class
WebAppInitializer.class
controller
IndexController.class
m2e-wtp
web-resources
META-INF
MANIFEST.MF
maven
site
site
pom.properties
pom.xml
test-classes
也许我只是对如何在编译和运行应用程序时部署 webapp 和资源文件夹感到困惑,但根据我的 Web 部署程序集,应该会发生以下情况:
Source Deploy Path
/src/main/java/ -> /target/WEB-INF/classes/
/src/main/resources/ -> /target/WEB-INF/classes/
/src/main/webapp/ -> /target/
/src/test/java/ -> /target/WEB-INF/classes/
/src/test/resources/ -> /target/WEB-INF/classes/
/target/m2e-wtp/web-resources/ -> /target/
至于我的构建路径,目前是这样的:
site/src/main/java
Output folder: site/target/classes
Included: (All)
Excluded: (None)
Native library location: (None)
Ignore optional compile problems: No
site/src/main/resources
Output folder: site/target/classes
Included: (All)
Excluded: **
Native library location: (None)
Ignore optional compile problems: No
site/src/test/java
Output folder: site/target/test-classes
Included: (All)
Excluded: (None)
Native library location: (None)
Ignore optional compile problems: No
site/src/test/resources
Output folder: site/target/test-classes
Included: (All)
Excluded: **
Native library location: (None)
Ignore optional compile problems: No
也许构建路径和 Web 部署程序集相互冲突,或者我只是没有以正确的方式构建它?我通过按下 Eclipse 的运行按钮来运行这个应用程序,并且正在使用 Spring Boot,如下所示:
package net.site.config;
import java.util.Arrays;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application
{
public static void main(String[] args) throws Throwable
{
ApplicationContext ctx = SpringApplication.run(Application.class, args);
System.out.println("Inspecting the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for(String beanName : beanNames)
{
System.out.println(beanName);
}
}
}
如果我的 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>site</groupId>
<artifactId>site</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>site</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.0.0.RC4</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.0.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</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-beans</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
还有我的 .classpath 文件夹:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
非常感谢任何帮助,因为我很想尽快克服这个障碍。如果您需要更多信息,请告诉我 - 谢谢!
【问题讨论】:
标签: eclipse spring maven spring-mvc