Spring MVC - 实例化servlet类时出错

时间:2015-11-22 13:19:26

标签: java spring spring-mvc tomcat

我正在尝试学习Spring MVC并且已经陷入了som问题。我想我的web.xml或servlet-config.xml文件中有错误。当我输入localhost:8080 / FitnessTracker / greeting.html时,我收到以下错误:

HTTP状态500 - 实例化servlet类时出错org.springframwork.web.servlet.DispatcherServlet

我的 web.xml 文件如下:

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>fitTrackerServlet</servlet-name>
        <servlet-class>org.springframwork.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/servlet-config.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>fitTrackerServlet</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

我的 servlet-config.xml 文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">


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


        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="sufffix" value=".jsp"/>
        </bean> 

        <mvc:annotation-driven></mvc:annotation-driven>


</beans>

我的控制器

package com.pluralsight.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    @RequestMapping(value = "/greeting")
    public String sayHello(Model model){

        model.addAttribute("greeting", "hello world!");


        return "Hello"; 
    }


}

enter image description here

enter image description here

可能出现什么问题?提前谢谢!

3 个答案:

答案 0 :(得分:2)

使用web.xml中的以下代码替换servlet类

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

您提到了错误的包名称。检查<servlet-class> servlet映射中web.xml中springframework的拼写。

如果使用java 8,请升级到spring版本4以解决任何不兼容问题

答案 1 :(得分:0)

尝试更改此行

localhost:8080/FitnessTracker/greeting.html

用这个

localhost:8080/FitnessTracker/greeting

尝试在web.xml中添加这样的反斜杠

 <url-pattern>/*</url-pattern>

答案 2 :(得分:-1)

解决。我的控制器返回&#34;您好&#34;而不是&#34;你好&#34;这是我想要显示的jsp文件的名称。