ssh action调用sevice层方法空指针状况
代码[code=Java][/code]package action;
import model.User;
import service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
@SuppressWarnings("serial")
public class UserAction extends ActionSupport{
private String password;
private List<User> userList;
private String username;
private UserService userService;
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public UserService getUserService() {
return userService;
}
public String login(){
System.out.println(username);
userList=userService.Login(username, password);
if(userList.size()>0&&userList!=null){
return SUCCESS;
}
else{
return ERROR;
}
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public List<User> getUserList() {
return userList;
}
}
[code=Java][/code]package service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import dao.UserDao;
import model.User;
import service.UserService;
@Component("userService")
public class UserServiceImpl implements UserService {
private UserDao userDao;
public List<User> Login(String name,String pwd) {
// TODO Auto-generated method stub
List<User> userList=userDao.findByNameAndPwd(name, pwd);
if(userList.size()>0&&userList!=null){
return userList;
}
else{
return null;
}
}
@Resource
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public UserDao getUserDao() {
return userDao;
}
public UserServiceImpl(){
}
}
[code=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: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"
default-autowire="byName"
default-lazy-init="true"
>
<context:annotation-config />
<context:component-scan base-package="com.rec" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--
<property name="annotatedClasses">
<list>
<value>com.bjsxt.model.User</value>
<value>com.bjsxt.model.Log</value>
</list>
</property>
-->
<property name="packagesToScan">
<list>
<value>model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.nys.bookstore.service.*.*(..))" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="isBookExistByNameAndAuthor" read-only="true" />
<tx:method name="add*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
</beans>
[code=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>
<constant name="struts.i18n.encoding" value="utf-8" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="action.UserAction" method="login">
<result name="success">/main.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
</struts>
[解决办法]
这么长。。。。请问action中的27行在哪,顺便说一下,if(userList.size()>0&&userList!=null){这个把userList!=null 放在前面。
[解决办法]
你的action沒交給spring管理,注入不了了,如果你想action也用注解,包名要寫好哦。。。
package action;
<context:component-scan base-package="com.rec" />