我想集成 Struts 2、Hibernate 和 Spring。我之前用 Hibernate、Spring、Spring MVC 做了一个示例项目。它工作得很好。现在我想用 Struts 2 替换 Spring MVC。我有以下问题。我有带有字段的 LoginAction 类@Autowired private UserService userService
。UserService 类包含一个字段 UserDAO 字段,该字段也注释为 Autowired。UserDAO 也包含注解的 SessionFactory。我敢肯定,struts 的确认是正确的。而且applicationContext.xml也没错。因为当我在这个文件中进行一些更改时,我得到了一个例外。Tomcat 启动时没有错误,并显示登录页面。但是当我尝试调用 UserService 类的方法时,我得到了一个 NPE,因为这个对象是空的。为什么 Spring 没有设置这个对象?我的代码:applicationContext.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config />
<context:component-scan base-package="com"/>
<bean id="userDAO" class="com.example.dao.impl.hibernate.HibernateUserDAO" />
<bean id="userService" class="com.example.service.impl.UserServiceImpl" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:~/database" />
<property name="username" value="user" />
<property name="password" value="user" />
</bean>
<bean name="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.example.entity.User</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<welcome-file-list>
<welcome-file>/WEB-INF/pages/login.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
</web-app>
登录动作类:
@Controller
public class LoginAction extends ActionSupport {
private String login;
private String password;
@Autowired
private UserService userService;
@Override
public String execute() throws Exception {
User user = null;
try {
user = userService.findByLogin(login);
} catch (Exception e) {
e.printStackTrace();
}
if (user != null) {
return user.getPassword().equals(password) ? SUCCESS : ERROR;
}
return ERROR;
}
// getters and setters
}
用户服务实现类:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDAO userDAO;
@Override
@Transactional
public User findByLogin(String login) throws SQLException {
return userDAO.findByLogin(login);
} }
和 UserDAO impl 类:
@Repository
public class HibernateUserDAO implements UserDAO {
@Autowired
public SessionFactory sessionFactory;
@Override
public User findByLogin(String login) throws SQLException {
return sessionFactory.getCurrentSession().createCriteria(User.class)
.add(Restrictions.eq("login", login)).list().get(0);
} }
Struts 确认很好,因为它反应正确。为什么 Spring 没有设置字段?有任何想法吗?
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.example.web.action.LoginAction">
<result name="success">/WEB-INF/pages/cabinet.jsp</result>
<result name="error">/WEB-INF/pages/login.jsp</result>
<result name="input">/WEB-INF/pages/login.jsp</result>
</action>
</package>