以下内容是基于导入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. python列表的通用操作

    #'+'和'*'#+可以将两个列表拼接为一个列表my_list = [1,2,3]+[4,5,6]#*可以将列表重复指定的次数my_list = [1,2,3]*5 print(my_list) #创 ...

  2. Linux--ps及top、ls命令

    day8  ps系统管理命令 ps是强大的后台进程检测命令 格式:ps [options] [--help] 选项参数: 1.-a :显示所有进程,包括PID等,包括其他用户运行的程序 2.-ef:显 ...

  3. Python学习:17.Python面向对象(四、属性(特性),成员修饰符,类的特殊成员)

    一.属性(特性) 普通方法去执行的时候,后面需要加括号,特性方法执行的时候和静态字段一样不需要不需要加括号. 特性方法不和字段同名. 特性方法不能传参数. 在我们定义数据库字段类的时候,往往需要对其中 ...

  4. Testing for diversifying selection for two clades with a background clade

    在利用branch-site检测趋同进化的时候 .可以将各个趋同进化枝分别进行检测,分析的时候不去除某一趋同枝系 .在分析的时候,需要去除其他趋同枝系的影响 I have sequences of a ...

  5. 脱离matlab运行可执行程序的步骤

    MCR是由matlab的运行环境,占用不到600M的对于用不同matlab版本生成的exe文件,MCR版本也会有不同,因此,在程序打包时,最好将相应版本的MCR一起打包.MCR环境的设置文件存放目录如 ...

  6. Linux入门第四天——shell基础

    一.shell概述 1.概述 命令行解释器(壳,也就是我们的操作界面),计算机只认识0101的二进制,我们需要通过ASCII表来进行翻译 较为官方的解释是: Shell 是一个用 C 语言编写的程序, ...

  7. 推荐一个学习Flex chart的好网站

    推荐一个学习Flex chart的好网站 2013-03-04 14:16:56|  分类: Flex |  标签: |字号大中小 订阅     推荐一个学习Flex chart的好网站 最近在做一个 ...

  8. 一个servlet如何处理多个请求

    页面1:表单的action=login?method=login 页面2:表单的action=login?method=insert ..... 然后通过method的值采用不同方法进行处理. 如下 ...

  9. 考研编程练习----Kruskal

    #include <stdio.h> #include <stdlib.h>   #define MAX 100   /* 定义边(x,y),权为w */ typedef st ...

  10. Matplotlib API汉化

    Pyplot API 示例汇总:https://matplotlib.org/gallery/index.html#api-examples 该matplotlib.pyplot模块包含的功能允许您快 ...