Results 1 to 4 of 4

Thread: Spring MVC Validation issue

  1. #1
    Join Date
    Apr 2008
    Posts
    2

    Default Spring MVC Validation issue

    I have created a web-app using Spring 2.5. I am using the Validation methods that Spring describes in there tutorial and I seem to get Validation before the form is even shown. here is my code...

    servlet xml:
    ************************************************** ****
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <!--
    - Application context definition for "springapp" DispatcherServlet.
    -->

    <beans>
    <bean id="wineappController"
    class="com.winesite.web.controllers.WineappControl ler" />

    <bean id="loginController"
    class="com.winesite.web.controllers.LoginControlle r">
    <property name="formView" value="login" />
    <property name="successView" value="members" />
    <property name="commandName" value="userDAO" />
    <property name="commandClass"
    value="com.winesite.dao.impl.UserDAOImplHibernate" />
    <property name="validator">
    <bean class="com.winesite.web.validators.LoginValidator" />
    </property>
    </bean>

    <bean id="addrecipesController"
    class="com.winesite.web.controllers.AddRecipesCont roller">
    <property name="formView" value="addRecipe" />
    <property name="successView" value="members" />
    <property name="commandName" value="wineService" />
    <property name="commandClass"
    value="com.winesite.domain.WineService" />
    <property name="validator">
    <bean class="com.winesite.web.validators.AddRecipeValida tor" />
    </property>
    </bean>

    <bean id="viewrecipesController"
    class="com.winesite.web.controllers.ViewRecipesCon troller"/>

    <bean id="urlMapping"
    class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
    <property name="mappings">
    <props>
    <prop key="/welcome.wine">wineappController</prop>
    <prop key="/login.wine">loginController</prop>
    <prop key="/viewrecipes.wine">viewrecipesController</prop>
    <prop key="/addrecipes.wine">addrecipesController</prop>
    </props>
    </property>
    </bean>

    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
    <property name="viewClass">
    <value>org.springframework.web.servlet.view.JstlVi ew</value>
    </property>
    <property name="prefix">
    <value>/jsp/</value>
    </property>
    <property name="suffix">
    <value>.jsp</value>
    </property>
    </bean>
    </beans>

    ************************************************** *****

    It is only the addrecipesController that is giving me a problem... Here is the code for it

    AddRecipesController
    ************************************************** *****
    public class AddRecipesController extends SimpleFormController{

    private static Logger logger = Logger.getLogger(AddRecipesController.class);

    protected Object formBackingObject(HttpServletRequest request)
    throws ServletException {
    WineService wineService = BeanFactory.newWineService();
    return wineService;
    }

    protected ModelAndView processFormSubmission(HttpServletRequest request,
    HttpServletResponse response, Object command, BindException errors) {
    logger.info("in process form submission");

    WineService wineService = (WineService)command;

    wineService.saveWineData();
    logger.info("saved wine data");
    if(errors.getAllErrors().size() > 0){
    try{
    return showForm(request, response, (BindException)errors);
    }catch(Exception e){
    logger.error("couldnt show form: [" + e + "]");
    }
    }
    return new ModelAndView(getSuccessView());
    }

    }
    ************************************************** *****

    Here is the Validator

    AddRecipeValidator
    ************************************************** *****
    public class AddRecipeValidator implements Validator {

    public boolean supports(Class givenClass) {
    return givenClass.equals(WineService.class);
    }

    public void validate(Object obj, Errors errors) {
    WineService wineServ = (WineService) obj;

    if(wineServ == null) {
    errors.reject("error.nullpointer", "Null data received");
    } else {

    /* Test givenData’s fields here */

    if(wineServ.getName().equals("")) {
    errors.rejectValue("name", "error.code",
    "Wine name was null, please enter a name");
    }

    if(wineServ.getNutrient() == null || wineServ.getNutrient().equals("")){
    errors.rejectValue("nutrient", "error.code",
    "Nutrient was null, please enter 'true' or 'false'");
    }

    if(wineServ.getYeast() == null || wineServ.getYeast().equals("")){
    errors.rejectValue("yeast", "error.code",
    "Yeast was null, please enter yeast used");
    }

    if(wineServ.getRecipe() == null || wineServ.getRecipe().equals("")){
    errors.rejectValue("recipe", "error.code",
    "Recipe was null, please enter recipe used");
    }

    if(wineServ.getBottleDate() == null){

    }else{
    DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    try{
    Date bottle = sdf.parse(wineServ.getBottleDate());
    }catch(ParseException e){
    errors.rejectValue("bottleDate", "error.code",
    "BottleDate was in a bad format, please enter 'MM/dd/yyyy' eg. 09/24/1985");
    }
    }

    if(wineServ.getStartDate() == null){

    }else{
    DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    try{
    Date start = sdf.parse(wineServ.getStartDate());
    }catch(ParseException e){
    errors.rejectValue("startDate", "error.code",
    "StartDate was in a bad format, please enter 'MM/dd/yyyy' eg. 09/24/1985");
    }
    }

    }
    }

    }
    ************************************************** ******

    Does anyone know why the validator would be running before the form is even submitted?

    Any Help would be appreciated.

    Thanks.

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,806

    Default

    use code tags

    they are our friends

    Does anyone know why the validator would be running before the form is even submitted?
    why did you say that?, i dont know the order
    but the client from a remote/local host send a request to the server (after to press the submit of course ) so the validation is done always in the server

    regards
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    Dec 2006
    Posts
    311

    Default

    When is it running.....on a GET (when the form is loaded)?

  4. #4
    Join Date
    Apr 2008
    Posts
    2

    Default Fixed

    I figured it out... stupid mistake, i was arriving at the form from a submit of this own form... Bad mistake on my part, not paying attention

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •