我创建一个简单的Spring MVC项目。当我在Tomcat上运行它时,一切都可以正常运行,但是我想使用Maven Jetty插件来运行它。在项目结构的图片下面:
我的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>com.journaldev.spring.mvc</groupId>
<artifactId>spring-mvc-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Spring MVC Example</name>
<description>Spring MVC Hello World Example</description>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.9.v20160517</version>
<configuration>
<webApp>
<descriptor>/WEB-INF/web.xml</descriptor>
</webApp>
<scanIntervalSeconds>2</scanIntervalSeconds>
</configuration>
</plugin>
</plugins>
<finalName>${project.artifactId}</finalName> <!-- added to remove Version from WAR file -->
</build>
</project>
我的web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>spring-mvc-example</display-name>
<!-- Add Spring MVC DispatcherServlet as front controller -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HomeController.java文件:
@Controller
public class HomeController {
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
System.out.println("Home Page Requested, locale = " + locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate);
return "home";
}
@RequestMapping(value = "/user", method = RequestMethod.POST)
public String user(@Validated User user, Model model) {
System.out.println("User Page Requested");
model.addAttribute("userName", user.getUserName());
return "user";
}
}
控制台的输出:
[INFO] Configuring Jetty for project: Spring MVC Example
[INFO] webAppSourceDirectory C:\Users\Tom\Downloads\spring-mvc-example\src\main\webapp does not exist. Defaulting to C:\Users\Tom\Downloads\spring-mvc-example\src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = C:\Users\Tom\Downloads\spring-mvc-example\target\classes
[INFO] Context path = /
[INFO] Tmp directory = C:\Users\Tom\Downloads\spring-mvc-example\target\tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] web.xml file = null
[INFO] Webapp directory = C:\Users\Tom\Downloads\spring-mvc-example\src\main\webapp
[INFO] jetty-8.1.4.v20120524
[INFO] No Transaction manager found - if your webapp requires one, please configure one.
[INFO] No Spring WebApplicationInitializer types detected on classpath
[INFO] started o.m.j.p.JettyWebAppContext{/,file:/C:/Users/Tom/Downloads/spring-mvc-example/src/main/webapp},file:/C:/Users/Tom/Downloads/spring-mvc-example/src/main/webapp
[INFO] started o.m.j.p.JettyWebAppContext{/,file:/C:/Users/Tom/Downloads/spring-mvc-example/src/main/webapp},file:/C:/Users/Tom/Downloads/spring-mvc-example/src/main/webapp
[INFO] started o.m.j.p.JettyWebAppContext{/,file:/C:/Users/Tom/Downloads/spring-mvc-example/src/main/webapp},file:/C:/Users/Tom/Downloads/spring-mvc-example/src/main/webapp
[INFO] Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server
我认为我在pom.xml中配置Jetty时犯了一些错误,并且Jetty没有看到上下文。通常服务器已启动,没有错误。当我查看控制台的输出时,有:
web.xml file = null
pom.xml中的Jetty配置出现一些问题。谢谢。
答案 0 :(得分:0)
如果您可以更改项目结构,从/WebContent/
重命名为/src/main/webapp/
,则它将与Maven提供的所有Web应用程序服务(码头,tomcat,jboss,glassfish等)一起使用,包括所有Intellij和Eclipse IDE Webapp服务。