Spring+hibernate集成方式(二)

       第二种集成方案原理:将StrutsAction交给Spring创建,这样,业务逻辑对象将会被注入,这样就避免了以来查找。其中代理Action的作用取得BeanFactory,然后到ioc容器中将本次请求对应的Action取出

 

       当客户端发送请求时,仍然由web.xml转到ActionServlet,在这里,struts-config.xml都将其转到DelegatingAction中,将取得具体Action对象的操作交给Spring框架来实现。在applicationContext中,由url字符串的不同,将具体的ActionBean创建好。我们在代理Action中,拿到Beanfactory,然后,根据需求取得相应的Action即可。

      部分代码实现:

web.xml

<servlet>

    <servlet-name>action</servlet-name>

    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

    <init-param>

      <param-name>config</param-name>

      <param-value>/WEB-INF/struts-config.xml</param-value>

    </init-param>

  </servlet>

  <!-- Standard Action Servlet Mapping -->

  <servlet-mapping>

    <servlet-name>action</servlet-name>

    <url-pattern>*.do</url-pattern>

  </servlet-mapping>

  

  <context-param>

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

    <param-value>classpath:applicationContext-*.xml</param-value>

  </context-param>

  <listener>

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

  </listener>

struts-config.xml

<struts-config>

    <form-beans>

        <form-bean name="loginForm" type="com.bjpowernode.usermgr.web.forms.LoginActionForm"/>

    </form-beans>

    <action-mappings>

        <action path="/login"

                type="org.springframework.web.struts.DelegatingActionProxy"

                name="loginForm"

                scope="request"

        >

            <forward name="success" path="/login_success.jsp"/>

        </action>

    </action-mappings>

    <message-resources parameter="MessageResources" />

</struts-config>


LoginActionForm.java

publicclass LoginActionForm extends ActionForm {

    private String username;

    private String password;

    public String getUsername() {

        returnusername;

    }

    publicvoid setUsername(String username) {

        this.username = username;

    }

    public String getPassword() {

        returnpassword;

    }

    publicvoid setPassword(String password) {

        this.password = password;

    }

}


LoginAction.java

publicclass LoginAction extends Action {

    private UserManager userManager;

    @Override

    public ActionForward execute(ActionMapping mapping, ActionForm form,

            HttpServletRequest request, HttpServletResponse response)

            throws Exception {

        LoginActionForm laf = (LoginActionForm)form;

        String username = laf.getUsername();

        String password = laf.getPassword();

        userManager.login(username, password);

        return mapping.findForward("success");

    }

    publicvoid setUserManager(UserManager userManager) {

        this.userManager = userManager;

    }

}


applicationContext.xml

<bean name="/login" class="com.bjpowernode.usermgr.web.actions.LoginAction" scope="prototype">
  <property name="userManager" ref="userManager"/>
 </bean>
<bean id="userManager" class="com.bjpowernode.usermgr.manager.UserManagerImpl"/>
posted @ 2012-06-01 21:56  转航  阅读(162)  评论(0编辑  收藏  举报