Spring Maven - 应用程序无法启动

时间:2015-08-10 07:49:00

标签: java spring maven spring-mvc java-ee

我遵循了开发spring web mvc应用程序的教程。我也使用maven来更轻松地解决我的家属,但似乎没有用。当我尝试运行项目时,它不会转到我的index.jsp,如果我尝试运行每个页面,它也会返回404。 这是我的控制器:

package com.springmvc.tutorial.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

@RequestMapping("/")
public ModelAndView helloWorld(){
    String message ="Hello world! Spring !";
    return new ModelAndView("hello","message",message);
}
}

这是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>SpringMVC9Tutorial</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>spring</display-name>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
 <servlet-mapping>
 <servlet-name>spring</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>
 </web-app>

这是spring-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">


<context:component-scan
    base-package="com.springmvc.tutorial.controller">
</context:component-scan>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
      <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

<mvc:default-servlet-handler />
</beans>

我的jsps有以下结构:

WEB-INF/index.jsp
WEB-INF/web.xml
WEB-INF/spring-servlet.xml
WEB-INF/jsp/hello.jsp

我看到这可能是一个重复的问题,但我找不到让我满意的答案。我没有任何错误或例外,可能只是一条警告信息:

[SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SpringMVC9Tutorial' did not find a matching property.

这是教程;我没有完全遵循它: http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/

1 个答案:

答案 0 :(得分:2)

我认为您的servlet上下文未正确初始化。请尝试在web.xml中指定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>

编辑:您的servelt上下文命名与servletname匹配,因此您不需要明确提及该文件。如果您有特定名称,您可以像上面一样指定。

<property name="prefix" value="/WEB-INF/jsp/"></property>
WEB-INF/index.jsp

您错过了WEB-INF中jsp的文件夹,正如您在servelt上下文中提到的那样。但是你的JSP不在jsp文件夹中。

告诉spring在spring-servlet.xml中通过这样的注释加载控制器

<mvc:annotation-driven/>