Spring学习基础---与Struts整合_JSP教程
教程Tag:暂无Tag,欢迎添加,赚取U币!
推荐:解读Spring异常处理配置Spring异常处理之需要增加一个bean的配置: !--ExceptionResolver-- beanid=exceptionResolverclass=org.springframework.web.servlet.handler.SimpleMappingExceptionResolver propertyname=defaultErrorView value/exception/failure/value /prop
《Spring开发指南》只写了一种与struts整合的方法,另一种到Spring2.0 Demo自带的Doc中查找到Action直接继承ActionSupport。详细信息:To integrate your Struts application with Spring, you have two options:
Configure Spring to manage your Actions as beans, using the ContextLoaderPlugin, and set their dependencies in a Spring context file. Subclass Spring’s ActionSupport classes and grab your Spring-managed beans explicitly using a getWebApplicationContext() method.
16.3.2. ActionSupport Classes
As previously mentioned, you can retrieve the WebApplicationContext from the ServletContext using the WebApplicationContextUtils class. An easier way is to extend Spring’s Action classes for Struts. For example, instead of subclassing Struts’ Action class, you can subclass Spring’s ActionSupport class.
The ActionSupport class provides additional convenience methods, like getWebApplicationContext(). Below is an example of how you might use this in an Action:
public class UserAction extends DispatchActionSupport {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering ’delete’ method...");
}
WebApplicationContext ctx = getWebApplicationContext();
UserManager mgr = (UserManager) ctx.getBean("userManager");
// talk to manager for business logic
return mapping.findForward("success");
}
}Spring includes subclasses for all of the standard Struts Actions - the Spring versions merely have Support appended to the name:
ActionSupport,
DispatchActionSupport,
LookupDispatchActionSupport and
MappingDispatchActionSupport.
The recommended strategy is to use the approach that best suits your project. Subclassing makes your code more readable, and you know exactly how your dependencies are resolved. However, using the ContextLoaderPlugin allow you to easily add new dependencies in your context XML file. Either way, Spring provides some nice options for integrating the two frameworks.
可见第二种方法更加简便。
JPetstore例子中并没有使用此方法,而使用了第三种方法。同样只需要动Action。如下:
public abstract class BaseAction extends Action {
private PetStoreFacade petStore;
public void setServlet(ActionServlet actionServlet) {
super.setServlet(actionServlet);
if (actionServlet != null) {
ServletContext servletContext = actionServlet.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
this.petStore = (PetStoreFacade) wac.getBean("petStore");
}
}
protected PetStoreFacade getPetStore() {
return petStore;
}
}
ok,这三种方法都可以让Spring与Struts整合,相比较第一种(因为配置很多这里没写,见refernce 16.3.1), 第二第三种方法比较简洁,侵入不多。
分享:Spring学习基础---配置文件1,配置文件的配置头 ?xmlversion=1.0encoding=UTF-8? !-- -ApplicationcontextdefinitionforJPetStore’sbusinesslayer. -ContainsbeanreferencestothetransactionmanagerandtotheDAOsin -dataAccessContext-local/jta.xml(seeweb.xml’scontextConfigLo
相关JSP教程:
- jsp response.sendRedirect不跳转的原因分析及解决
- JSP指令元素(page指令/include指令/taglib指令)复习整理
- JSP脚本元素和注释复习总结示例
- JSP FusionCharts Free显示图表 具体实现
- 网页模板:关于jsp页面使用jstl的异常分析
- JSP页面中文传递参数使用escape编码
- 基于jsp:included的使用与jsp:param乱码的解决方法
- Java Web项目中连接Access数据库的配置方法
- JDBC连接Access数据库的几种方式介绍
- 网站图片路径的问题:绝对路径/虚拟路径
- (jsp/html)网页上嵌入播放器(常用播放器代码整理)
- jsp下显示中文文件名及绝对路径下的图片解决方法
- 相关链接:
- 教程说明:
JSP教程-Spring学习基础---与Struts整合。