使用spring+struts2无法注入bean对象,求救!

cap_jack_sp 2015-11-08 07:01:10
配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

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

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

application.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<import resource="com/common/commonContext.xml"/>
<bean id="optLoggerBean"
class="com.common.OptLoggerBean">
</bean>
<bean id="exceptionLogger"
class="com.common.ExceptionLogger">
</bean>
<aop:config>
<aop:pointcut id="actionPoint"
expression="within(com.*.action..*)"/>
<aop:aspect id="optLoggerAspect"
ref="optLoggerBean">
<aop:around pointcut-ref="actionPoint"
method="logger"/>
</aop:aspect>

<aop:aspect id="exceptionAspect"
ref="exceptionLogger">
<aop:after-throwing
pointcut-ref="actionPoint"
method="logger"
throwing="e"/>
</aop:aspect>
</aop:config>
<bean id="stuDao" class="com.huangcf.dao.impl.StudentDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="ser" class="com.huangcf.service.impl.StudentServiceImpl">
<property name="dao" ref="stuDao"></property>
<property name="transactionTemplate" ref="transactionTemplate" />
</bean>

<bean id="studentAction" class="com.huangcf.action.StudentAction"
scope="prototype">
<property name="ser" ref="ser"></property>
<property name="username">
<value type="java.lang.String">admin</value>
</property>
<property name="password">
<value type="java.lang.String">admin</value>
</property>
</bean>
</beans>

commonContext.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 id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:ORCL</value>
</property>
<property name="username">
<value>system</value>
</property>
<property name="password">
<value>Aa123456</value>
</property>
<property name="maxActive">
<value>255</value>
</property>
<property name="maxIdle">
<value>2</value>
</property>
<property name="maxWait">
<value>120000</value>
</property>
</bean>

<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
</bean>
<!--
<bean id="jdbctTxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" >
</property>
</bean>
<bean
id="JdbcBasicTxProxy" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<description>数据库事务代理</description>
<property name="transactionManager" ref="jdbctTxManager"/>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean
id="BasicJdbcDAO" abstract="true"
class="org.springframework.jdbc.core.support.JdbcDaoSupport">
<description>JdbcDAO</description>
<property name="dataSource" ref="dataSource"/>
</bean> -->
</beans>

action代码:

package com.huangcf.action;

import com.common.BaseAction;
import com.huangcf.entity.Student;
import com.huangcf.service.StudentService;

public class StudentAction extends BaseAction {

public String username;
public String password;
private StudentService ser;

@Override
public String execute(){
try{
Student student = new Student();
student.setUsername(username);
student.setPassword(password);
ser.login(student);
}catch(Exception e){
e.printStackTrace();
logger.error(e);
}
return SUCCESS;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public StudentService getSer() {
return ser;
}

public void setSer(StudentService ser) {
this.ser = ser;
}
}

service代码:

package com.huangcf.service.impl;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.huangcf.dao.StudentDao;
import com.huangcf.entity.Student;
import com.huangcf.service.StudentService;

public class StudentServiceImpl implements StudentService {

private StudentDao dao;
private TransactionTemplate transactionTemplate;

public boolean login(final Student stu) {
transactionTemplate.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus arg0) {
boolean b = dao.login(stu);
System.out.println(b);
return b;
//throw new RuntimeException("抛出异常,在transactionTemplate中回调的事务将会回滚");
}
});
return false;
}

public StudentDao getDao() {
return dao;
}

public void setDao(StudentDao dao) {
this.dao = dao;
}

public TransactionTemplate getTransactionTemplate() {
return transactionTemplate;
}

public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
}

dao代码:

package com.huangcf.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.common.BaseDAO;
import com.huangcf.dao.StudentDao;
import com.huangcf.entity.Student;

public class StudentDaoImpl extends BaseDAO implements StudentDao {
public boolean login(Student stu) {
String sql = "select login_name username,password password from tb_user where login_name = 'admin' and password = 'admin'";
Student item = queryForObject(sql, Student.class);
if(item == null){
return false;
}
return true;
}
}

调用action时报空指针,说service为空username、password也是null,原因在哪?
...全文
158 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-11-08 07:01
社区公告
暂无公告