Struts2 Exception Handling
I. Case study: Enter the Logon account and password on the input. jsp page to test the exception handling mechanism.
Ii. Case Analysis: struts2 provides a local exception handling mechanism and a global exception handling mechanism. Local Exception Processing takes precedence over global Exception Processing. When the exception cannot be found, global Exception Processing is queried.
A) exception class declaration
public class UsernameException extends Exception {private String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public UsernameException(String message){super(message);this.message = message;}}
B) throwing an exception
If (! "Admin". equals (username) {throw new UsernameException ("Incorrect username! ");}
C) Struts. xml configuration Exception Handling
1. Local Exception Handling (for the current action .)
/login.jsp
/usernameInvalid.jsp
/passwordInvalid.jsp
2. Global Exception Handling (for the current package .)
/UsernameInvalid. jsp
/PasswordInvalid. jsp
Iii. Experience:
A) exception handling. When writing a program, you must remember to throw an exception to the outside. Never try... catch. Especially during the interview, exception handling is also very important. I interviewed a company and the interviewer pointed out this point. Therefore, when an exception occurs, remember to throw it out.
B) Code robustness. What is code robustness. For example, when learning servlet, add, delete, modify, and query databases. You may be able to handle dozens of rows, but such a program is more easily "handled" by others. Here, the solution means destruction. Even if you cannot write a program on all sides at the moment, you have to consider it at the very least. When problems are solved, you may find that you can easily implement the functions in another way.
C) exception handling is often seen in projects, but not in our case. If the exception is handled in this way, struts2 will fail. Each system has its own exception handling solution. When developing a system, we only need to configure global exceptions to ensure the robustness of the Code. There is no need to handle all exceptions that are unlikely to happen.
4. Reference project: http://download.csdn.net/detail/u011638419/8182127
5. happy every day:
Before the age of 20, my parents told me that they were not allowed to meet their boyfriends,
But after the age of 20, my parents said, don't come back if you don't have a boyfriend.
The right to final interpretation belongs to partner aishang. Please indicate the source for reprinting.