Struts在SSH项目中的应用

Struts的详细使用可以参考在线文档http://struts.apache.org/1.3.10/userGuide/index.html

Struts在SSH项目中使用,其实就是要将spring与struts集成到一起,更大限度的发挥它们两者的作用。Struts本身是基于servelet技术的,它脱离不开web project。在使用struts的时候除了需要引入struts的相关jar包,最重要的是在web.xml中配置struts实现的ActionServlet,它是struts的核心控制器。最后引入struts-config.xml文件就可以使用struts了

Web.xml文件代码如下,注意观察配置的ActionServlet:

<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> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </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>

ActionServlet能够截取你请求的url,通过匹配struts-config.xml文件中的action标签中的path属性来找到我们自己编写的Action类,在它里面可以调用业务逻辑类,同时在action标签中已经配置了我们编写的ActionForm类,它的属性名称与调用Action的表单中的标签名称有对应且相同的关系。ActionServlet会将表单数据赋值给ActionForm,从而可以在我们编写的Action中使用ActionForm获取表单数据。其处理过程如下所示:


对应的struts-config.xml文件如下:

<struts-config> <form-beans> <form-bean name="LoginActionForm"type="com.myStruts.LoginActionForm"/> </form-beans> <action-mappings> <action path="/loginLogic" type="com.myStruts.LoginAction" name="LoginActionForm" scope="request" > <forward name="success"path="/success.jsp"></forward> <forward name="failure"path="/failure.jsp"></forward> </action> </action-mappings> </struts-config>

独立使用struts的时候,我们需要在struts的配置文件struts-config.xml中配置我们自己的Action,见上面的代码,但是如果我们将struts与spring集成到一起应用,情况就可能放生变化。

Struts与spring有两种集成方案,第一中集成方案struts-config.xml文件的配置不用改变,struts的MVC架构和未集成spring是一样的,如下图所示:


但未集成spring的时候我们会自己手动创建自己编写的业务类,而集成了spring以后我们我们可以将要创建的业务类纳入spring的ioc容器中进行管理,使用BeanFactory来创建。代码如下所示:

public class LoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionFormform, HttpServletRequest request, HttpServletResponse response) throws Exception { //转换成ActionForm LoginActionForm laf=(LoginActionForm)form; //获取表单的内容 int id=laf.getId(); String name=laf.getName(); //查询出输入id下的name BeanFactory bf=new ClassPathXmlApplicationContext("applicationContext.xml"); PersonManager personManager=(PersonManager)bf.getBean("PersonManager"); String retName=personManager.getName(id); //判断所输入用户名和通过id获取的用户名是否匹配 if(retName.equals(name)){ return mapping.findForward("success"); }else{ return mapping.findForward("failure"); } //returnsuper.execute(mapping, form, request, response); } }
当然这样做的前提是我们需要在spring的配置文件application-context.xml中配置好要创建的业务逻辑类PersonManager。配置如下所示:

<bean id="PersonDao4MySqlImplWithHibernate" class="com.mySpring.dao.PersonDao4MySqlImplWithHibernate"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="PersonManager" class="com.mySpring.manager.PersonManagerImpl"> <constructor-arg ref="PersonDao4MySqlImplWithHibernate"></constructor-arg> </bean>

这种集成中我们可以看出来Action依赖BeanFactory类,采用了依赖查找的方式来创建所需要调用的业务类PersonManager,我们可以采取第二种集成方案。它的原理图如下所示:


通过上图所示我们可以看出ActionServlet截取url后转向到的不再是我们自己编写的Action了,而是转向到spring实现的action代理类。也就是我们要将spring实现的代理Action(org.springframework.web.struts.DelegatingActionProxy)配置到struts-config.xml文件中去,实例如下所示:

<struts-config> <form-beans> <form-bean name="LoginActionForm"type="com.myStruts.LoginActionForm"/> </form-beans> <action-mappings> <action path="/loginLogic" type="org.springframework.web.struts.DelegatingActionProxy" name="LoginActionForm" scope="request" > <forward name="success"path="/success.jsp"></forward> <forward name="failure"path="/failure.jsp"></forward> </action> </action-mappings> </struts-config>

那么此时是如何调用到我们所真正需要的Action上的呢?了解这个问题首先要清楚DelegatingActionProxy类起到了什么作用。它会去得BeanFactory类而后在ioc容器中获取我们所需要的Action,同第一种集成方案不同之处除了在struts-config.xml文件中配置的Action为spring的代理Action外,还有一点不同就是要把我们真正需要调用的Action放到ioc容器中,即配置到spring的applicationContext.xml文件中。

在将我们自己的Action配置到applicationcontext.xml文件时有一种默认的规范,即我们此处配置的Bean不能像配置其他Bean一样使用id(参考上面的applicationContext.xml文件内容),而要使用name属性,并且这个name的值要和在struts-config.xml文件中配置代理action的path属性值相同,参见本段前两段的struts-config.xml文件代码,对比下面的applicationContext.xml代码如下:

<bean name="/loginLogic" class="com.myStruts.LoginAction" scope="prototype"> <property name="personManager"ref="PersonManager"></property> </bean>
本文介绍了struts的工作原理,以及它与spring集成应用的两种方案。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值