Introduction

1.Struts背后的运行机制

a.一个客户端在浏览器地址栏中输入了一个URL地址:http://localhost:8080/struts2/hell,当他输入了这个URL地址之后,这个URL地址的请求会通过HTTP协议发送给我们的Tomcat,Tomcat收到这个请求之后,它看客户请求的是哪个Web Application,然后找到客户请求的Web Application(eg:struts2_0100_introduction),再把该Web Application交给对应的程序处理,既然是这个Web Application, Tomcat必然要读这个Web Application对应的web.xml文件,来看它是怎么配置的,这时候Tomcat就会发现一个配置,这个配置就是<filter>和<filter-mapping>,具体如下:

 

<filter>
     <filter-name>struts2</filter-name>
     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
     <filter-name>struts2</filter-name>
     <!--对于Struts2,这里永远用(/*)-->
     <url-pattern>/*</url-pattern>
</filter-mapping>

 

 

b.在web.xml中配置了一个filter,这个filter会过滤所有URL地址(/*),所以我们在浏览器地址栏中输入:http://localhost:8080/struts2/hell 时,这个地址就是被web.xml配置文件中org.apache.struts2.

dispatcher.ng.filter.StrutsPrepareAndExecuteFilter产生的对象接受到,它接受到后,①首先看你的struts-config.xml文件中的namespace配置,即和http://localhost:8080/struts2/hell 中的struts2后面的"/"相对应。

②接下来tomcat就会在struts-config.xml中查namespace="/"里面的是不是有一个名为hell的action,如果有,它就会找里面<result>是谁;

一个简单的struts2工程运行机制:


 
 

 

2.namespace决定了action的访问路径,默认为"", 可以接收所有路径的action。

namespace可以为 /  , 或者 /xxx , 或者 /xxx/yyy, 对应的action访问路径为 /index.action, /xxx/index.action, 或者/xxx/yyy/index.action

namespace最好也用模块来进行命名

 

3.修改了struts-config.xml后不能立刻给我反馈,需要重启服务器才能访问成功,怎样才能不需要重启服务器,就能由反馈呢?

在struts-config.xml中配置常量<constant name="struts.devMode" value="true" />

 

4.package作用:Java中package的作用是给我们的类打个包,避免类重名的情况。

在struts2中 package的作用一样,eg:我有2个action,一个叫index,另一个也叫index,怎么给它们作区分呢?可以把它们放到不同的package(包)中

 

5.凡是name为success的result(<result name="success" ></reuslt>),name都可以省略不写,即<result name="success" >/hell.jsp</reuslt>等价于<result>/hell.jsp</reuslt>

 

6.在没找到精确的package的情况下,全部交由namespace=“”的package来处理,也就是说namespace=""的package包括了其它package无法处理的东西;

你找一个action的时候,先到对应的namespace中查找,在对应的namespace中没找到,才会到namespace为空(即namespace=“”)的package中找action,如果都没找到就会报错。

 

7.当你建完JSP文件时,它的默认的编码是ISO-8859-1,你要想改它默认编码的话,是在什么地方?

Window / preferences / Web  /JSP file  修改 Encoding为UTF-8,以后建立的JSP页面都为UTF-8了

 

8.struts2每次访问必定new一个新的action,而struts1是每次访问可能是同一个action。

 

 

 

猜你喜欢

转载自weigang-gao.iteye.com/blog/2188889