Announcement Announcement Module
Collapse
No announcement yet.
Learning Spring MVC, some initial questions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Learning Spring MVC, some initial questions

    Hi all. I've spent a couple of weeks doing the various Spring MVC tutorials that are out there, and am now setting about coding an application. I have several years experience with Struts, and am basically recoding an existing Struts app with Spring, so as to learn its capabilities and differences.

    I have a couple of initial questions which I hope you can help with.

    1) When declaring a Controller in the myapp-servlet.xml file, I'm confused with the two properties 'successView' and 'formView'. They seem to be treated differently in all the examples I have seen. The success view always seems to have a file extension, ie 'success.htm' and refers to a mapping declared elsewhere in the file. But the form view, as far as I can tell, seems to refer directly to one of the files under the JSP directory. Why is this? Why are the two treated differently? Take this tutorial, I hope you'll see what I mean.

    2) I want to know the best way of handling user-defined validation. For example, say I have a form with a field asking for a username. The standard Validation object will check that the field is not blank, and then if ok, forward onto the success view. Then, in the onSubmit() method of my controller, I want to interogate my database, and see if that user exists. If not, I want to return to the form, with an appropriate error message.

    Now, in Struts, I would have found access to the ActionErrors object, and returned to the page, which worked fine. But here, I'm not sure if I have access to the Errors object - and even if I did, if I return to the original form in my return new ModelAndView code it seems to reset the form anyway.

    How would you guys handle this kind of thing?


    Many thanks for any help you can give!

  • #2
    Hi im stull quite new to Spring but I will take a stab at your questions.

    Q1.
    The PriceIncreaseFormController shown in the example provides the Model for the formView, so it points stright to the view. The successView is set to home.html so that spring mapps it to a controller that will create the Model for it.

    Q2.
    You can create an class that implements the org.springframework.validation.Validator interface and interogate your database in the validate method.

    Or in your onSubmit method of your controller you can interogate your database and set any errors onto the BindException. The call the showForm method to show the form view again.

    Does that help?

    Ben

    Comment


    • #3
      Hi Jamin, thanks for your reply.

      Originally posted by jamin View Post
      Q2.
      You can create an class that implements the org.springframework.validation.Validator interface and interogate your database in the validate method.

      Or in your onSubmit method of your controller you can interogate your database and set any errors onto the BindException. The call the showForm method to show the form view again.
      Perfect! The showForm() method was just what I was after.

      Originally posted by jamin View Post
      Q1.
      The PriceIncreaseFormController shown in the example provides the Model for the formView, so it points stright to the view. The successView is set to home.html so that spring mapps it to a controller that will create the Model for it.
      This still isn't clear to me. In that example, if I change the successView to point to an actual page, in the same way as the formView does, then I get a 404 error. It appears to be expecting a mapping.

      Yet if I try and enter a mapping into the form view (ie index.htm) it automatically appends .jsp on the end, implying that in this case, it is expecting a direct page reference.

      The two seem to work in opposite ways to each other.

      Comment


      • #4
        Hi,

        Its appending the .jsp beacuse the InternalResourceViewResolver (shown below) is configured todo so.

        Code:
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="viewClass">
                   <value>org.springframework.web.servlet.view.JstlView</value>
                </property>
                <property name="prefix"><value>/WEB-INF/jsp/</value></property>
                <property name="suffix"><value>.jsp</value></property>
            </bean>
        If you look at the example at the following line of code

        Code:
        return new ModelAndView(new RedirectView(getSuccessView()));
        As far as Im aware the RedirectView will do as it says, redirect the incomming http request to the view specified, eg hello.html. Spring will then mapp this to the springappController bean which will create the model and which will get passed to the hello.jsp view.

        I dont know If thats any help.

        Comment


        • #5
          gezhall,

          when using FormControllers, the important point to remember is, the same formController is responsible for both "painting" the jsp (ie providing data to jsp) and handling the submit during a post.

          The formView is resolved to whatevername.jsp via the InternalViewResolver.

          The successView does not go directly to a JSP, instead it has to goto another formController, to let that formController "paint" the JSP of its' formView property. Since you cannot directly goto controller using the new ModelAndView(), you do a RedirectView, ie Redirect of View to another Controller. (in this case Redirect to hello.htm, which will eventually invoke the springappController, in turn calling the formBackingObject and paint its own view).

          If your second page is not a Form and just a simple HTML or non-form JSP, you could get the successView resolved to a jsp directly instead of a Redirect.

          Comment


          • #6
            That clears things up very nicely, thanks to you both :-)

            Comment

            Working...
            X