Struts2学习第八课 声明式异常处理

Posted alittlecomputer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2学习第八课 声明式异常处理相关的知识,希望对你有一定的参考价值。

异常处理:exception-mapping元素

exception-mapping元素:配置当前的action的声明式异常处理

exception-mapping元素有两个属性:

--exception:指定需要铺货的异常类型

--result:指定一个响应结果,该结果将在捕获到指定异常时被执行,既可以来自当前action的声明也可以来自global-results声明。

可以通过global-exception-mappings元素为应用程序提供一个全局性的异常补货映射,但是在global-exception-mapping元素下声明的任何exception-mapping元素只能在global-results元素下声明的某个result元素。

声明式异常处理机制由ExceptionMappingInteceptor拦截器负责处理,当某个exception-mapping元素声明的异常被捕获到时,ExceptionMappingInterceptor拦截器就会向ValueStack中添加两个对象:

--exception:表示被捕获异常的Exception对象

--exceptionStack:包含着呗捕获异常的栈

可以在视图上通过<s:property/>标签显示异常消息

看代码:

package logan.struts.study;

import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;

public class Product implements RequestAware,SessionAware{
    
    private Integer productId;
    private String productName;
    private String productDesc;
    
    private double productPrice;

    public Integer getProductId() {
        return productId;
    }

    public void setProductId(Integer productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductDesc() {
        return productDesc;
    }

    public void setProductDesc(String productDesc) {
        this.productDesc = productDesc;
    }

    public double getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(double productPrice) {
        this.productPrice = productPrice;
    }

    @Override
    public String toString() {
        return "Product [productId=" + productId + ", productName=" + productName + ", productDesc=" + productDesc
                + ", productPrice=" + productPrice + "]";
    }
    
    public String save(){
        System.out.println("save: " + this);
        
        ValueStack valueStack = ActionContext.getContext().getValueStack();
        Test test = new Test();
        
        test.setProductDesc("AABBCCDD");
        test.setProductName("ABCD");
        valueStack.push(test);
        
        session.put("product", this);
        
        request.put("test", test);
        
        int i = 10/0;
        
        
        return "success";
    }
    
    private Map<String, Object> session;
    private Map<String, Object> request;

    @Override
    public void setSession(Map<String, Object> session) {
        // TODO Auto-generated method stub
        this.session = session;
        
    }

    @Override
    public void setRequest(Map<String, Object> request) {
        // TODO Auto-generated method stub
        this.request = request;
        
    }
}
package logan.struts.study;

public class Test {
    
    private String productName;
    private String productDesc;
    public String getProductName() {
        return productName;
    }
    public void setProductName(String productName) {
        this.productName = productName;
    }
    public String getProductDesc() {
        return productDesc;
    }
    public void setProductDesc(String productDesc) {
        this.productDesc = productDesc;
    }
    

}

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>

    <!-- 打开静态方法调用的限制 -->
    <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
    <package name="default" namespace="/" extends="struts-default">
        <action name="product-save" class="logan.struts.study.Product" method="save">
            <exception-mapping result="input" exception="java.lang.ArithmeticException"></exception-mapping>
            <result name="input">/input.jsp</result>
            <result>/details.jsp</result>
            
        </action>
        
        
    
    </package>
    
    
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>struts2-5</display-name>
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <welcome-file-list>
      <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

details.jsp

<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
        <s:debug></s:debug>
        productId:${productId }
        <br><br>
        productName:${productName }
        <br><br>
        productDesc:${productDesc }
        <br><br>
        productPrice:${productPrice }
        <br><br>
        productPrice:<s:property value="productPrice"/>
        <br><br>
        productName1:${sessionScope.product.productName }
        <br><br>
        productName1.1:<s:property value="#session.product.productName"/>
        <br><br>
        productName2:${requestScope.test.productName }
        <br><br>
        productName2.1:<s:property value="#request.test.productName"/>
        <br><br>
        <!-- 使用 OGNL 调用public 类的public类型的静态字段和静态方法 -->
        <s:property value="@java.lang.Math@PI"/>
        <br><br>
        <s:property value="@java.lang.Math@cos(0)"/>
        <br><br>
        <!-- 调用对象栈的方法为一个属性赋值 -->
        <s:property value="setProductName(\'logan\')"/>
        <br><br>
        <s:property value="productName"/>
        <br><br>
        <%
            String [] names = new String[]{"aa","bb","cc","dd"};
            request.setAttribute("names", names);
        %>
        length: <s:property value="#attr.names.length"/>
        <br><br>
        names[2]:<s:property value="#attr.names[2]"/>
        <br><br>
        <%
            Map<String,String> letters = new HashMap<String,String>();
            request.setAttribute("letters", letters);
            letters.put("AA", "a");
            letters.put("BB", "b");
            letters.put("CC", "c");
            
        %>
        <!-- 使用OGNL访问Map -->
        <s:property value="#attr.letters.size"/>
        <br><br>
        AA:<s:property value="#attr.letters[\'AA\']"/>
        
</body>
</html>

input.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <s:debug></s:debug>
    <br>
    <s:property value="exception"/>-${exception }
    <br>
    <s:property value="exception.message"/>-${exception.message }
    <br>

    <form action="product-save.action" method="post">
    ProductName:<input type="text" name="productName">
    <br><br>
    ProductDesc:<input type="text" name="productDesc">
    <br><br>
    ProductPrice:<input type="text" name="productPrice">
    <br><br>
    <input type="submit" value="Submit">
    <br><br>
    </form>

</body>
</html>

 

以上是关于Struts2学习第八课 声明式异常处理的主要内容,如果未能解决你的问题,请参考以下文章

Python第八课 异常处理

接口测试学习-python第八课(数据驱动测试)

struts2的异常处理机制

Struts2声明式异常处理

Python学习,第八课 - 函数

Golang✔️走进 Go 语言✔️ 第八课 函数