亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

總結(jié)Java的Struts框架的異常處理方法

 更新時間:2015年12月03日 15:11:27   作者:woshixuye  
這篇文章主要介紹了Java的Struts框架的異常處理方法,Struts是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下

Struts提供了一個更簡單的方式來處理未捕獲的異常,并將用戶重定向到一個專門的錯誤頁面。您可以輕松地Struts配置到不同的異常有不同的錯誤頁面。

Struts的異常處理所使用的“exception”攔截容易?!癳xception”攔截器作為默認的棧的一部分,所以不必做任何額外的配置。它可為準備使用的盒。讓我們看到了一個簡單的Hello World示例進行一些修改在HelloWorldAction.java文件。在這里,我們特意推出了一個空指針異常在我們HelloWorldAction動作代碼。

package com.yiibai.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport{
 private String name;

 public String execute(){
  String x = null;
  x = x.substring(0);
  return SUCCESS;
 }
 
 public String getName() {
  return name;
 }

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

讓我們 helloWorld.jsp保持內(nèi)容如下:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
 Hello World, <s:property value="name"/>
</body>
</html>

以下是內(nèi)容index.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>
<title>Hello World</title>
</head>
<body>
 <h1>Hello World From Struts2</h1>
 <form action="hello">
  <label for="name">Please enter your name</label><br/>
  <input type="text" name="name"/>
  <input type="submit" value="Say Hello"/>
 </form>
</body>
</html>

struts.xml 應(yīng)該像這樣:

<?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>
<constant name="struts.devMode" value="true" />
 <package name="helloworld" extends="struts-default">
  
  <action name="hello" 
   class="com.yiibai.struts2.HelloWorldAction" 
   method="execute">
   <result name="success">/HelloWorld.jsp</result>
  </action>

 </package>
</struts>

現(xiàn)在右擊項目名稱,并單擊Export > WAR File創(chuàng)建一個WAR文件。然后部署此WAR在Tomcat的webapps目錄下。最后,啟動Tomcat 服務(wù)器和嘗試訪問URL http://localhost:8080/HelloWorldStruts2/index.jsp。這會給出以下畫面:

2015123150544471.jpg (560×285)

輸入一個值“Struts2”,并提交頁面。應(yīng)該看到以下頁面:

2015123150611598.jpg (560×441)

在上面的例子所示,默認的異常攔截器做了非常出色的處理異?!,F(xiàn)在,讓我們創(chuàng)建一個專用的錯誤頁面,我們的例外。創(chuàng)建一個文件名為error.jsp 如以下內(nèi)容:

<%@ 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>
<title></title>
</head>
<body>
 This is my custom error page
</body>
</html>

Let us now configure Struts to use this this error page in case of an exception. Let us modify thestruts.xml as follows:

<?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>
<constant name="struts.devMode" value="true" />
 <package name="helloworld" extends="struts-default">
 
  <action name="hello" 
   class="com.yiibai.struts2.HelloWorldAction" 
   method="execute">
   <exception-mapping exception="java.lang.NullPointerException"
   result="error" />
   <result name="success">/HelloWorld.jsp</result>
   <result name="error">/Error.jsp</result>
  </action>
   </package>
</struts>

在上面的例子所示,現(xiàn)在我們已經(jīng)配置 Struts使用專用error.jsp的NullPointerException異常。如果現(xiàn)在重新運行該程序,現(xiàn)在看到下面的輸出:

2015123151019132.jpg (560×263)


根據(jù)<exception-mapping…../>元素出現(xiàn)位置的不同,異常映射又可分為兩種:
局部異常映射:將<exception-mapping… />元素作為<action…/>元素的子元素配置;
全局異常映射:將<exception-mapping… />元素作為<global-exception-mappings… />元素的子元素配置;
 
全局異常映射對所有的Action都有效,但局部異常映射僅對該異常映射所在的Action有效。
如果局部異常映射和全局異常映射配置了同一個異常類型,在<action…./>元素內(nèi)的局部異常映射將覆蓋全局異常映射。

Struts.xml

 <package name="ssh2" extends="struts-default">
  <global-results>
   <result name="sql">/exception.jsp</result>
   <result name="root">/exception.jsp</result>
  </global-results>
  <global-exception-mappings>
   <exception-mapping exception="java.sql.SQLException" result="sql"/>
   <exception-mapping exception="java.lang.Exception" result="root"/>
  </global-exception-mappings>
  <action name="login" class="loginAction">
   <result>/welcome.jsp</result>
   <result name="nullPointer">/nullPointer.jsp</result>
   <exception-mapping exception="java.lang.NullPointerException" result="nullPointer"/>
  </action>
 </package>

Action

 public class loginAction extends ActionSupport
 {
  public String add() throws SQLException
  {
    return "toadd";
  }
 }

 
有異常往外拋即可。你也可以在方法里面拋,比如throw SQLException。
 
我們可以使用Struts2的標簽輸出異常信息:
輸出異常的message屬性信息:<s:property value="exception.message" />
輸出異常堆棧信息:<s:property value="exceptionStack" />。
 

有了處理系統(tǒng)異常的基礎(chǔ),我們來看一看自定義異常:

package com.exception ;
public class MyException extends Exception 
{
 
 private String message;
 
 public MyException(String message)
 {
   super(message);
   this.message = message ;
 }
 public String getMessage() {
   return message;
 }
 public void setMessage(String message) {
   this.message = message;
 }
}

public String execute() throws Exception
{
 if(!"hello".equals(usename) || !"world".equals(password))
 {
    throw new MyException("用戶名或密碼錯誤,您發(fā)現(xiàn)了吧!");
 }
 return "success" ;
}

在action配置中的異常處理
    

<struts>
 <package name="struts2" extends="struts-default">  
  <action name="login" class="com.struts2.LoginAction">
   <exception-mapping result="myex" exception="com.exception.MyException">   
   </exception-mapping>
   <result name="myex">/error.jsp</result>
   <result name="success">/result.jsp</result>
  </action>
 </package> 
</struts>

 
在全局配置中的異常處理

<struts>
 <package name="struts2" extends="struts-default">
  <global-results>
   <result name="myexception1">/error.jsp</result>
  </global-results>
  
  <global-exception-mappings>
   <exception-mapping result="myexception1"
    exception="com.exception.MyException">   
   </exception-mapping>
  </global-exception-mappings>
  
  <action name="login" class="com.struts2.LoginAction">
   <result name="success">/result.jsp</result>
  </action>
 </package>
</struts>

 
錯誤頁面error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%> 
<html>
 <body>
 <!-- 這個exception 是 exception="com.exception.MyException" -->
 <s:property value="exception.message"/>
 </body>
</html>

 

總結(jié)
局部異常處理比全局異常處理高,并且可覆蓋全局異常處理,如果定義了全局異常映射,那么會對所有的Action生效,反之定義了局部異常映射則會對當前Action生效,
如果在全局區(qū)域和局部區(qū)域定義了相同的異常映射,首先去局部異常區(qū)域找result結(jié)果頁面,如果找到了,則直接跳轉(zhuǎn)到錯誤結(jié)果頁面,不管全局有沒有相同的結(jié)果,都被局部所覆蓋,如果在局部區(qū)域沒找到,則去全局區(qū)域找。

相關(guān)文章

  • javaweb Servlet開發(fā)總結(jié)(一)

    javaweb Servlet開發(fā)總結(jié)(一)

    Servlet是sun公司提供的一門用于開發(fā)動態(tài)web資源的技術(shù)。這篇文章主要介紹了javaweb Servlet開發(fā)的第一篇,感興趣的小伙伴們可以參考一下
    2016-05-05
  • java實現(xiàn)快速排序的方法

    java實現(xiàn)快速排序的方法

    這篇文章主要介紹了java實現(xiàn)快速排序的方法,涉及java排序的相關(guān)操作技巧,需要的朋友可以參考下
    2015-05-05
  • Eclipse中引入com.sun.image.codec.jpeg包報錯的完美解決辦法

    Eclipse中引入com.sun.image.codec.jpeg包報錯的完美解決辦法

    Java開發(fā)中對圖片的操作需要引入 com.sun.image.codec.jpeg,但有時引入這個包會報錯,利用下面的操作可以完成解決這個問題
    2018-02-02
  • Mybatis實現(xiàn)分頁的注意點

    Mybatis實現(xiàn)分頁的注意點

    Mybatis提供了強大的分頁攔截實現(xiàn),可以完美的實現(xiàn)分功能。下面小編給大家分享小編在使用攔截器給mybatis進行分頁所遇到的問題及注意點,需要的朋友一起看看吧
    2017-07-07
  • Java 8函數(shù)式接口Function BiFunction DoubleFunction區(qū)別

    Java 8函數(shù)式接口Function BiFunction DoubleFunction

    這篇文章主要為大家介紹了Java 8函數(shù)式接口Function BiFunction DoubleFunction區(qū)別示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • Java concurrency之AtomicLong原子類_動力節(jié)點Java學(xué)院整理

    Java concurrency之AtomicLong原子類_動力節(jié)點Java學(xué)院整理

    AtomicLong是作用是對長整形進行原子操作。下面通過本文給大家介紹Java concurrency之AtomicLong原子類的相關(guān)知識,感興趣的朋友一起看看吧
    2017-06-06
  • SPFA 算法實例講解

    SPFA 算法實例講解

    下面小編就為大家?guī)硪黄猄PFA 算法實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • springboot之端口設(shè)置和contextpath的配置方式

    springboot之端口設(shè)置和contextpath的配置方式

    這篇文章主要介紹了springboot之端口設(shè)置和contextpath的配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 詳解Java中hashCode的作用

    詳解Java中hashCode的作用

    這篇文章主要介紹了詳解Java中hashCode的作用的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • SpringBoot MyBatis簡單快速入門例子

    SpringBoot MyBatis簡單快速入門例子

    MyBatis 是一款優(yōu)秀的持久層框架,它支持自定義 SQL、存儲過程以及高級映射。這篇文章主要介紹了SpringBoot MyBatis快速入門-簡單例子,需要的朋友可以參考下
    2021-07-07

最新評論