spring和struts整合中注意的问题
来源:互联网 发布:把app软件绿色 编辑:程序博客网 时间:2024/04/28 00:34
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 内容很简单。不粘贴了。
- spring和struts整合中注意的问题
- spring和struts整合中注意的问题
- Struts时整合spring 应注意的问题
- struts 2整合spring要注意的问题(二)
- JPetStore中Struts和Spring的整合
- struts和spring的整合
- struts和spring的整合!
- 关于Struts、Spring和Hibernate整合的问题
- 有关Struts和Spring框架整合时出现的问题
- struts和spring整合创建Action对象的问题?
- 关于struts和Spring整合问题
- Spring 和 struts 整合的三种方式。<注意》本文为转贴
- Struts2和Spring整合时使用Struts-plugin的注意点
- 关于spring整合struts的问题
- 整合 Struts 和 Spring
- struts和spring整合
- 整合 Struts 和 Spring
- 整合 Struts 和 Spring
- 朋友结婚了
- 用.NET框架实现序列化
- 在html文件引入其它html文件的几种方法
- 你必须知道的.NET之恩怨情仇:is和as
- 工作日记——PS中的Graphics State
- spring和struts整合中注意的问题
- [转载]JavaScript如何区分浏览器的类型
- 排列组合算法
- Hashcode
- 从网上整理的一些电信词汇
- 深入了解Oracle的体系结构
- 什么是 AJAX?
- 树形语句
- vs快捷键大全(转)