ExceptionDemo

功能:

利用struts.xml 捕获异常

ExceptionDemo_第1张图片 不满足这三个值就报异常

1.web.xml

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
  <filter>
    <filter-name>struts2filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
  filter>
  <filter-mapping>
    <filter-name>struts2filter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>
web-app>

 

2.UserAction.java

package com.action;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
    private static final long serialVersionUID=1L;
    
    private String name;
    private String age;
    private String tel;
    
    public String getName() {
        return name;
    }



    public void setName(String name) {
        this.name = name;
    }



    public String getAge() {
        return age;
    }



    public void setAge(String age) {
        this.age = age;
    }



    public String getTel() {
        return tel;
    }



    public void setTel(String tel) {
        this.tel = tel;
    }



    public String execute() throws Exception{
        if(!getName().equals("Tom"))
        {
            throw new SecurityException("Wrong Name!");
        }
        else if(!getAge().equals("20"))
        {
            throw new Exception("Wrong Age!");
        }
        else if(!getTel().equals("12345"))
        {
            throw new java.sql.SQLException();
        }
        else
        {
            return SUCCESS;
        }
    }

}

 

3.SecurityException.java

package com.action;

public class SecurityException extends Exception{
    private static final long serialVersionUID=1L;
    
    public SecurityException()
    {
        super();
    }
    
    private String message;
    
    public SecurityException(String message)
    {
        this.message=message;
    }
    
    public String getMessage()
    {
        return message;
    }

}

 

4.index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <center>
        <s:form action="user" method="post" namespace="/">  
            <s:textfield label="Name" name="name" />
            <s:textfield label="Age" name="age" />
            <s:textfield label="Tel" name="tel" />
            <s:submit/>
        s:form>
    center>
body>
html>

 

5.struts.xml

xml version="1.0" encoding="UTF-8"?>
DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default" namespace="/" extends="struts-default">
        <global-results>
            <result name="Exception">/Exception.jspresult>
            <result name="SQLException">/SQLException.jspresult>
        global-results>
        
        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="Exception">exception-mapping>
            <exception-mapping exception="java.sql.SQLException" result="SQLException">exception-mapping>
        global-exception-mappings>
        
        <action name="user" class="com.action.UserAction">
            <exception-mapping exception="com.action.SecurityException" result="Login">exception-mapping>
            <result name="Login">/Login.jspresult>
            <result>/success.jspresult>
        action>
    package>
struts>

 

6.Exception.jsp/ SQLException.jsp/ Login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <center>
        <s:property value="exception.message" /><br/>            
        <s:property value="exceptionStack" /><br/>
    center>
body>
html>

 7.success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <center>
        <h2>success!!!h2>
    center>
body>
html>
    

 

转载于:https://www.cnblogs.com/c0liu/p/5497588.html

你可能感兴趣的:(ExceptionDemo)