spring框架和struts框架可以方便得进行整合,一般可以有三四种整合方式。
其中最常用的就是采用struts的action交给spring进行代理管理。但其中会遇到一些问题:
1. 首先注意版本的问题,如果使用struts1.2则应该使用spring.1.2x的版本,如果使用2.x的版本会无法加载相应的xml中的配置。
2。 注意整合中导入spring中对struts的支持包: spring-struts.jar和spring-web.jar
3。其他就是配置 在web.xml配置
<?
xml version="1.0" encoding="UTF-8"
?>
< web-app xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" version ="2.4"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< context-param >
< param-name > contextConfigLocation </ param-name >
< param-value >
/WEB-INF/classes/applicationContext.xml
</ param-value >
</ context-param >
< 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 > 3 </ param-value >
</ init-param >
< init-param >
< param-name > detail </ param-name >
< param-value > 3 </ param-value >
</ init-param >
< load-on-startup > 0 </ load-on-startup >
</ servlet >
<!-- <servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
这个servlet和下面的listence用其中任意一个即可 -->
< listener >
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
</ listener >
< servlet-mapping >
< servlet-name > action </ servlet-name >
< url-pattern > *.do </ url-pattern >
</ servlet-mapping >
</ web-app >
< web-app xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" version ="2.4"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< context-param >
< param-name > contextConfigLocation </ param-name >
< param-value >
/WEB-INF/classes/applicationContext.xml
</ param-value >
</ context-param >
< 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 > 3 </ param-value >
</ init-param >
< init-param >
< param-name > detail </ param-name >
< param-value > 3 </ param-value >
</ init-param >
< load-on-startup > 0 </ load-on-startup >
</ servlet >
<!-- <servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
这个servlet和下面的listence用其中任意一个即可 -->
< listener >
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
</ listener >
< servlet-mapping >
< servlet-name > action </ servlet-name >
< url-pattern > *.do </ url-pattern >
</ servlet-mapping >
</ web-app >
如果不配置监听或者apring的servlet,则无法进行加载。
4. spring的applictionContent.xml 比较简单就是配置一个bean和action
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="helloTest" class ="cn.HelloTest" ></ bean >
< bean name ="/helloAction" class ="cn.action.HelloAction" >
< property name ="helloService" >
< ref bean ="helloTest" />
</ property >
</ bean >
</ beans >
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="helloTest" class ="cn.HelloTest" ></ bean >
< bean name ="/helloAction" class ="cn.action.HelloAction" >
< property name ="helloService" >
< ref bean ="helloTest" />
</ property >
</ bean >
</ beans >
其中 name="/helloAction" 必须和struts的配置文件中的path一致 class即是struts的aciton类
5。struts的配置文件如下
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd" >
< struts-config >
< data-sources />
< form-beans />
< global-exceptions />
< global-forwards />
< action-mappings >
< action path ="/helloAction"
type ="org.springframework.web.struts.DelegatingActionProxy" >
< forward name ="ok" path ="/ok.jsp" ></ forward >
</ action >
</ action-mappings >
< message-resources parameter ="com.yourcompany.struts.ApplicationResources" />
</ struts-config >
<! DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd" >
< struts-config >
< data-sources />
< form-beans />
< global-exceptions />
< global-forwards />
< action-mappings >
< action path ="/helloAction"
type ="org.springframework.web.struts.DelegatingActionProxy" >
< forward name ="ok" path ="/ok.jsp" ></ forward >
</ action >
</ action-mappings >
< message-resources parameter ="com.yourcompany.struts.ApplicationResources" />
</ struts-config >
注意的是这里struts的action指向spring的一个代理类:org.springframework.web.struts.DelegatingActionPro通过代理建立关联。
6.其他就很简单了,看看action类的内容:
/**/
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package cn.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import cn.IHelloService;
/** */ /**
* MyEclipse Struts
* Creation date: 03-13-2008
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class HelloAction extends Action ... {
/**//*
* Generated Methods
*/
private IHelloService helloService;
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) ...{
// TODO Auto-generated method stub
String type = "aa";
if (request.getParameter("type").equals("1")) ...{
System.out.println("================================");
System.out.println(this.helloService.getString(type));
} else ...{
System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&");
}
return null;
}
/** *//**
* @return 返回 helloService。
*/
public IHelloService getHelloService() ...{
return helloService;
}
/** *//**
* @param helloService 要设置的 helloService。
*/
public void setHelloService(IHelloService helloService) ...{
this.helloService = helloService;
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
}
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package cn.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import cn.IHelloService;
/** */ /**
* MyEclipse Struts
* Creation date: 03-13-2008
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class HelloAction extends Action ... {
/**//*
* Generated Methods
*/
private IHelloService helloService;
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) ...{
// TODO Auto-generated method stub
String type = "aa";
if (request.getParameter("type").equals("1")) ...{
System.out.println("================================");
System.out.println(this.helloService.getString(type));
} else ...{
System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&");
}
return null;
}
/** *//**
* @return 返回 helloService。
*/
public IHelloService getHelloService() ...{
return helloService;
}
/** *//**
* @param helloService 要设置的 helloService。
*/
public void setHelloService(IHelloService helloService) ...{
this.helloService = helloService;
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
}
ok,这样基本完成了配置,对于另外的IHelloService 接口和实现类 HelloTest 内容很简单。不粘贴了。