当前位置: 代码迷 >> Java Web开发 >> SSH使用注解方式出错误 求教
  详细解决方案

SSH使用注解方式出错误 求教

热度:1341   发布时间:2013-02-25 21:11:16.0
SSH使用注解方式出异常 求教
一个简单的SSH(struts2.1 + hibernate3.3 + spring3.0)例子改为用注解配置后启动tomcat报错:
 Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userAction' defined in file [C:\Program Files\Apache Software Foundation\apache-tomcat-7.0.5\webapps\ssh5\WEB-INF\classes\com\lm\struts2\userAction.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.lm.struts2.userAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:946)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:892)
.............
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.lm.struts2.userAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141)
..................
Caused by: java.lang.NullPointerException
at org.apache.struts2.ServletActionContext.getRequest(ServletActionContext.java:112)
at com.lm.struts2.userAction.<init>(userAction.java:21)

意思好像是不能初始化UserAction

下面是配置文件

applicationContext.xml:
XML code
<?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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    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"      default-autowire="byName">        <context:annotation-config />    <context:component-scan base-package="com.lm"></context:component-scan>    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">        <property name="driverClassName"            value="com.mysql.jdbc.Driver">        </property>        <property name="url" value="jdbc:mysql://127.0.0.1:3306"></property>        <property name="username" value="root"></property>        <property name="password" value="lm"></property>        <property name="maxActive" value="100"></property>        <property name="maxIdle" value="30"></property>        <property name="maxWait" value="500"></property>        <property name="defaultAutoCommit" value="true"></property>    </bean>    <bean id="sessionFactory"        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="configLocation"            value="classpath:hibernate.cfg.xml">        </property>        <property name="dataSource" ref="dataSource"></property>    </bean></bean>


struts.xml:
XML code
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><package name="suibiaojiao" extends="struts-default" namespace="/" >   <action name="*_*" class="com.lm.struts2.{1}Action" method="{2}" >      <result name="ok"  type="redirect">user_findAll.action</result>      <result name="error">/error.jsp</result>      <result name="findAllOK">/studentList.jsp</result>      <result name="deleteOK" type="redirect">user_findAll.action</result>      <result name="input" >/save.jsp</result>        <result name="updatePOK" >/update.jsp</result>        <result name="updateOK" type="redirect">user_findAll.action</result>     </action> </package></struts>
  相关解决方案