Development of JSF applications in netbeans (6)

Source: Internet
Author: User
Tags netbeans

Create a custom ConverterAlthough it is very simple to use a required domain and converter for verification, it is also very limited. For example, the validators can verify that the birthdate domain is a valid date, but cannot verify that the date is the previous date. To adjust the date verification method, we will create a custom validator. Our validators verify that the date format is correct and is the past date. If an error occurs, the validator displays the corresponding message. To create a custom converter, you need to create an implementation Javax. Faces. converter. Converter And Faces-config.xml . You can use <F: converter> Mark to use the converter.1. Right-click the project node, select new> Java class, and name the class MydateconverterAnd put it in Astrologer. ConvertPackage, and then click Finish. 2. Implement the converter interface in the class declaration as follows:
 public class MyDateConverter implements Converter {
3. Use the IDE prompts to add appropriate import statements and implement abstract methods. (In the previous section, you used the IDE prompt to implement the validate method ). IDE generates two methods: GetasobjectAnd Getasstring. 4. Add the following code to the Method Getasobject Medium(Make sure to change the string parameter in the method declaration to value ):
public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
        String pattern = "dd/MM/yyyy";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date nDate;
        try {
             nDate = sdf.parse(value);
        } catch (ParseException ex) {
            FacesMessage message = new FacesMessage();
            message.setDetail("Date is missing or not valid");
            message.setSummary("Date is missing or not valid");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ConverterException(message);
        }
        if(nDate.getTime() > new Date().getTime()){
            FacesMessage message = new FacesMessage();
            message.setDetail("Date is bigger than current date");
            message.setSummary("Date is bigger than current date");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ConverterException(message);
        }
        return nDate;
    }
5. Modify the method signature and add the following code Getasstring Method(In the method signature, make sure to change the object parameter name to value ):
public String getAsString(FacesContext facesContext, UIComponent uIComponent, Object value) {
    return value.toString();
}
6. Use Alt + Shift + F to add required import statements. (You should choose to introduce Java. Text. parseexception And Java. util. Date7. Open the faces-config.xml and then add the following code:
<converter>
   <converter-id>astrologer.MyDateConverter</converter-id>
   <converter-class>astrologer.convert.MyDateConverter</converter-class>
</converter>
   
8. Open Greeting. jsp Update Form:
<p>Enter your name: 
<p>Enter your email: 
        <f:validator validatorId="astrologer.EmailValidator" />
    
<p>Enter your birthday: 
        <f:converter converterId="astrologer.MyDateConverter" />
    
(dd/mm/yyyy)</p>
 
    
9. Run the project. When you enter an invalid date or a future date, you will see the following error message: page 180 of the User-Defined converter reference book. Book: Java ee 5 practical tutorial

 

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.