【strut2笔记】helloworld演示

简单的helloworld演示

建立环境

使用Myeclipse  建立Webproject  

 

下载strut2.1包,包括jar包,文档,示例等。

http://struts.apache.org/  这里下

【strut2笔记】helloworld演示


【strut2笔记】helloworld演示


打开示例文件夹,看到五个war包,可以用RAR解压缩出来得到示例:

【strut2笔记】helloworld演示 

第一个包,strut2-blank-2.1.6 是最简单空白的包,解压缩后得到一个空白示例

【strut2笔记】helloworld演示 

建立基本框架

那么可以开始建立strut2 基本框架:

WEB-INF目录下classes目录下的struts的配置文件struts.xml拷到工程src目录下:

 

配置文件struts.xml会报错,不管它先

 

【strut2笔记】helloworld演示【strut2笔记】helloworld演示 

下面拷贝jar包类库,WEB-INF\lib目录下有九个包,去掉不用的两个:junitspring-test包。其他都拷到工程lib

【strut2笔记】helloworld演示 

下面弄strut2 xml filter过滤器配置,notepad打开WEB-INF\web.xml  复制下面的filter到工程中的web.xml中:

【strut2笔记】helloworld演示 

仔细观察上面的filter配置,可以看到有个ng,这是next generation的意思,就是下一代,也就是strut2.1 ,先前还有strut2.0的 ,不要下错了帮助文档,两者有区别

 

对于所有的strut配置,上面url-pattern 标签中的路径标准写法是/* 不建议改。意思是过滤所有的url

建立自己的配置

打开报错的 配置文件struts.xml <</span>struts> 标签中的东西注释掉先,这样就不会报错了,然后我们自己来写一个配置:

首先是设置开发模式,这样就可以边修改边查看,如果不设置开发模式,那么改了后还要重启服务器部署。

 

  •     <</span>constant name="struts.devMode" value="true" />

    然后是包和包中的action

 

  •     <</span>package name="default" namespace="/" extends="struts-default">
  •         <</span>action name="hello">
  •             <</span>result >
  •              /hello.jsp
  •             </</span>result>
  •         </</span>action>
  •     </</span>package>    

上面的东西都是放在strut标签中的。

 

namespace就是要运行的JSP所在路径,这里是根目录,把index.jsp改成hello.jsp

action name="hello" 属性就是URL访问路径,可以访问hello,也可以访问hello.action

【strut2笔记】helloworld演示【strut2笔记】helloworld演示 

 

 

 

其他注意

 

除了上面的开发模式要注意设置外,还有:

 

当我们想看jar包的源码的时候,打开看不到,怎么办

 

这样:

先找到要看的那个jar包,右键 -- properties --  java sourse attachment ,路径在:

解压出来的full包下:struts-2.3.15/src/core/src/main/java 确定就可以了。

【strut2笔记】helloworld演示 

这样,打开下面的class就可以看到反编译的源码了:

【strut2笔记】helloworld演示【strut2笔记】helloworld演示 

当我们想关联这个jar包的帮助文档的时候

我们设置这个jar包的帮助文档路径:

找到要看的那个jar包,右键 -- properties --  javadoc location ,路径:

struts-2.3.15/doc/ 路径下相对应名字的文档文件夹,就可以了,以后选择某个引用包下的方法,按下F1就可以调出帮助文档。

 

关于XMLdtd文件 引用的问题

如果使用url的模式比如:"http://struts.apache.org/dtds/struts-2.3.dtd"> 这样,在没有网的时候,dtd就打不开了,也没法在我们输入<</font>的时候给我们提示了,所以我们使用本地dtd的方式

首先找到DTD文件,可以网上下,也可以找jar包,解压开来找到:

【strut2笔记】helloworld演示【strut2笔记】helloworld演示 

window--preference--搜索catalog 找到XML catalog   ADD添加一个dtd 路径选择上面的,当然也可以复制dtd出来放到任意地方,设置路径。

当然也可以这样使用:修改直接DTD位置

DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"/WEB-INF/classes/struts-2.3.dtd">

然后将DTD放在WebRoot\WEB-INF\classes下就好

【strut2笔记】helloworld演示【strut2笔记】helloworld演示 

这样,当我们在XML中输入<</font>标签时,按下alt+/ 就会弹出提示:

【strut2笔记】helloworld演示 

 

Helloworld strut2 运作图:

【strut2笔记】helloworld演示 

Struct2运行原理
接收

当用户访问网页:http://localhost:8080/Strut2_learning/hello 的时候,tomcat接受到请求,会查看配置文件web.xml,然后看到里面的filter 

 

  •   <</span>filter>
  •     <</span>filter-name>struts2</</span>filter-name>     <</span>filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</</span>filter-class>
  •     </</span>filter>
  •   <</span>filter-mapping>
  •     <</span>filter-name>struts2</</span>filter-name>
  •     <</span>url-pattern>/*</</span>url-pattern>
  •   </</span>filter-mapping>

然后这个请求就被org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter这个过滤器接受了,首先它使用<</span>url-pattern>/*</</span>url-pattern>来过滤所有的URL地址,所以当地址后面的是hello的时候,hello就会被上面的这个长长的StrutsPrepareAndExecuteFilter接收到。

 

查路径

接收到以后,过滤器首先查看访问的namespace ,也就是路径中action所属的位置,namespace 与访问路径一一对应,过滤器会去查struct.xml文件中对应于namespacepackage,可以看到struct.xml文件中:

 

  •  <</span>package name="default" namespace="/" extends="struts-default">
  •         <</span>action name="hello">
  •             <</span>result >
  •              /hello.jsp
  •             </</span>result>
  •         </</span>action>
  • </</span>package>    

namespace 是“/”。对上了namespace就继续查actionname,没对上就返回到web.xml去交给tomcat处理。

 

PS:注意,namespace默认不填的时候是空,也就是不写的时候,namespace= 。这时候,任意路径都可以访问这个package中的action

访问顺序是:先看packagenamespace对不对得上,对的上就找action对不对得上,对上了就看action有没有class属性,没有的话直接返回result,有的话还要看对应的classexecute方法的返回串对不对得上resultname。对的上就返回result中的东西,对不上就异常了。

action

查到路径后呢,就到这个package 中找action对应<</span>result > <</span>result >可以有多个,名字不同。

这时候就找到了对应的 hello.jsp

然后就反馈/hello.jsp给我们。

 

总的来说

 

1)就是访问者将访问的东西交给服务器

2)服务器根据web.xml配置找到一个Strut2filter ,给任务给filter

3)filter根据strut.xml配置找到result ,返回给客户端

4)strut2 总的来说 是把请求和视图分开


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值