无法让Spring MVC Hello World工作

时间:2016-07-08 07:39:13

标签: spring-mvc

我尝试了各种建议,但无法使我的Spring MVC Hello World应用程序正常工作。有人可以用同样的方式帮助我吗?

以下是我的文件内容:

的web.xml

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <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.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

pom.xml(只是依赖项部分)

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.1.RELEASE</version>
    </dependency>
</dependencies>

spring.xml

<context:component-scan base-package="com.aditya.controllers" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

我的控制器类

@Controller
public class FirstController
{
    @RequestMapping("/helloWorld")
    public String greetUser(final ModelMap model)
    {
        model.addAttribute("message", "Hello World!!!");
        return "helloWorld";
    }
}

helloWorld.jsp(位于src / main / webapp / WEB-INF / jsp)

<html>
<body>
    <h1>${message}</h1>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

我发现至少有2个丢失的配置。

web.xml 中,您错过了ContextLoaderListener

<德尔>              org.springframework.web.context.ContextLoaderListener               ...     

spring.xml 中,您错过了控制器组件扫描配置。假设您的FirstController在包com.example.web.controller中。

<context:component-scan base-package="com.example.web.controller"></context:component-scan>

您可以参考Spring文档了解配置详细信息。 http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-servlet

答案 1 :(得分:0)

通过将web.xml修改为:

来实现它
<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <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.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

注意servlet-mapping的url-pattern的变化。