The handling mechanism of declarative exception in STRUTS2

Source: Internet
Author: User
Tags exception handling getmessage

See https://jadyer.github.io/for full edition




This is a Struts2.1.8.1 application, the code is as follows

First, Web.xml.

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <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>exceptiontest.jsp</ Welcome-file> </welcome-file-list> </web-app>

Then a exceptiontest.jsp page for entering a username and password to test the exception

<%@ page language= "java" pageencoding= "UTF-8"%> <%@ taglib prefix= "s" uri= "/struts-tags"%>

Result.jsp page that appears when both username and password are correct

<%@ page language= "java" pageencoding= "UTF-8"%>

usernameinvalid.jsp page that displays exception information when the user name is invalid

<%@ page language= "java" pageencoding= "UTF-8"%> <%@ taglib prefix= "s" uri= "/struts-tags"%>

passwordinvalid.jsp page that displays exception information when the password is invalid

<%@ page language= "java" pageencoding= "UTF-8"%> <%@ taglib prefix= "s" uri= "/struts-tags"%>

About STRUTS2 configuration file Struts.xml

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd "> <struts> <package name=" struts2.1 "extends=" Struts-default "> <global-results> <result name= "Passwordinvalid" >/passwordInvalid.jsp</result> </global-results> < global-exception-mappings> <!--<exception-mapping result= "Sqlinvalid" exception= "Java.lang.SQLException" />--> <exception-mapping result= "Passwordinvalid" exception= "Com.jadyer.exception.PasswordException" > </global-exception-mappings> <action name= "Exceptiontest" class= " Com.jadyer.action.ExceptionTestAction "> <exception-mapping result=" usernameinvalid "exception=" Com.jadyer.exception.UsernameException "/> <result name=" Usernameinvalid ">/usernameinvalid.jsp</" Result> <result name= "Success" >/result.jsp</result> </action> </package> </struts> <!--******************* "Struts2 declarative exception handling" **************************************************** --> <!--Struts2 itself can be seen as an empty container, where a lot of interceptors are placed to make it full, and the requests we send need to go through one interceptor--> <!-- and its declarative exception handling is implemented by the exception interceptor in the Defaultstack default interceptor--> <!--exception occurs when the exception interceptor catches the exception and then reads the information about the exception configuration from the configuration file. and related processing--> <!--******************* "Struts2 Abnormal Configuration" ******************************************************** --> <!--The configuration of exceptions, like struts1.x, has two types of exceptions, namely, global and local exceptions--> <!--and <exception-mapping/> The element has two required attributes, respectively, the result and exception--> <!--The Exception property represents the type of exception thrown in the program, and its value is the name of the package name plus class--> <!-- Once the program encounters an exception for the exception property, it will go to the view resource identified by the result property and then render to the user an interface that displays an exception--> <!--For example, when the action throws a Passwordexception exception, It first looks for the passwordexception exception configuration in the local exception configuration--> <!--The results are not found, and then it looks for the global exception configuration. When it finds passwordexception in the global exception configuration--> <!--It also gets a result named Passwordinvalid. But then it's not going directly to <global-results/> to match result--> <!--but to go back to the <action/> tag and look forResult is the element of passwordinvalid, and it turns out that there is no--> <!--at this point, it will look for results in <global-results/> with the name Passwordinvalid. Then go to the corresponding view resources--> <!--*********************************************************************************** -->

Custom Usernameexception Exception class

Package com.jadyer.exception; /** * Custom Usernameexception Exception class * @see custom exceptions in Java are inherited directly or indirectly java.lang.Exception * @see General custom exception models are defined as this class Suppresswarnings ("Serial") public class Usernameexception extends Exception {private String message; public usernameexce Ption (String message) {this.message = message;} public String GetMessage () {return message;} public void Setmessage (STR ing message) {this.message = message;}}

Custom Passowordexception Exception class

Package com.jadyer.exception; /** * Custom Passwordexception Exception class * * * @SuppressWarnings ("Serial") public class Passwordexception extends Exception {private S Tring message; Public passwordexception (String message) {super [message]; this.message = message;} public String GetMessage () {return me Ssage; public void Setmessage (String message) {this.message = message;}}

Finally, the Exceptiontestaction.java to handle abnormal distribution.

Package com.jadyer.action import com.jadyer.exception.PasswordException; import Com.jadyer.exception.UsernameException; Import Com.opensymphony.xwork2.ActionSupport; @SuppressWarnings ("Serial") public class Exceptiontestaction extends Actionsupport {private String username; private STR ing password; /* Two setter and getter//** * Throw any type of exception in the method body of execute () The key reason is that the execute () method itself throws in the Declaration Exception * So any subclass of Exception exception can be throw in the execute () method body * This provides a great convenience for custom exceptions/public String execute () throws Exception {if (!) Admin. Equals (This.getusername ()) {throw new Usernameexception ("Invalid user name");//username invalid}else if (!) Jadyer ". Equals (This.getpassword ())) {throw new passwordexception (" Invalid password ");//password invalid the}else{return SUCCESS;// Username and password is Allright}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.