急!!!关于SSH2 spring对象注入问题..感觉很简单就是不知道哪错了.

覆水难收 2010-11-17 11:10:10
今天做了一个关于SSH2得工程. tomcat启动没有报错.struts框架好使.但是当action类直接调用dao类就出错了.给人的感觉就是dao的对象没有注入进去...关于整合这块我是个菜鸟.找了2天了.希望高手来指点指点啊..



Spring 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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:oracle9i">
</property>
<property name="username" value="cxc"></property>
<property name="password" value="cxc"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>cn/cxc/model/Userinfo.hbm.xml</value></list>
</property></bean>

<bean id="UserinfoDAO" class="cn.cxc.dao.UserinfoDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="userinfoaction" class="cn.cxc.action.UserinfoAction">
<property name="userinfodao">
<ref bean="UserinfoDAO" />
</property>
</bean>

</beans>




Action类代码



package cn.cxc.action;

import javax.persistence.Entity;

import cn.cxc.dao.UserinfoDAO;
import cn.cxc.model.Userinfo;


@Entity
public class UserinfoAction {
private UserinfoDAO userinfodao;
public UserinfoDAO getUserinfodao() {
return userinfodao;
}
public void setUserinfodao(UserinfoDAO userinfodao) {
this.userinfodao = userinfodao;
}
public String test(){
userinfodao.sshtest();
System.out.println("测试struts");
return "success";
}
}




DAO 类 代码



package cn.cxc.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.cxc.model.Userinfo;

public class UserinfoDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(UserinfoDAO.class);
//property constants
public static final String USERNAME = "username";
public static final String PASSWORD = "password";

public void sshtest(){
System.out.println("测试SSH2....");
}

以下忽略。。。



然后是异常..类代码




at cn.cxc.action.UserinfoAction.test(UserinfoAction.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)

以下忽略...
...全文
345 34 打赏 收藏 举报
写回复
34 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
覆水难收 2010-11-17
[Quote=引用 28 楼 gukuitian 的回复:]
25楼的不是说了吗,试试


XML code


<struts>
<constant name="struts.objectFactory" value="spring" />
<package name="struts2springhibernate" extends="struts-default">
<action name="*……
[/Quote]

不好使...依然老样子
  • 打赏
  • 举报
回复
gukuitian 2010-11-17
25楼的不是说了吗,试试



<struts>
<constant name="struts.objectFactory" value="spring" />
<package name="struts2springhibernate" extends="struts-default">
<action name="*Action" class="userinfoaction" method="{1}">
<result name="success">MyJsp.jsp</result>
</action>
</package>
</struts>


  • 打赏
  • 举报
回复
覆水难收 2010-11-17
大虾们....咋都没动静了啊..
  • 打赏
  • 举报
回复
覆水难收 2010-11-17
[Quote=引用 25 楼 magicluo 的回复:]
你已经把 UserinfoAction 通过 spring来托管了,别名是userinfoaction

那你在struts配置文件中的action 是不是 userinfoaction?

不要写成了 cn.cxc.action.UserinfoAction
[/Quote]

struts配置文件



<struts>
<constant name="struts.objectFactory" value="spring" />
<package name="struts2springhibernate" extends="struts-default">
<action name="*Action" class="cn.cxc.action.UserinfoAction" method="{1}">
<result name="success">MyJsp.jsp</result>
</action>
</package>
</struts>
  • 打赏
  • 举报
回复
magicluo 2010-11-17
你已经把 UserinfoAction 通过 spring来托管了,别名是userinfoaction

那你在struts配置文件中的action 是不是 userinfoaction?

不要写成了 cn.cxc.action.UserinfoAction
  • 打赏
  • 举报
回复
覆水难收 2010-11-17
全部的异常信息


java.lang.NullPointerException
cn.cxc.action.UserinfoAction.test(UserinfoAction.java:23)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)


  • 打赏
  • 举报
回复
覆水难收 2010-11-17
[Quote=引用 21 楼 zl3450341 的回复:]
你的配置文件有问题,可以去官网下个struts2的文档,里面有struts+spring+JPA的配置文件的例子。
如果配置文件对着文档弄好了,还有问题的话,看看jar包有冲突没有
[/Quote]

恩 下午回来我看看..谢谢..
  • 打赏
  • 举报
回复
覆水难收 2010-11-17
[Quote=引用 19 楼 fulinkster 的回复:]
你有struts2-spring-plugin-2.1.8.1.jar这个包吗?
[/Quote]

又回到了以前的起点...不报异常了.但是还是注入不进去..
  • 打赏
  • 举报
回复
你的配置文件有问题,可以去官网下个struts2的文档,里面有struts+spring+JPA的配置文件的例子。
如果配置文件对着文档弄好了,还有问题的话,看看jar包有冲突没有
  • 打赏
  • 举报
回复
覆水难收 2010-11-17
我也搜到了 在找..马上...
  • 打赏
  • 举报
回复
fulinkster 2010-11-17
你有struts2-spring-plugin-2.1.8.1.jar这个包吗?
  • 打赏
  • 举报
回复
覆水难收 2010-11-17

加入<constant name="struts.objectFactory" value="spring"></constant> 出现的异常..

严重: Exception starting filter struts2
Unable to load configuration. - [unknown location]
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:431)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4001)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4651)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1041)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:964)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:502)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1277)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:321)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
at org.apache.catalina.core.StandardService.start(StandardService.java:519)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: Unable to load configuration. - [unknown location]
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:374)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418)
... 29 more
Caused by: Cannot locate the chosen ObjectFactory implementation: spring - [unknown location]
at org.apache.struts2.config.BeanSelectionProvider.alias(BeanSelectionProvider.java:293)
at org.apache.struts2.config.BeanSelectionProvider.alias(BeanSelectionProvider.java:264)
at org.apache.struts2.config.BeanSelectionProvider.register(BeanSelectionProvider.java:203)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:169)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)
... 31 more
2010-11-17 11:39:25 org.apache.catalina.core.StandardContext start
严重: Error filterStart
2010-11-17 11:39:25 org.apache.catalina.core.StandardContext start
严重: Context [/ssh5] startup failed due to previous errors
2010-11-17 11:39:25 org.apache.catalina.core.ApplicationContext log
信息: Closing Spring root WebApplicationContext
2010-11-17 11:39:25 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
严重: The web application [/ssh5] registered the JBDC driver [oracle.jdbc.driver.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
log4j:ERROR LogMananger.repositorySelector was null likely due to error in class reloading, using NOPLoggerRepository.



  • 打赏
  • 举报
回复
覆水难收 2010-11-17
[Quote=引用 14 楼 fulinkster 的回复:]
引用 6 楼 qq12306674 的回复:

引用 5 楼 liuyuanshijie 的回复:
<constant name="struts.objectFactory" value="spring" /> 这个有吧??


貌似没有..


<constant name="struts.objectFactory" value="spring"></constant>
把……
[/Quote]

出异常了....
  • 打赏
  • 举报
回复
gukuitian 2010-11-17
异常信息都拿出来看看。
  • 打赏
  • 举报
回复
覆水难收 2010-11-17
我又搭建了一个更高版本的.结果效果是一样的. 什么都好使 唯独对象注入不进去....
  • 打赏
  • 举报
回复
fulinkster 2010-11-17
[Quote=引用 6 楼 qq12306674 的回复:]

引用 5 楼 liuyuanshijie 的回复:
<constant name="struts.objectFactory" value="spring" /> 这个有吧??


貌似没有..
[/Quote]

<constant name="struts.objectFactory" value="spring"></constant>
把这个加到struts.xml里
  • 打赏
  • 举报
回复
覆水难收 2010-11-17
那是我日志文件打出来的信息..反正什么也没提示就是一个空指针.下面都是重复了.没有实际意义.
  • 打赏
  • 举报
回复
gukuitian 2010-11-17
异常信息全吗?
  • 打赏
  • 举报
回复
覆水难收 2010-11-17
[Quote=引用 10 楼 gukuitian 的回复:]
<property name="mappingResources">
<list>
<value>cn/cxc/model/Userinfo.hbm.xml</value>
</list>
</property>

这里你 不觉得少了一个吗?
[/Quote]

少了什么?这里我是用hibernate反向映射的...都是自动生成的代码..
  • 打赏
  • 举报
回复
gukuitian 2010-11-17
<property name="mappingResources">
<list>
<value>cn/cxc/model/Userinfo.hbm.xml</value>
</list>
</property>

这里你 不觉得少了一个吗?
  • 打赏
  • 举报
回复
加载更多回复
相关推荐
发帖
Web 开发
加入

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-11-17 11:10
社区公告
暂无公告