struts2零配置导致spring注入失败


先贴代码: com.action.LoginAction


 package com.action;
import org.apache.struts2.convention.annotation.*;
import com.opensymphony.xwork2.ActionSupport;
import com.service.ILoginUserService;

@Namespace("/")
@ParentPackage("struts-default")

public class LoginAction extends ActionSupport
{
private LoginUserBean loginUser = null;
private ILoginUserService userService = null;


public void setLoginUser(LoginUserBean loginUser) 
{
    this.loginUser = loginUser;
}

public LoginUserBean getLoginUser() 
{
    return loginUser;
}

public void setUserService(ILoginUserService userService) 
{
    System.out.println("----setUserService----");
    this.userService = userService;
}

public ILoginUserService getUserService() 
{
    return userService;
}

@Action(value="login",
        results=
        {
            @Result(name="success", location="/info.jsp"),
            @Result(name="error", location="/error.jsp")
        }
)
public String login() throws Exception
{
    System.out.println("----login() Method----");
  //System.out.println(userService.findById("0001").getUsername());
    if(userService.login(loginUser.getUsername(), loginUser.getPassword()) == null)
        return SUCCESS;
    return ERROR;
}

}

applicationContext.xml


 <bean id="UserDAO" class="com.dao.UserDAO">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>
<bean id="LoginUserService" class="com.service.LoginUserService">
    <property name="userDAO">
        <ref bean="UserDAO"/>
    </property>
</bean>
<bean id="LoginAction" class="com.action.LoginAction" scope="prototype">
    <property name="userService">
        <ref bean="LoginUserService"/>
    </property>
</bean>

struts.xml


 <?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.devMode" value="true"/>
<constant name="struts.convention.classes.reload" value="true"/>

 <!-- 
 <package name="struts2" extends="struts-default" namespace="/">
    <action name="login" class="LoginAction" method="login">
        <result name="success">/info.jsp</result>
        <result name="error">/error.jsp</result>
    </action>
 </package>
 -->
</struts>

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>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.FilterDispatcher
    </filter-class>
    <init-param>
        <param-name>actionPackage</param-name>
        <param-value>com.action</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
 </web-app>

运行结果:

请输入图片描述 环境说明:

IDE:Myeclipse8.6

框架版本为:Struts2 + Spring3 + Hibernate3.3

服务器:Tomcat6.0

数据库:Oracle 10g

数据库连接的是公司服务器的数据库,所以applicationContext.xml关于数据库那段配置没有贴,但是数据库是没有问题的,确定可以连接上。

问题描述:

用正常的struts配置文件写配置代码(struts.xml里注释部分),spring可以正常注入, 但是用struts的零配置方法写配置代码,spring无法对userService对象进行注入,也就是运行结果为空指针。

有没有哪位高手知道,或者哪位同仁遇到过类似的问题,请指点一下~

spring struts2 java-ee

我也有ID了 11 years, 4 months ago

Your Answer