Spring配置文件
<bean id="loginService" class="com.testPlatform.service.LoginService"> <property name="userDao" ref="userDaoImp"/> </bean>
<bean id="loginAction" class="com.testPlatform.action.LoginAction" scope="prototype"> <property name="loginService" ref="loginService"/> </bean>
Struts配置文件<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <constant name = "struts.i18n.encoding" value="UTF-8"/> <package name ="login" extends="struts-default"> <action name="login" class="com.testPlatform.action.LoginAction" method="loginValidate"> <result name="error">/resource/login.jsp</result> <result name="success">/resource/main.jsp</result> </action> </package> </struts>
代码:
package com.testPlatform.action;
import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.testPlatform.domain.User; import com.testPlatform.service.LoginService;
public class LoginAction {
private LoginService loginService;
private Integer rtxID; private String password;
public String loginValidate() { User user = loginService.loginValidate(rtxID, password); if( null == user ) { return "error"; }
HttpServletRequest request = ServletActionContext.getRequest(); request.getSession().setAttribute("sessionuserinfo", user);
return "success"; }
public LoginService getLoginService() {
return loginService; } public void setLoginService(LoginService loginService) { System.out.println("LoginService setter---------------------------"); this.loginService = loginService; } }
每次页面发起请求 来到函数loginValidate()时,发现loginService的值为null,不知道为什么 我在LoginService类的构造函数中 增加了日志打印,tomcat启动的时候,出现了相应的日志,说明LoginService的构造函数已经被执行了,但为什么页面发起请求的时候LoginAction 的成员loginService的值为null?java.lang.NullPointerException com.testPlatform.action.LoginAction.loginValidate(LoginAction.java:19) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)以下是截取的 tomcat启动日志 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7c4f8358: defining beans [dataSource,sessionFactory,transactionManager,daoTemplate,userDaoImp,loginService,loginAction]; root of factory hierarchy 2015-3-20 14:22:31 org.springframework.orm.hibernate3.HibernateTransactionManager afterPropertiesSet 信息: Using DataSource [org.apache.commons.dbcp.BasicDataSource@7b3cb2c6] of Hibernate SessionFactory for HibernateTransactionManager UserDaoImp construct--------------------------- LoginService construct--------------------------- setUserDao setter--------------------------- LoginService setter---------------------------
struts2和spring需要一个插件来IOC###### struts配置呢 ######<bean id="loginService" class="com.testPlatform.service.LoginService">
com.testPlatform.service.LoginServiceImpl Bean指定的应该是实现类LoginServiceImpl ,不是接口
######
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。