服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

整合spring与struts的几种方法

    spring与struts对model-view-controller模式均提供了很好的支持。相比之下,struts是一个比较单纯的mvc框架,在实际应用中,开发人员更喜欢使用struts完成mvc的设计,因而在有必要对spring与struts进行整合。

    整合的关键点在于:将struts中action的实例生成不再由struts自己负责,而交于spring容器去管理。因此,进行整合的一个技术前提是struts中作为controller的actionservlet必须能够装载spring的应用程序环境,而spring的org.springframework.web.struts.contextloaderplugin恰好提供了这种支持。在struts的配置文件struts-config.xml中,将contextloaderplugin注册即可,示例如下:

<?xml version="1.0" encoding="iso-8859-1"?>
<!doctype struts-config public
"-//apache software foundation//dtd struts configuration 1.1//en"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
...
<plug-in classname=”org.springframework.web.struts.contextloaderplugin”>
<set-property property=”contextconfiglocation” value=”/web-inf/config.xml”/>
</plug-in>
...
</struts-config>

    其中的config.xml是spring的配置文件。

    之后,我门可以采用三种方式之一来整合spring和struts:
    1.struts的action继承spring的actionsupport类,并在action中获取spring的applicationcontext。这是最简单的一种整合方式,但有三个缺点:第一,struts与spring紧密耦合,不能改换到其他ioc容器;第二,难以使用spring aop特性;第三,对于需要使用dispatchaction的struts应用无能为力。

    2.在struts的配置文件中,以spring的delegatingrequestprocessor类代替struts的requestprocessor类,并在spring的配置文件中定义与struts配置文件中<action-mappings>对应的bean,从而将struts的action与spring分开,并把struts的动作置于spring的控制之下。这种整合方式的优点是将不再依赖spring这个特定的ioc容器,但必须依赖struts的requestprocessor类。    

    3.通过spring的delegatingactionproxy类代理struts的动作,即在struts配置文件中,定义<action-mappings>的type属性全部改为delegatingactionproxy,而不是具体的类名,并在spring配置文件中定义与struts动作映射对应的bean,从而将struts的action与spring分开,并把struts的动作置于spring的控制之下。无疑,这是最灵活的一种整合方式。   

    下面就通过helloworld示例来分析一下第三种整合方式的实施步骤。

    step 1:修改struts的配置文件struts-config.xml
<?xml version="1.0" encoding="iso-8859-1" ?>
<!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.2//en" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
<form-beans>
<form-bean name="helloworld" type="com.strutstest.action.helloworld"/>
</form-beans>

<action-mappings>
<!--注意此处的type属性定义为delegatingactionproxy 类-->
<action path="/helloworld" type="org.springframework.web.struts.delegatingactionproxy" name="helloworld" validate="true" input="/web-inf/jsp/input.jsp">
<forward name="index" path="/web-inf/jsp/index.jsp"/>
<forward name="show" path="/web-inf/jsp/show.jsp"/>
</action>
<action
path="/input"
type="org.apache.struts.actions.forwardaction"
parameter="/web-inf/jsp/input.jsp"/>
</action-mappings>
<!--注册contextloaderplugin -->
<plug-in classname="org.springframework.web.struts.contextloaderplugin">
<set-property property="contextconfiglocation" value="/web-inf/config.xml" />
</plug-in>
<message-resources parameter="messages"/>
</struts-config>
    step 2:修改spring的配置文件config.xml
<?xml version="1.0" encoding="utf-8"?>
<!doctype beans public "-//spring//dtd bean 2.0//en"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
<bean id=”helloworldservice” class=”com.strutstest.service.impl.helloworldserviceimpl”>
</bean>

<!--注意此处的映射必须与struts中的动作对应-->
<bean name=”/helloworld” class=”com.strutstest.action.helloworldaction”>
<property name=”helloworldservice”>
<ref bean=”helloworldservice”/>
</property>
</bean>
</beans>
    step 3:定义作为model的action form类及相应接口、实现

    定义action form:
package com.strutstest.action;import javax.servlet.http.httpservletrequest;import org.apache.struts.action.actionerror;import org.apache.struts.action.actionerrors;import org.apache.struts.action.actionform;import org.apache.struts.action.actionmapping;public class helloworld extends actionform {private string msg = null;public void setmsg(string msg) {this.msg = msg;}public string getmsg() {return this.msg;}public void reset(actionmapping mapping, httpservletrequest req) {this.msg = null;}public actionerrors validate(actionmapping mapping,httpservletrequest request) {actionerrors errors = new actionerrors();if("".equals(getmsg())) {errors.add("msg",new actionerror("error"));}return errors;}}

    定义helloworld类的接口:

package com.strutstest.service;import com.strutstest.action.helloworld;public interface helloworldservice {public abstract string addmsg(helloworld helloworld);}

    定义接口的实现:

package com.strutstest.service.impl;

import com.strutstest.action.helloworld;
import com.strutstest.service.helloworldservice;

public class helloworldserviceimpl implements helloworldservice {
public string addmsg(helloworld helloworld) {
helloworld.setmsg("hello world... " + helloworld.getmsg());
return helloworld.getmsg();
}
}

    step 4:定义action

package com.strutstest.action;import java.io.ioexception;import java.util.hashmap;import java.util.map;import javax.servlet.servletexception;import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import org.apache.log4j.logger;import org.apache.struts.action.*;import org.apache.struts.actions.dispatchaction;import org.springframework.context.applicationcontext;import org.springframework.web.struts.actionsupport;import com.strutstest.service.helloworldservice;public class helloworldaction extends action {private logger logger = logger.getlogger(this.getclass().getname());//依赖注入 private helloworldservice helloworldservice;public helloworldservice gethelloworldservice () {return helloworldservice;}public void sethelloworldservice (helloworldservice helloworldservice) {this.helloworldservice = helloworldservice;}public actionforward execute(actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {string msg = gethelloworldservice().addmsg((helloworld)form);request.setattribute("helloworld", msg);return mapping.findforward("show");}}

    step 5:定义视图

    定义用户输入界面input.jsp:

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>

<%@page contenttype="text/html;charset=gbk"%>
<html:html locale="true">
<head>
<title><bean:message key="title"/></title>
<html:base/>
</head>
<html:messages id="msg">
<bean:write name="msg"/>
</html:messages>
<body>
<form name="helloworld" action="/mystruts/helloworld.do" method="post">
<bean:message key="welcome"/><input type="text" name="msg" value=""/><br>
<input type="submit" name="method" value="<bean:message key="submit"/>"/>
</form>
</body>
</html:html>

    定义系统响应界面show.jsp:

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@page contenttype="text/html;charset=gbk"%>
<html:html locale="true">
<head>
<title><bean:message key="title"/></title>
<html:base/>
</head>
<body bgcolor="white">
<html:errors/>
<html:messages id="msg">
<bean:write name="msg"/>
</html:messages>
<%
string str = (string)request.getattribute("helloworld");
%>
<body>
<font color=”blue”>
<bean:message key="input"/>"${helloworld}"<br>
</font>
</body>
</html:html>

扫描关注微信公众号