执行简单的SPRING MVC示例时出错:SEVERE:为servlet mydispatcher分配异常

时间:2014-04-30 06:52:09

标签: java xml spring spring-mvc servlets

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>MySpringMVC</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

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

<servlet-mapping>
    <servlet-name>mydispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

但得到以下错误

SEVERE: StandardWrapper.Throwable
java.lang.NullPointerException
    at org.springframework.core.SerializableTypeWrapper$TypeProxyInvocationHandler.invoke(SerializableTypeWrapper.java:239)
    at org.springframework.core.$Proxy0.hashCode(Unknown Source)
    at org.springframework.util.ObjectUtils.nullSafeHashCode(ObjectUtils.java:273)
    at org.springframework.core.ResolvableType.hashCode(ResolvableType.java:794)
    at org.springframework.util.ConcurrentReferenceHashMap.getHash(ConcurrentReferenceHashMap.java:214)
    at org.springframework.util.ConcurrentReferenceHashMap.getReference(ConcurrentReferenceHashMap.java:246)
    at org.springframework.util.ConcurrentReferenceHashMap.get(ConcurrentReferenceHashMap.java:226)
    at org.springframework.core.ResolvableType.forType(ResolvableType.java:1169)
    at org.springframework.core.ResolvableType.forType(ResolvableType.java:1148)
    at org.springframework.core.ResolvableType.forTypes(ResolvableType.java:827)
    at org.springframework.core.ResolvableType.getInterfaces(ResolvableType.java:403)
    at org.springframework.core.ResolvableType.as(ResolvableType.java:365)
    at org.springframework.core.convert.support.GenericConversionService.getRequiredTypeInfo(GenericConversionService.java:266)
    at org.springframework.core.convert.support.GenericConversionService.addConverterFactory(GenericConversionService.java:103)
    at org.springframework.core.convert.support.DefaultConversionService.addScalarConverters(DefaultConversionService.java:79)
    at org.springframework.core.convert.support.DefaultConversionService.addDefaultConverters(DefaultConversionService.java:63)
    at org.springframework.core.convert.support.DefaultConversionService.<init>(DefaultConversionService.java:50)
    at org.springframework.core.env.AbstractPropertyResolver.<init>(AbstractPropertyResolver.java:41)
    at org.springframework.core.env.PropertySourcesPropertyResolver.<init>(PropertySourcesPropertyResolver.java:41)
    at org.springframework.core.env.AbstractEnvironment.<init>(AbstractEnvironment.java:112)
    at org.springframework.core.env.StandardEnvironment.<init>(StandardEnvironment.java:54)
    at org.springframework.web.context.support.StandardServletEnvironment.<init>(StandardServletEnvironment.java:44)
    at org.springframework.web.servlet.HttpServletBean.createEnvironment(HttpServletBean.java:215)
    at org.springframework.web.servlet.HttpServletBean.getEnvironment(HttpServletBean.java:205)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:126)
    at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:806)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)
Apr 30, 2014 12:13:15 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet mydispatcher
java.lang.NullPointerException
    at org.springframework.core.SerializableTypeWrapper$TypeProxyInvocationHandler.invoke(SerializableTypeWrapper.java:239)
    at org.springframework.core.$Proxy0.hashCode(Unknown Source)
    at org.springframework.util.ObjectUtils.nullSafeHashCode(ObjectUtils.java:273)
    at org.springframework.core.ResolvableType.hashCode(ResolvableType.java:794)
    at org.springframework.util.ConcurrentReferenceHashMap.getHash(ConcurrentReferenceHashMap.java:214)
    at org.springframework.util.ConcurrentReferenceHashMap.getReference(ConcurrentReferenceHashMap.java:246)
    at org.springframework.util.ConcurrentReferenceHashMap.get(ConcurrentReferenceHashMap.java:226)
    at org.springframework.core.ResolvableType.forType(ResolvableType.java:1169)
    at org.springframework.core.ResolvableType.forType(ResolvableType.java:1148)
    at org.springframework.core.ResolvableType.forTypes(ResolvableType.java:827)
    at org.springframework.core.ResolvableType.getInterfaces(ResolvableType.java:403)
    at org.springframework.core.ResolvableType.as(ResolvableType.java:365)
    at org.springframework.core.convert.support.GenericConversionService.getRequiredTypeInfo(GenericConversionService.java:266)
    at org.springframework.core.convert.support.GenericConversionService.addConverterFactory(GenericConversionService.java:103)
    at org.springframework.core.convert.support.DefaultConversionService.addScalarConverters(DefaultConversionService.java:79)
    at org.springframework.core.convert.support.DefaultConversionService.addDefaultConverters(DefaultConversionService.java:63)
    at org.springframework.core.convert.support.DefaultConversionService.<init>(DefaultConversionService.java:50)
    at org.springframework.core.env.AbstractPropertyResolver.<init>(AbstractPropertyResolver.java:41)
    at org.springframework.core.env.PropertySourcesPropertyResolver.<init>(PropertySourcesPropertyResolver.java:41)
    at org.springframework.core.env.AbstractEnvironment.<init>(AbstractEnvironment.java:112)
    at org.springframework.core.env.StandardEnvironment.<init>(StandardEnvironment.java:54)
    at org.springframework.web.context.support.StandardServletEnvironment.<init>(StandardServletEnvironment.java:44)
    at org.springframework.web.servlet.HttpServletBean.createEnvironment(HttpServletBean.java:215)
    at org.springframework.web.servlet.HttpServletBean.getEnvironment(HttpServletBean.java:205)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:126)
    at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:806)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)

mydispatcher.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean name="*/index.html" class="com.mvcController.HelloWorldController" />


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

HelloWorldController.java

package com.mvcController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloWorldController extends AbstractController 
{

protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    ModelAndView model = new ModelAndView("HelloWorldPage");
    model.addObject("msg", "hello world");
    return model;
}

} 

/WEB_INF/pages/HelloWorldPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<h1>Spring MVC Hello World Example</h1>
<h2>${msg}</h2>

</body>
</html>

我已经发布了我的简单helloworld项目中的所有代码,但是我认为xmls中的所有条目都没有正确执行,这是正确的。是的,我正在使用SPRING 4.0.3中的SPRING库,但是不同版本的SPRING会对执行简单的helloworld程序产生任何问题吗?

上面提到的每一件事都是在eclipse动态web项目中我只是在tomcat 6.0上运行index.html但是出现了错误。

来自WEB-INF / lib的罐子

commons-logging-1.1.3.jar
javax.servlet.jsp.jstl-1.2.1.jar
servlet-api-2.3.jar
spring-2.5.jar
spring-aop-4.0.3.RELEASE.jar
spring-aspects-4.0.3.RELEASE.jar
spring-beans-4.0.3.RELEASE.jar
 spring-context-4.0.3.RELEASE.jar
spring-core-4.0.3.RELEASE.jar
spring-expression-4.0.3.RELEASE.jar
spring-framework-bom-4.0.3.RELEASE.jar
spring-instrument-4.0.3.RELEASE.jar
spring-instrument-tomcat-4.0.3.RELEASE.jar
spring-jdbc-4.0.3.RELEASE.jar
spring-jms-4.0.3.RELEASE.jar
spring-messaging-4.0.3.RELEASE.jar
spring-orm-4.0.3.RELEASE.jar
spring-oxm-4.0.3.RELEASE.jar
spring-test-4.0.3.RELEASE.jar
spring-tx-4.0.3.RELEASE.jar
spring-web-4.0.3.RELEASE.jar
spring-webmvc-4.0.3.RELEASE.jar
spring-webmvc-portlet-4.0.3.RELEASE.jar
spring-websocket-4.0.3.RELEASE.jar

我已经包含了所有可用的罐子。我想扩展这个简单的MVC,所以刚刚添加...

任何帮助都会非常有用

2 个答案:

答案 0 :(得分:3)

这只是因为“spring-2.5.jar”文件。如果从lib文件夹和buildpath中删除该文件,它将正常工作。

答案 1 :(得分:0)

更改

<bean name="*/index.html" class="com.mvcController.HelloWorldController" />

<bean name="/index.html" class="com.mvcController.HelloWorldController" />

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>