以下内容是基于导入struts2-2.3.32.jar包来讲的
1.全局视图配置
xml标签:
<global-results> <result name="error">/error.jsp</result> </global-results>
1 package com.ronng.web.action;
2
3 public class TwoAction {
4 public String show(){
5 return "error";
6 }
7
8 public String look(){
9 return "error";
10 }
11 }
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="com.rong.web.action" namespace="/" extends="struts-default">
<!-- 全局视图。当action标签里面不包含result标签时,就会寻找全局的global-results结果视图 -->
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<action name="error1" class="com.ronng.web.action.TwoAction" method="show">
</action>
<action name="error2" class="com.ronng.web.action.TwoAction" method="look">
</action>
</package>
</struts>
访问路径:
http://localhost:8080/struts/error1
http://localhost:8080/struts/error2
返回的都是同一个error.jsp页面
2.全局的异常配置
1 package com.ronng.web.action; 2 3 public class TwoAction { 4 public String show() throws Exception{ 5 //抛出异常,无法返回"error"字符串 6 int number=1/0; 7 return "error"; 8 } 9 10 public String look()throws Exception{ 11 return "error"; 12 } 13 }
struts.xml配置是有顺序的:全局结果视图需要放在全局异常的前面
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="com.rong.web.action" namespace="/" extends="struts-default">
<!-- 全局视图。当action标签里面不包含result标签时,就会寻找全局的global-results结果视图 -->
<global-results>
<result name="error">/error.jsp</result>
<result name="exception">/exception.jsp</result>
</global-results>
<!-- 全局异常配置 。result对应全局视图的result的name-->
<global-exception-mappings>
<exception-mapping result="exception" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<action name="error1" class="com.ronng.web.action.TwoAction" method="show">
</action>
<action name="error2" class="com.ronng.web.action.TwoAction" method="look">
</action>
</package>
</struts>
以上的方法不能处理类似404的错误,若要处理404错误,则只需要修改web.xml配置文件
<error-page>标签配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>struts</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
</web-app>
3.最简单的action配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="com.rong.web.action" namespace="/" extends="struts-default">
<!-- 最简单的action
class 默认是struts-default.xml的
<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
默认执行的method 是 execute
方法默认的返回值是什么 "success"
-->
<!-- 对于最简单的action也就是
没有配置class的action要执行的哪个action,
可以不使用默认的,重新配置 ,但是依然会使用默认的 execute方法-->
<default-class-ref class="com.ronng.web.action.OneAction"></default-class-ref>
<action name="simple">
<result>/index.jsp</result>
</action>
</package>
</struts>
4.路径访问搜索顺序
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--
资源路径
http://localhost:8080/struts/search
http://localhost:8080/struts/aa/bb/cc/search
以上两种方式均可访问到同一页面
其中http://localhost:8080/struts是服务器以及项目信息
主要看这段:/aa/bb/cc/search
搜索顺序:没找到时继续往前找,由于search是action里面的name路径,所以最后才判断
先判断package,最后才判断action
namespace是/aa/bb/cc
namespace是/aa/bb
namespace是/aa
namespace是/
有这个/的命名空间了
那就在这个命名空间的action里找search
-->
<!-- namespace命名空间不写时,默认是斜杠"/" -->
<package name="com.rong.web.action" namespace="/" extends="struts-default">
<action name="search" class="com.ronng.web.action.OneAction">
<result>/index.jsp</result>
</action>
</package>
</struts>