Question: here is a org.springframework.validation.Validator ,please fill in the blanks using the hints package comps368.tma02; import comps368.tma02.ItemsController.Form; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.stereotype.Component; import
here is a org.springframework.validation.Validator ,please fill in the blanks using the hints
package comps368.tma02;
import comps368.tma02.ItemsController.Form; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.Validator;
/******* IMPORTANT : You can refer to u5p99 to complete this class *******/ /******* * IMPORTANT : But 10000% guarantee that you cannot complete with direct * copy and paste, nor without understand what the course * material coding is doing for *******/
/* 1. Annotation */ @Component public class ItemValidator implements Validator /* 2. The interface for Spring Validation */ {
/* 3. This is the tricky point. You need to validate whether the item code is already exists. Therefore you need to retrieve the DB to see whether search by same item code has results. So, how can you retrieve DB in Spring? */ /* 4. You should Override supports() method. However, beware what is the class you are validating? It is the view model of Items */
@Override public void validate(Object o, Errors errors) { /* 5. Cast to which class type? */ _i = (/* 5. Cast to which class type? */)o; //6. Validate the item code is null or empty (use the .isEmpty() method of string) //7. If null or empty, set the reject value by the errors.rejectValue() method // Go to u5p75 to check what are the 3 parameters need to pass to that function // Read start from the sentence "As shown in Listing 5.43,....." //check the item code is used before //8. Validate the item code is exists in DB //7. If exists, set the reject value by the errors.rejectValue() method // Go to u5p75 to check what are the 3 parameters need to pass to that function // Read start from the sentence "As shown in Listing 5.43,....." //9. Validate the price range from 0 to 999.99 //7. If the price is smaller than 0 or greater than 999.99, set the reject value by the errors.rejectValue() method // Go to u5p75 to check what are the 3 parameters need to pass to that function // Read start from the sentence "As shown in Listing 5.43,....." } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
