Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC Validation issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


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

      Comment


      • #4
        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

        Comment

        Working...
        X