读书人

struts 1.3.2 module配备以及forward中

发布时间: 2012-09-07 10:38:15 作者: rapoo

struts 1.3.2 module配置以及forward中相对路径的使用
定义了module之后,我们的xml中的任何forward配置(global-forwards, action中的forward)中的path属性,(只要不设置contextRelative=true,默认为false),前面都会套上module的prefix(要求这个path的值以admin /打头)。比如我们在admin module中有一个forward,他的path是/jsp/index.jsp,那么,到了URL中就变成了http://<domain name>/<our webapp context name>/jsp/index.jsp了。其实这个行为就是在controller的配置中,forwardPattern的行为(forwardPattern的默认值是$M$P,除非我们手动修改这个配置,否则就是上面的那套规则)。

forward是服务器内部重定向,程序收到请求后重新定向到另一个程序,客户机并不知道,URL不改变;redirect则是服务器收到请求后发送一个状态头给客 户,客户将再请求一次,这里多了两次网络通信的来往,URL改变。
forward 会将 request state , bean 等等信息带往下一个 jsp
redirect 是送到 client 端后再一次 request , 所以资料不被保留.
使用 forward 你就可以用 getAttribute() 来取的前一个 jsp 所放入的 bean 等等资料示在浏览器地址栏中的URL一般不一样,而且后者以redirect时的参数(地址)为准。


ActionServlet配置


strutsConfig-manager.xml


既然有了Module,就存在module之间相互切换访问的问题了
<action-mappings>
<action path="/toModule"
type="org.apache.struts.actions.SwitchAction"/>
...
</action-mappings>
http://localhost:8080/toModule.do?prefix=/admin&page=/index.do

在使用forward的时候配置contextRelative=true
<global-forwards>
<forward name="toModuleB"
contextRelative="true"
path="/moduleB/index.do"
redirect="true"/>
...
</global-forwards>

<html:link module="/moduleB" path="/index.do"/>

读书人网 >软件架构设计

热点推荐