以下内容是基于导入struts2-2.3.32.jar包来讲的

1.全局视图配置

xml标签:
<global-results>
<result name="error">/error.jsp</result>
</global-results>
 package com.ronng.web.action;

 public class TwoAction {
public String show(){
return "error";
} public String look(){
return "error";
}
}
<?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.全局的异常配置

 package com.ronng.web.action;

 public class TwoAction {
public String show() throws Exception{
//抛出异常,无法返回"error"字符串
int number=1/0;
return "error";
} public String look()throws Exception{
return "error";
}
}

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>

5.获取域对象以及ActionContext上下文

A.第一种方式(通过ServletActionContext获取)

 package com.ronng.web.action;

 import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext; import org.apache.struts2.ServletActionContext; /**
* 普通类获取域对象
* 通过ServletActionContext获取
* @author Rong
*
*/
public class TwoAction {
public String show() throws Exception{
//使用原生API,耦合度高,很少使用
PageContext pageContext = ServletActionContext.getPageContext();
//可通过pageContext获取其余域对象
// ServletRequest request = pageContext.getRequest();
// HttpSession session = pageContext.getSession();
// ServletContext application = pageContext.getServletContext();
HttpServletRequest request = ServletActionContext.getRequest();
//获取前台传过来的值
String parameter = request.getParameter("name");
System.out.println(parameter);
HttpSession session = request.getSession();
ServletContext application = ServletActionContext.getServletContext();
request.setAttribute("name", "rong");
session.setAttribute("name", "jie");
application.setAttribute("name", "long");
return "success";
}
}
<body>
通用的域: ${name }<br/>
request域:${requestScope.name }<br/>
session域:${sessionScope.name }<br/>
application域:${applicationScope.name }<br/>
</body>
<?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 name="search" class="com.ronng.web.action.TwoAction" method="show">
<result>/two.jsp</result>
</action>
</package>
</struts>

B.第二种方式(ActionContext上下文的get方式)

 package com.ronng.web.action;

 import org.apache.struts2.dispatcher.ApplicationMap;
import org.apache.struts2.dispatcher.RequestMap;
import org.apache.struts2.dispatcher.SessionMap; import com.opensymphony.xwork2.ActionContext; /**
* 普通类获取域对象
* 通过ActionContext上下文get方式获取
* 松耦合
* @author Rong
*/
public class TwoAction {
public String show() throws Exception{
//获取ActionContext上下文。ActionContext在请求最开始的时候封装了所有数据。
ActionContext actionContext = ActionContext.getContext();
//通过get方法获取域对象。其中get里面的字符串是唯一指定的。
RequestMap requestMap = (RequestMap) actionContext.get("request");
SessionMap<String, Object> sessionMap = (SessionMap<String, Object>) actionContext.get("session");
ApplicationMap applicationMap = (ApplicationMap) actionContext.get("application");
requestMap.put("name", "rong");
sessionMap.put("name", "jie");
applicationMap.put("name", "long");
return "success";
}
}

C.第三种方式(ActionContext上下文的getXxx方式)

 package com.ronng.web.action;

 import java.util.Map;

 import com.opensymphony.xwork2.ActionContext;

 /**
* 普通类获取域对象
* 通过ActionContext上下文getXxx方式获取
* 松耦合
* @author Rong
*/
public class TwoAction {
public String show() throws Exception{
//获取ActionContext上下文。ActionContext在请求最开始的时候封装了所有数据。
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> request = actionContext.getContextMap();
Map<String, Object> session = actionContext.getSession();
Map<String, Object> application = actionContext.getApplication();
request.put("name", "rong");
session.put("name", "jie");
application.put("name", "long");
return "success";
}
}

D.第四种方式(实现XxxAware接口)

根据需要实现不同域的接口

 package com.ronng.web.action;

 import java.util.Map;

 import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware; /**
* 实现接口方式获取域对象
* 松耦合
* @author Rong
* 这里列举了实现多种域接口,实际根据自己需要实现接口
*/
public class TwoAction implements RequestAware,SessionAware,ApplicationAware{
//需要自己去声明成员变量(域对象)
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> applicaton;
public String show() throws Exception{
request.put("name", "r");
session.put("name", "j");
applicaton.put("name", "l");
return "success";
}
//域对象赋值
@Override
public void setApplication(Map<String, Object> arg0) {
this.applicaton=arg0;
} @Override
public void setSession(Map<String, Object> arg0) {
this.session=arg0;
} @Override
public void setRequest(Map<String, Object> arg0) {
this.request=arg0;
}
}

Struts2(三)的更多相关文章

  1. Struts2(三)——数据在框架中的数据流转问题

    一款软件,无在乎对数据的处理.而B/S软件,一般都是用户通过浏览器客户端输入数据,传递到服务器,服务器进行相关处理,然后返回到指定的页面,进行相关显示,完成相关功能.这篇博客重点简述一下Struts2 ...

  2. Struts2 (三)

    1 Struts2的拦截器 Struts2拦截器在访问某个Action方法之前或之后实施拦截,拦截器是可插拔的,拦截器是AOP的一种实现. Struts2拦截器栈:将拦截器按一定顺序联结成一条链,在访 ...

  3. 框架学习之Struts2(三)---OGNL和值栈

    一.OGNL概述 1.1OGNL是对象图导航语言(Object-Graph Navigation Languaged)的缩写,他是一种功能强大的表达式语言,通过简单一致的表达式语法,可以存取Java对 ...

  4. struts2(三) 输入校验和拦截器

    前面知道了struts2的架构图和struts2的自动封装表单参数和数据类型自动转换,今天来学struts2的第三第四个东西,输入校验和拦截器, --WH 一.输入校验 在以前我们写一个登录页面时,并 ...

  5. Struts2(三)配置详解

    一.概述 Struts2提供了多种可选的配置文件形式. 其中,struts-default.xml和default.properties是框架级别的配置文件,这两个文件在Struts的核心JAR包中, ...

  6. Struts2 (三) — OGNL与值栈

    一.OGNL表达式 1.概述 1.1什么是OGNL ​ OGNL是Object-Graph Navigation Language的缩写,俗称对象图导航语言. 它是一种功能强大的表达式语言,通过它简单 ...

  7. Struts2(三):新建Struts2工程

    下载的struts2xx-all.zip包对搭建项目的作用 一般情况下,我们下载一个Struts2的full包时,并不知道新建过程中需要哪些包,那么这时我们可以从下载的包中解压出的目录\apps\st ...

  8. Struts2 三、指定Struts2处理的请求后缀

    Action的请求通常情况下默认为以.action结尾,例如:http://localhost:9000/Struts2/hello/helloAction_sayHello.action    .a ...

  9. 浅谈Struts2(三)

    一.Struts2收集client的参数 核心思路: <form method="post" action="XXXX"> <input ty ...

  10. 深入struts2(三)---工作机制和运行流程图

    1     工作原理 1.1     体系架构 图2.1 struts2.0体系架构图 1.2     工作机制 针对上节体系架构图,以下分步说明运行流程 Ø  client初始化一个指向Servle ...

随机推荐

  1. docker入门——安装(CentOS)、镜像、容器

    Docker简介 什么是docker 官方解释: Docker is the company driving the container movement and the only container ...

  2. C++的一些关键字用法

    const 这个关键字真是太常用了, 所以干脆总结一下. int const a = 8; //定义一个int常量a, 不能再给a赋值了 const int a = 8; //和上面一样 int co ...

  3. jenkins+maven+docker集成java发布(二)#远程发布

    jenkins+maven+docker集成java发布(一)中写了在Jenkins服务器自动部署业务,那需要将java项目部署到其他服务器怎么操作 这里需要依赖插件Publish Over SSH ...

  4. 基于Doxygen_C语言代码文档一键生成的记录与规范(嵌入式适用)

    下位机代码格式规范整合记录 注册 doxygen 账号获取doxygen 的 *.exe 执行文件 https://pan.baidu.com/s/1MF5v-Ts80BysmZtXSqONmg 提取 ...

  5. STM32F103C8T6、STM32F103ZET6工程模板

    STM32F103C8T6工程模板,推荐使用以下最新版本 最终版 2018 7 16  https://pan.baidu.com/s/1lIdZ2awus_quVu332RvJ6Q https:// ...

  6. hadoop errors

    1.taskTracker和jobTracker 启动失败 2011-01-05 12:44:42,144 ERROR org.apache.hadoop.mapred.TaskTracker: Ca ...

  7. flex 自定义tooltip

    //flex用例网址 http://thanksmister.com/2012/01/18/flex-chart-datatip-renderer/ http://help.adobe.com/en_ ...

  8. echarts x轴文字换行显示

    xAxis : [ { splitLine:{show:false}, type : 'category', data : ['社交人际','沟通交流','心理认知','游戏玩耍','大小运动','生 ...

  9. [并发并行]_[线程模型]_[Pthread线程使用模型之二 工作组work crew]

    Pthread线程使用模型之二工作组(Work crew) 场景 1.一些耗时的任务,比如分析多个类型的数据, 是独立的任务, 并不像 pipeline那样有序的依赖关系, 这时候pipeline就显 ...

  10. 监听Entity Framework生成的Sql语句

               Entity Framework为我们提供了很大的方便,但有时候,我们想看看EF生成的Sql语句到底是怎样的,一种方式是我们可以启用Sql Server Profer工具.今天介 ...