struts--(IV.) exception handling mechanism

Source: Internet
Author: User

In the usual case, after we get the exception, we need to navigate the page to an error prompt and prompt for the error message. With Stuts we can deal with exceptions in two ways: 1. Programmatic Exception Handling
    • That is, when we invoke the method of the business logic Layer object in action, the exception is handled manually after the exception is intercepted by try{}catch
    • When programming exception handling, we can use the message processing mechanism of struts to process these exception information.

2. Automatic exception handling mechanism

    • That is, instead of catching an exception in action, it throws an exception to the struts framework to handle
    • We need to indicate in the configuration file how struts handles these thrown exceptions
    • Use the <exception/> element to define automatic exception handling
first, the main steps: 1. Configure exception Tags: struts-config.xml
1 <ActionPath= "/login"type= "Com.jialin.LoginAction"name= "UserForm"Scope= "Request"Validate= "true"attribute= "UF"input= "/index.jsp">2      <exception Key= "Login.user.not.found"type= "Com.jialin.UserNotFoundException" />3      <exception Key= "Login.password.error"type= "Com.jialin.PasswordErrorException"Path= "/loginfail.jsp" />4 5 </Action>

The exception only works on the current action, or you can configure the Global exception

1  <global-exceptions> 2     <ExceptionKey= "Login.user.not.found"type= "Com.jialin.UserNotFoundException" />3     <ExceptionKey= "Login.password.error"type= "Com.jialin.PasswordErrorException"Path= "/loginfail.jsp" />4  </global-exceptions> 
1) The key attributes of the <exception/> element configuration are:
    • The key – is the key of the error message text corresponding to this exception, and the value of this key needs to be determined in the resource properties file .
    • type– that defines what type of exception to handle
    • path– defines what page to turn to when an exception occurs, and if you do not define the Path property, by default, the value of the input property in the action configuration will be used as the page to turn

2) which priority? --The nearest principle

2. On the JSP page, use the:

To display the error message text for which the exception corresponds

1**2*sign in to action3*4*@author Jialin function: Obtain form data, call the model layer business logic, return to the steering information5*/6  Public classLoginactionextendsAction {7 8 @Override9      PublicActionforward Execute (actionmapping mapping, actionform form,Ten httpservletrequest request, httpservletresponse response) One             throwsException { AUseractionform UserForm =(useractionform) Form; -String UserName =userform.getname (); -         intPassword =Userform.getpassword (); the  -Usermanage Usermanage =Newusermanage (); -User User =NewUser (); - User.setname (userName); + User.setpassword (password); -         //There are no more trycatch statements here . + usermanage.validateuser (user); A         returnMapping.findforward ("Success"); at  -     } -  - } -  -  in<span style= "font-family:arial; Background-color: #ffffff "></span>

Ii. principle of proximity

struts--(IV.) exception handling mechanism

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.