心灵之火. 2008-12-19 10:13
浏览 253
已采纳

ssh中整合,为什么将Action交由Spring托管,才可以在Action中使用依赖注入!

ssh中整合,为什么将Action交由Spring托管,才可以在Action中使用依赖注入!

传统正确的整合方式:

struts-config.xml:

<struts-config>

  <data-sources>

  <form-beans>

    <form-bean name="loginForm" type="com.yourcompany.struts.form.LoginForm">

  </form-beans>

  <action-mappings>

    <action <br="">      attribute="loginForm"

      input="/form/login.jsp"

      name="loginForm"

      path="/login"

      scope="request"

      type="org.springframework.web.struts.DelegatingActionProxy">  //使用代理

      <forward name="show" path="/show.jsp">

    </action>

  </action-mappings>

  <message-resources parameter="com.yourcompany.struts.ApplicationResources">

<plug-in classname="org.springframework.web.struts.ContextLoaderPlugIn">

<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml">

</plug-in>

</struts-config>



applicationContext.xml:

<beans>

<bean id="personDAO" class="com.deng.PersonDAO">



<bean name="/login" class="com.yourcompany.struts.action.LogAction">

<property name="personDAO" ref="personDAO">

</property>

</bean>

</beans>



web.xml:



<context-param>   

<param-name>contextConfigLocation</param-name>   



<param-value>/WEB-INF/applicationContext*.xml</param-value>   

</context-param>   

<listener>   

<listener-class>org.springframwork.web.content.ContextLoaderListener</listener-class>   

</listener> 





这样一来:我想在action里面注入server,只要在action里面

privaet PersonDAO personDAO;

public PersonDAO getPersonDAO() {

return personDAO;

}

public void setPersonDAO(PersonDAO personDAO) {

this.personDAO = personDAO;

}



就可以拿到personDAO对象想干吗就干吗了。。





我的问题是:



我前几天一个不小心有个问题没弄明白:为什么将Action交由Spring托管,才可以在Action中使用依赖注入!

我的:

struts-config.xml:

<action-mappings>

    <action <br="">      attribute="loginForm"

      input="/form/login.jsp"

      name="loginForm"

      path="/login"

      scope="request"

      type="com.yourcompany.struts.action.LogAction">  //不使用代理

      <forward name="show" path="/show.jsp">

    </action>



applicationContext.xml:

<beans>

<bean id="personDAO" class="com.deng.PersonDAO">



<bean name="/login" class="com.yourcompany.struts.action.LogAction">  //就象和<bean id="personDAO" class="com.deng.PersonDAO">一样<property name="personDAO" ref="personDAO">

</property>

</bean>

</beans>



然后在action里面

privaet PersonDAO personDAO;

..get()和..set()居然拿到的personDAO是空。为什么??



为什么:<bean id="personDAO" class="com.deng.PersonDAO">直接在spring里面直接写可以,而action非要托管才能注入,不能象server啊 dao之类的直接在applicationContext.xml里面写配置。



请大家教下我,谢谢了!

  • 写回答

6条回答 默认 最新

  • shangwulei 2008-12-24 17:26
    关注

    struts框架来支持产生的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?