Spring之WEB模块配置详解

网友投稿 473 2023-03-09


Spring之WEB模块配置详解

Spring框架七大模块简单介绍

Spring中MVC模块代码详解

Spring的WEB模块用于整合Web框架,例如Struts1、Struts2、jsF等

整合Struts1

继承方式

Spring框架提供了ActionSupport类支持Struts1的Action。继承了ActionSupport后就能获取Spring的BeanFactory,从而获得各种Spring容器内的各种资源

import org.springframework.web.struts.ActionSupport;

public class CatAction extends ActionSupport{

public ICatService getCarService(){

return (ICatService) getWebApplicationContext().getBean("catService");

}

public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){

CatForm catForm = (CatForm) form;

if("list".equals(catForm.getAction())){

http:// returnthis.list(mapping,form,request,response);

}

}

public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){

CatForm catForm = (CatForm) form;

ICatService catService =getCatService();

List catList =catService.listCats();

request.setAttribute("carList",catList);

return mapping.find("list");

}

}

Spring在web.xml中的配置

contextConfigLocation

/WEB-INF/classes/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

CharacterEncodingFilter

/*

如果与Hibernate结合使用,需要在web.xml中添加OpenSessionInViewFilter过滤器,将session范围扩大到JSP层,防止抛出延迟加载异常

hibernateFilter

org.springframework.orm.hibernate3.support. OpenSessionInViewFilter

hibernateFilter

*.do

代理方式

继承方式融入Spring非常简单,但是缺点是代码与Spring发生了耦合,并且Action并没有交给Spring管理,因此不能使用Spring的AOP、IoC特性,使用代理方式则可以避免这些缺陷

public class CatAction extends Action{ //此处继承的Struts 1的Action

private ICatService catService;

//setter、getter略

public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){

CatForm catForm = (CatForm) form;

if("list".equals(catForm.getAction())){

returnthis.list(mapping,form,request,response);

}

}

public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){

CatForm catForm = (CatForm) form;

ICatService catService =getCatService();

List catList =catService.listCats();

request.setAttribute("carList",catList);

return mapping.find("list");

}

}

这个Action没有与Spring发生耦合,只是定义了一个ICatService属性,然后由Spring负责注入

struts-congfig.xml配置

web.xml的配置与上面的继YsRZhUuWfw承方式相同

使用代理方式的Action可以配置拦截器等Spring特性,例如给CatAction配置方法前拦截器和返回后拦截器

catBeforeInterceptor

catAfterInterceptor

整合Struts 2

Spring整合Struts 2需要struts2-spring-2.011.jar包

public class CatAction{

private ICatService catService;

private Cat cat;

//setter、getter略

public String list(){

catService.listCats();

return "list";

}

public String add(){

catService.createCat(cat);

return list();

}

}

struts.xml配置

除了正常的配置之外,还需要添加名为struts.objectFactory的常量,把值设为spring,表示该Action由Spring产生。然后把的class属性改为catAction,Struts2将会到Spring中寻找名为catAction的bean

{1}

/list.jsp

/list.jsp

Spring配置

web.xml配置

contextConfigLocation

/WEB-INF/classes/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

Struts2

org.apache.struts2.dispatcher.FilterDispatcher

Struts2

/*

总结

以上就是本文关于Spring之WEB模块配置详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。

参考:

浅谈Springmvc中的页面跳转问题

Spring AOP入门Demo分享

Spring框架web项目实战全代码分享


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java基于递归和循环两种方式实现未知维度集合的笛卡尔积算法示例
下一篇:浅谈 vue 中的 watcher
相关文章

 发表评论

暂时没有评论,来抢沙发吧~