这个是我的Action类@Controller("loginAction")
public class LoginAction extends Action {

@Resource(name="easybuy_userService")
private Easybuy_userService userService;


public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord");
Easybuy_user ebuy_user = userService.findByName(userName, passWord);
request.getSession().setAttribute("ebuy_user", ebuy_user);
if(ebuy_user == null) {
return mapping.findForward("failed");
}
return mapping.findForward("success");
}
}
这个是我的service类
@Service("easybuy_userService")
public class Easybuy_userServiceImpl implements Easybuy_userService {
@Resource(name="easybuy_userDAO")
private Easybuy_userDAO userDAO;
public Easybuy_user findByName(String userName, String passWord) {
if((userName == null || userName == "") && (passWord == null || passWord == "")) {
return null;
}else {
Easybuy_user ebuy_user = userDAO.findByName(userName, passWord);
return ebuy_user;
}
}}
这个是我的配置文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
<!-- 配置annocation -->
<context:annotation-config></context:annotation-config>
<!-- spring需要注入的文件所在的包 -->
<context:component-scan base-package="com.wq.dao,com.wq.service,com.wq.web.action"></context:component-scan>
<!-- 放置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:orcl">
</property>
<property name="username" value="zhangbo"></property>
<property name="password" value="bobo"></property>
</bean>
<!-- 启动事务管理机制 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>

</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
这个是我的测试类public class TestService extends BaseTest{
@Resource(name="easybuy_userService")
private Easybuy_userService service;
@Before
public void setUp() throws Exception {
} @After
public void tearDown() throws Exception {
} @Test
public void testFindByName() {
//fail("Not yet implemented");
String userName = "zhangbo";
String passWord = "bobo";
Easybuy_user ebuy_user = service.findByName(userName, passWord);
System.out.println(ebuy_user.getEu_address());
}}
奇怪了。测试类中的Easybuy_userService的实现类都注入进来了呀。但是在Action中的Easybuy_userService的实现类就是NULL!!!!!
我采用的是spring的自动扫描机制。Action类中也已经被扫描到了呀。求解呀。
这个问题烦了几天了。。求大神帮忙看看呀。谢谢了。
对了。我用的是spring+struts1框架来管理的。

解决方案 »

  1.   

    spring 不需要setter方法了么?
      

  2.   

    stuys也不许交给spring管理,不知道你有没有交嘛
      

  3.   


    struts-config.xml<struts-config>
      <form-beans />
      <global-exceptions />
      <global-forwards />
      <action-mappings />
      
      <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
      </controller>
      <message-resources parameter="com.wq.web.ApplicationResources" />
    </struts-config>
    struts-config-user.xml
     
      <action-mappings >
        <action
          path="/test"
          type="com.wq.web.action.TestAction"
          validate="false"
          cancellable="true" />
        <action
          path="/login"
          type="com.wq.web.action.LoginAction"
          validate="false"
          cancellable="true">
          <forward name="failed" path="/login.jsp" />
          <forward name="success" path="/index.jsp" />
        </action>  </action-mappings>谢了
      

  4.   

    在struts的配置文件中class属性的值不能是action的包名+类名了,而是你spring中action的ID
      

  5.   


    不知道LZ代码贴的是否完整,不过从现在的代码来看,确实少了 get/set。
      

  6.   


    不对呀。
    现在我把struts的配置该成
     <action
          path="/login"
          type="loginAction"
          validate="false"
          cancellable="true">
          <forward name="failed" path="/login.jsp" />
          <forward name="success" path="/index.jsp" />
        </action>现在直接提示500错误了。
      

  7.   


    在这里get/set方法好像没有影响的。因为我的测试类里面也没有写get/set方法。但是测试类运行正常。