一、在struts.xml文件中配置Action执行后的跳转资源
Action执行后的跳转资源位置是通过<result>标签指定的,在指定资源位置时可以用绝对路径,也可以使用相对路径。
绝对路径是以"/"开头,"/"代表当前Web应用的上下文路径;
相对路径不以"/"开头,相对于当前执行的action的路径,也就是namespace指定的路径。
例如:
<package name="default" extends="struts-default" namespace="/admin">
<action name="login" class="com.cn.LoginAction">
<result>success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
假设当前Web应用的上下文路径是/bbs,那么执行/bbs/admin/login.action成功后,会跳转到
/bbs/admin/success.jsp;如果执行结果为失败,会跳转到
/bbs/error.jsp
二、jsp文件中的路径(了解)
问题背景:
在Struts2框架中,web.xml文件的配置如下:
<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>
<url-pattern>/*</url-pattern>
</filter-mapping>
struts.xml文件配置如下:
<struts>
<constant name="struts.devMode" value="true"/>
<package name="path" extends="struts-default" namespace="/path">
<action name="path" class="com.cn.PathAction">
<result name="path">/path.jsp</result>
</action>
</package>
</struts>
Action中execute方法的返回值为"path"
当Action执行后会跳转至path.jsp页面,当在一个jsp页面(如path.jsp)中指定一个链接(如<a href="index.jsp">index.jsp</a>)时,此时path.jsp和index.jsp在同一目录(Web-INF)下,当在path.jsp页面中点击index.jsp链接时,会报404错误(找不到index.jsp),此时URL地址栏为:http://localhost:8080/webapp/path/index.jsp。这就说明jsp页面中写的相对路径并不是以当前jsp文件所在目录为根目录,而是以action的路径来确定的。