0

我想集成 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>

4

3 回答 3

1

你在混合东西。Spring 将创建一个上下文,因为您已经声明了一个ContextLoaderListener,但会从您的 struts.xml 配置文件Struts创建它自己的Action( ) 类堆栈。ActionSupport换句话说,它正在实例化自己的LoginAction对象。

于 2013-10-04T16:23:34.653 回答
1

就像@SotiriosDelimanolis 说的,你就是mixing东西。这是你做的:

  1. 从控制器类中删除 @Controller 注释。
  2. 检查您是否已经struts2-spring-plugin在类路径中。NOTE:插件的版本应该和你的 strut2 版本一样。
  3. make 你应该在 中定义你的spring bean application-context.xml,无论是显式的还是过度的组件扫描。
  4. 检查是否有

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

    在你的定义web.xml

玩得开心!这是link参考

于 2013-10-04T19:11:43.950 回答
0

您没有使用 spring MVC(在 web.xml 中不使用调度程序 servlet);但是您正在尝试在 Action servlet 中使用像 @Controller 这样的 MVC api 注释。那应该如何工作?

于 2013-10-04T18:40:48.073 回答