JSP from simple to deep (11-2)

Source: Internet
Author: User

So far, we still have some questions that have not been clarified. First, you cannot clear the name area. If you type an incorrect content (not an integer value) in the "age" area, a Java Exception error occurs.
Now we will solve this problem using other flags in the Blazix flag library. Blazie provides a blx: setProperty flag to solve this problem. Blx: setProperty allows us to define an exception error handling method. If an exception occurs, we can collect an error message for the user and continue to process it.
Below is our modified SaveName. jsp, which can handle any errors. It not only displays GetName. jsp, but also can type the correct data on it, and automatically connects to NextPage. jsp:

<% @ Taglib prefix = "blx" uri = "/blx. tld" %>

<%!


Boolean haveError;


StringBuffer errors;




Public void errorHandler (String field,


String value,


Exception ex)


{


HaveError = true;


If (errors = null)


Errors = new StringBuffer ();


Else


Errors. append ("<P> ");


Errors. append ("<P> Value for field" "+


Field + "" is invalid .");


If (ex instanceof java. lang. NumberFormatException)


Errors. append ("The value must be a number .");


}


%>


<%


// Variables must be initialized outside declaration!


HaveError = false;


Errors = null;


%>


---------------


<BODY>


<Jsp: useBean id = "user" class = "UserData" scope = "session"/>


<Blx: setProperty name = "user"


Property = "*"


OnError = "errorHandler"/>


<%


If (haveError ){


Out. println (errors. toString ());


PageContext. include ("GetName. jsp ");


} Else


PageContext. forward ("NextPage. jsp ");


%>


</BODY>


</HTML>



Note that haveError and errors must be reinitialized each time because they are initialized outside the declared environment.

Now, if an error occurs in the blx: setProperty processing process, we will display this error and return to GetName. jsp again so that users can correct this error. If no error occurs, we automatically connect the user to NextPage. jsp.

Related Article

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.