Spring MVC 4 Hello World StandardWrapper.Throwable java.lang.NullPointerException

时间:2015-01-14 08:12:54

标签: java spring maven spring-mvc

我正在使用项目的maven结构和GlassFish v2.1来执行此应用程序。

我的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring.proj3</groupId>
<artifactId>Spring4MVCApp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Spring4MVCApp Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
    <java-version>1.7</java-version>
    <org.springframework-version>4.0.3.RELEASE</org.springframework-version>
    <org.springframework.security-version>3.2.3.RELEASE</org.springframework.security-version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>

    <!-- Spring -->
    <!-- Spring Dependencies -->
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
</dependencies>
<build>
    <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <configuration>
        <source>1.7</source>
        <target>1.7</target>
       </configuration>
    </plugin>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.4</version>
     </plugin>
    </plugins>

    <finalName>SpringSample2</finalName>

</build>

我的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>Archetype Created Web Application</display-name>

<servlet>
 <servlet-name>dispatcher</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>

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

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>

<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

调度-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

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

控制器

package com.javahash.spring.controller;

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

@Controller
public class HelloWorldController {

@RequestMapping("/hello")
public String hello(
        @RequestParam(value = "name", required = false, defaultValue = "World") String name,
        Model model) {
    model.addAttribute("name", name);
    return "helloworld";
   }

}

我的问题:

无法部署代码,在标题中出现上述错误。

尝试了一些步骤:

(i)修改web.xml顶部的版本 - 没有用 (ii)一个不同的,更晚的版本的春天。

这里没什么用。

堆栈跟踪

Severe: WebModule[/Spring4MVCApp]StandardWrapper.Throwable
java.lang.NullPointerException
at      org.springframework.core.SerializableTypeWrapper$TypeProxyInvocationHandler.invoke(SerializableTypeWrapper.java:239)
at org.springframework.core.$Proxy34.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)

Severe: WebModule[/Spring4MVCApp]PWC1396: Servlet /Spring4MVCApp threw load() exception
java.lang.NullPointerException
at       org.springframework.core.SerializableTypeWrapper$TypeProxyInvocationHandler.invoke(SerializableTypeWrapper.java:239)
at org.springframework.core.$Proxy34.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)

Severe: ContainerBase.addChild: start: 
LifecycleException:  java.lang.NullPointerException
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4927)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:5324)
at com.sun.enterprise.web.WebModule.start(WebModule.java:353)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:989)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:973)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:704)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1627)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1232)
at com.sun.enterprise.server.WebModuleDeployEventListener.moduleDeployed(WebModuleDeployEventListener.java:182)
at com.sun.enterprise.server.WebModuleDeployEventListener.moduleDeployed(WebModuleDeployEventListener.java:278)
at com.sun.enterprise.admin.event.AdminEventMulticaster.invokeModuleDeployEventListener(AdminEventMulticaster.java:1005)
at com.sun.enterprise.admin.event.AdminEventMulticaster.handleModuleDeployEvent(AdminEventMulticaster.java:992)
at com.sun.enterprise.admin.event.AdminEventMulticaster.processEvent(AdminEventMulticaster.java:470)
at com.sun.enterprise.admin.event.AdminEventMulticaster.multicastEvent(AdminEventMulticaster.java:182)
at com.sun.enterprise.admin.server.core.DeploymentNotificationHelper.multicastEvent(DeploymentNotificationHelper.java:308)
at com.sun.enterprise.deployment.phasing.DeploymentServiceUtils.multicastEvent(DeploymentServiceUtils.java:231)
at com.sun.enterprise.deployment.phasing.ServerDeploymentTarget.sendStartEvent(ServerDeploymentTarget.java:298)
at com.sun.enterprise.deployment.phasing.ApplicationStartPhase.runPhase(ApplicationStartPhase.java:132)

由于空间限制,已经剪掉了每条线下面的几条线

1 个答案:

答案 0 :(得分:-1)

从pom.xml中删除以下依赖项。在你的舞台上真的不需要弹簧。

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework-version}</version>
</dependency>