Custom exceptions thrown in the background
1 PublicString Execute ()throwsException {2 3 if(GetUser (). Equalsignorecase ("User"))4 {5 Throw NewZcxexception ("Custom Exception");6 }7 if(GetUser (). Equalsignorecase ("SQL"))8 {9 Throw NewJava.sql.SQLException ("User name cannot be SQL");Ten } One if(GetUser (). Equals ("Zcx")) A { -Addactionmessage ("Server Prompt! "); - returnSUCCESS; the } - returnERROR; -}
Struts Configuration exception Handling
1 2 3< PackageName= "Zcx.controller" namespace= "/"extends= "Struts-default" >4<!--define a global result map--5<global-results>6<!--definition when SQL, root two logical exceptions all correspond exception.jsp page--7<result name= "SQL" >/WEB-INF/content/exception.jsp</result>8<result name= "Root" >/WEB-INF/content/exception.jsp</result>9</global-results>Ten One<!--defining global exception mappings-- A<global-exception-mappings> -<!--When an SqlException exception is encountered in the action, -The system will be transferred to the result of name SQL-- the<exception-mapping exception= "java.sql.SQLException" result= "sql"/> -<!--When an exception exception is encountered in the action, -The system will be transferred to the result of name root-- -<exception-mapping exception= "Java.lang.Exception" result= "root"/> +</global-exception-mappings> - + A<action name= "Login"class= "Zcx.controller.LoginAction" > at<!--defines a local exception map, and when an myexception exception is encountered in the action, -The system will be transferred to the result of the name my-- -<exception-mapping exception= "zcx.controller.ZcxException" result= "my" ></exception-mapping> -<result name= "My" >/WEB-INF/content/exception.jsp</result> -<result name= "Error" >/WEB-INF/content/error.jsp</result> -</action> in -<!--to handle all action--> to<action name= "*" > +<!--return the corresponding page-- -<result>/WEB-INF/content/{1}.jsp</result> the</action> * $ Panax Notoginseng</ Package>
Struts2-Learning Note Series (8)-Exception handling