通向架构师的道路第二十二天万能框架spring四使用struts2.docx

上传人:b****4 文档编号:3455147 上传时间:2022-11-23 格式:DOCX 页数:42 大小:553.85KB
下载 相关 举报
通向架构师的道路第二十二天万能框架spring四使用struts2.docx_第1页
第1页 / 共42页
通向架构师的道路第二十二天万能框架spring四使用struts2.docx_第2页
第2页 / 共42页
通向架构师的道路第二十二天万能框架spring四使用struts2.docx_第3页
第3页 / 共42页
通向架构师的道路第二十二天万能框架spring四使用struts2.docx_第4页
第4页 / 共42页
通向架构师的道路第二十二天万能框架spring四使用struts2.docx_第5页
第5页 / 共42页
点击查看更多>>
下载资源
资源描述

通向架构师的道路第二十二天万能框架spring四使用struts2.docx

《通向架构师的道路第二十二天万能框架spring四使用struts2.docx》由会员分享,可在线阅读,更多相关《通向架构师的道路第二十二天万能框架spring四使用struts2.docx(42页珍藏版)》请在冰豆网上搜索。

通向架构师的道路第二十二天万能框架spring四使用struts2.docx

通向架构师的道路第二十二天万能框架spring四使用struts2

一、前言

SSH有了,现在我们要把我们的struts层从原来的1.3替换成struts2.x,引入了struts2.0后我们会发觉我们的代码和框架的变化还是不小的

二、Struts2的好处

1)在struts2的方法里,一切变量是线程安全的,而原有的struts1不是的;

2)在struts2中如果你声明了如下这样的代码:

   

privaterStringstudentName=””;

publicvoidsetStudentName(StringstudentName){

   this.studentName=studentName;

}

publicStringgetStudentName(){

   returnthis.studentName;

}

那么当你对这个studentName进行符值后,不需要再把它用request.setAttribute这样的形式把值带到页面中去了,相当于你可以省去在request中来回的setAttribute{…}getAttribute{…}的操作(有时由于忘记把一个listset 到request中去,经常导致一个页面就是不显示列表,对吧?

这样的事可以被极大程度上避免掉)。

3)更丰富且描述简单的页面标签,可以直接支持将一个Object和页面的进行绑定,如:

我在后台如果有一个StudentVO,这个StudentVO如下描述:

               privateStringstudentNo="";

               privateStringstudentName="";

 

               publicStringgetStudentNo(){

                               returnstudentNo;

               }

 

               publicvoidsetStudentNo(StringstudentNo){

                               this.studentNo=studentNo;

               }

 

               publicStringgetStudentName(){

                               returnstudentName;

               }

 

               publicvoidsetStudentName(StringstudentName){

                               this.studentName=studentName;

               }

于是我在前台jsp里可以直接这样使用我的标签和我这个VO中的某个字段进行绑定:

textfieldname="studentVO.studentName"size="24"maxlength="25"/>

4)原有在struts1中的formbean彻底消失,去而代之的是使用VO对象,一个strutsaction就是一个普通的类,只是它extendsActionSupport而己。

5)良好的注入机制,连session,request,response都可以注入了,因此你的一个action方法就是一个普通类方法,这样做的好处是极大化将servlet与我们的action进行解耦合。

试想如果是原有的struts1的action方法,我现在要改成swing的actionPerform,你是不是要把原有的action方法包括传参都要进行重构啊?

而现在有了struts2,由于连session,request,response都是被注入的,因此这个struts2的action方法可以直接重用。

Strtus2还有很多好处,这边不一一列举了,在struts2的官方文档和stepbystep等书中详细有说,我们这边主要以实战为主,讲述struts2怎么和spring进行整合并且能够开发我们的应用。

三、整合spring和struts2

我们还是用我们的Maven2。

Struts2变化很大,它是一个几乎被重写的框架,而不是一个“增强”的框架,它是继承自xwrok的框架并且在整个框架中全面使用了filter机制。

对于我们的maven的pom.xml文件来说,这个lib库的改动还是很大的。

甚至还会出现一些莫名奇妙的错误而其原因是因为lib库的版本不对或者是有冲突,为此笔者整理了一份ssh2的所有需要的jar的mavenpom.xml文件。

虽然,我会在后面把这个xml文件完整列出来但还是希望大家在一开始跟着我能够一步步走,对pom.xml文件和工程进行排错,这样你将对一些常用的框架的lib库有个比较熟悉的过程。

3.1延用原有的myssh工程中的pom.xml文件

我们新建一个maven的web工程-myssh2,并将原有的myssh工程中的pom.xml文件拷入工程中。

请确保你使用的jdk版本为version1.6.x。

3.2去除所有的struts1.3的依赖关系

打开这个pom.xml文件,把下面这段所有的关于struts1.3的依赖包全部去除。

               org.apache.struts

               struts-core

               1.3.10

               org.apache.struts

               struts-el

               1.3.10

               org.apache.struts

               struts-extras

               1.3.10

               org.apache.struts

               struts-faces

               1.3.10

               org.apache.struts

               struts-mailreader-dao

               1.3.10

               org.apache.struts

               struts-scripting

               1.3.10

               org.apache.struts

               struts-taglib

               1.3.10

               org.apache.struts

               struts-tiles

               1.3.10

3.3增加struts2的依赖包

我们把原有的struts1.3的依赖包去除后加入struts2的依赖包

               org.apache.struts

               struts2-spring-plugin

               2.3.1.2

               org.apache.struts

               struts2-core

               2.3.1.2

 

存盘后,此时maveneclipse插件会自动开始编译和下载相关的jar到你的本地maven的repository中,然后我们会发觉这个pom.xml文件出错了:

抛一个sun.tool.jar没有找到的错误。

道理很简单,因为该tool.jar其实已经存在在我们本地安装的jdk的lib目录下了,因此我们不需要这个包,但是maven是自动依赖的,你没有看到它在pom.xml文件中出现不代表这层依赖关系不存在。

因此我们需要做的是exclude这个包。

让我们在maveneclipse插件中打开这个pom.xml文件,切换到“DependencyHierarchy”视图,然后找到这个tool.jar文件,点左边这个list中的tools:

1.5.0然后右键选“ExcludeMavenArtifact”。

选[ok]按钮然后存盘。

我们可以看到这个pom.xml文件一切正常了,没有红色的“叉叉”了,我们切换到pom.xml视图,可以看到它其实做了这么一件事(注意红色标粗的语句):

               org.apache.struts

               struts2-spring-plugin

               2.3.1.2

               org.apache.struts

               struts2-core

               2.3.1.2

               

                               

                                               tools

                                               com.sun

                               

               

然后:

1)我们把原先ssh工程中的resources目录下所有的东西拷到myssh2工程的resources目录下;

2)我们把原先的ssh工程的java文件拷过来;

3)我们把原先的ssh工程的src/main/webapp目录下的文件也拷贝过来;

4)不要忘了把WEB-INF/web.xml文件和index.jsp文件也拷过来啊!

1)我们把原有的org.sky.ssh.student.form和org.sky.ssh.login.form删了;

2)我们把原有的service类中的一些需要传入StudentForm的方法的中的StudentForm改成

org.sky.ssh.vo.StudentVO,其内容如下:

packageorg.sky.ssh.vo;

 

importjava.io.Serializable;

 

publicclassStudentVOimplementsSerializable{

               privateStringstudentNo="";

               privateStringstudentName="";

 

               publicStringgetStudentNo(){

                               returnstudentNo;

               }

 

               publicvoidsetStudentNo(StringstudentNo){

                               this.studentNo=studentNo;

               }

 

               publicStringgetStudentName(){

                               returnstudentName;

               }

 

               publicvoidsetStudentName(StringstudentName){

                               this.studentName=studentName;

               }

}

3)把原有的两个action文件也删了吧(删了就删了,反正我们要用struts2来重写)

4)打开web.xml文件,把下面这些内容去掉

               

                               /WEB-INF/struts-bean.tld

                               /WEB-INF/struts-bean.tld

               

               

                               /WEB-INF/struts-html.tld

                               /WEB-INF/struts-html.tld

               

               

                               /WEB-INF/struts-logic.tld

                               /WEB-INF/struts-logic.tld

        

5)打开web.xml文件,把.do都改成.action

6)打开web.xml文件,把这些去掉

               action

               org.apache.struts.action.ActionServlet

               

                               config

                               /WEB-INF/struts-config.xml,

                                                           /WEB-INF/struts-config/login.xml,

                                                           /WEB-INF/struts-config/index.xml

                               

               

               

                               debug

                               3

               

               

                               detail

                               3

               

               2

7)打开web.xml文件,加入struts2的配置

               struts2

               org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

               struts2

               *.action

可以看到struts2框架是一个基于filter的框架,确保这个filter在所有工程中你自己定义的filter的最后面,要不然你自己定义的filter会被struts2给filter掉(这点一定要注意)。

最后别忘了把:

               exclude

               /jsp/login/login.jsp,

                                                         /login.action

               

这边的原有的/login.do改成/login.action哦。

 

 

确保工程编译没有任何问题,然后我们按照番外篇《第十九天》中的“四、如何让Maven构建的工程在eclipse里跑起来”对工程进行设置,使得工程可以在eclipse的tomcat中跑起来。

跑起来后直接抛出一堆的错,然后我们来看为什么

就是下面这个狗屁错。

其原因在于由于使用的struts2。

原有的这个cglib:

2.1.3,这个包对于spring3和hibernate3还有struts1.3来说没有任何问题,在遇到struts2时就冲突了,因此我们需要把这个包也给exclude掉

Exclude掉了后没有cglib包了,AOP类反射没法玩了,怎么办?

简单:

手工在pom.xml文件中添加一个cglib较新版本的包,如下:

               org.hibernate

               hibernate-entitymanager

               3.3.1.ga

               

                               

                                               cglib

                                               cglib

                               

               

              

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1