码迷,mamicode.com
首页 > 其他好文 > 详细

ExceptionDemo

时间:2016-05-16 12:57:58      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

功能:

利用struts.xml 捕获异常

技术分享 不满足这三个值就报异常

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>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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-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 here</title>
</head>
<body>
    <center>
        <s:form action="user" method="post" namespace="/">  <!-- 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.jsp</result>
            <result name="SQLException">/SQLException.jsp</result>
        </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.jsp</result>
            <result>/success.jsp</result>
        </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 here</title>
</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 here</title>
</head>
<body>
    <center>
        <h2>success!!!</h2>
    </center>
</body>
</html>
    

 

ExceptionDemo

标签:

原文地址:http://www.cnblogs.com/c0liu/p/5497588.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!