Announcement Announcement Module
Collapse
No announcement yet.
simpleformcontroller in tile Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • simpleformcontroller in tile

    hello,

    i have tiles set up with header, content, and footer. in the header tile, i would like to have a simple "search" form. this is available/included in all views/pages.

    i am having problems binding the search form with this view. what is the proper configuration in the spring servlet? since there is not set bean name (link), how do i bind the form for all the pages and set up the controller?

    i'm new to spring, so any help is appreciated. thanks in advance.

  • #2
    how about setting sessionForm=true in your bean configuration.

    if I'm not mistaken you will have to put back your command object into the session in your onSubmit() method once you have completed your work.

    Comment


    • #3
      thanks for your response.

      if i put the command object into the session, it will be able to bind to the form. is this the proper way to handle this?

      but now how would it know what the controller is? as in when the "submit" button is clicked, what controller would it go to? since the controller is also normally configured to the bean name/link in the spring servlet config. and this is a "global" form.

      Comment


      • #4
        you can specify the controller in <form:form action="search" > tag.

        so when you map /search = searchController , when the submit button is clicked it will always go to searchController

        Comment


        • #5
          hey the "action=" works...

          but now, i'm having trouble doing the sessionform. i did this in the config file:

          <property name="sessionForm"><value>true</value></property>

          But since this form is in a tile, I am getting the below:

          Neither BindingResult nor plain target object for bean name 'command' available as request attribute

          I tried overriding formBackingObject of the controller and returning a new instance of my search form. That didn't seem to make a difference. Thanks for your help again.

          Comment


          • #6
            Hi...

            Well, like I said in post #2 you will have put your command object back into the session before returning the ModelAndView

            so in your on submit method do the follwing

            Code:
            	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
            
                            // your search code...
            
            		WebUtils.setSessionAttribute(request, getFormSessionAttributeName(), command);
            		return new ModelAndView(getSuccessView());
            	}
            you can read the documentation to understand how the session form works, i'm not quite sure at which point the command object is removed from the session, so in order for your command object to be available always you have to put it back to the session.

            you can also search the forum to get more info on this.. im sure there's a good explanation

            hope this helps..
            Last edited by rukshan; Nov 11th, 2008, 12:23 AM. Reason: typo

            Comment


            • #7
              i think it's starting to make more sense.

              how would i put it into the session initially? onSubmit() is called after submitting a search. but i am getting:

              "Neither BindingResult nor plain target object for bean name 'command' available as request attribute"

              when i log-in and go to the home page.

              i tried doing this to my home page controller processing:

              modelAndView.addObject("command",new com.forms.SearchForm());

              that seems to work the first time, and only for the home view. after that, back to the error above. even after putting the command back in the session on submit of the search controller.

              Comment


              • #8
                when you do the following

                modelAndView.addObject("command",new com.forms.SearchForm());

                the object is only available for that request..

                try to put the command object into the session
                by

                WebUtils.setSessionAttribute(request, "command" , new com.forms.SearchForm());

                hope it works...

                Comment


                • #9
                  thanks for holding my hand through this.

                  unfortunately, still not working.

                  WORKS for the immediate view
                  //modelAndView.addObject("command",new com.forms.SearchForm());


                  DOES NOT WORK at all //httpServletRequest.getSession().setAttribute("comm and", new com.forms.SearchForm());

                  WebUtils.setSessionAttribute(httpServletRequest, "command", new com.forms.SearchForm());

                  it doesn't seem like it's looking in the session for "command". i tried changing the default "command" name, no luck.

                  just another question. since i'm using "action=" in the form itself, is it actually looking in the config file for that action to know this is a session form?
                  Last edited by pgibbons; Nov 12th, 2008, 12:40 AM.

                  Comment


                  • #10
                    Originally posted by pgibbons View Post
                    thanks for holding my hand through this.
                    it doesn't seem like it's looking in the session for "command". i tried changing the default "command" name, no luck.

                    just another question. since i'm using "action=" in the form itself, is it actually looking in the config file for that action to know this is a session form?
                    its not looking for the command in the session from your jsp, the command is looked for in the session in you formBackingObject() method in search controller.

                    when you specifify action=yoururl the form will be submitted to that specific url.

                    since the form is only looking for the command object in the request we can try the following.

                    you set your command in session in your homeController. by doing

                    WebUtils.setSessionAttribute(httpServletRequest, "command", new
                    com.forms.SearchForm());

                    then in your jsp
                    Code:
                    <c:if test="${command eq null}">
                         <c:set var="command" value="${sessionScope.command}"/>
                    </c:if>
                    
                    <form:form action="search" commandName="command">
                    your seacch form inputs....
                    </form:form>
                    by doing the above, when the command is not available in your request attibutes it will get the command that we stored in session.

                    if this works you will no longer need the sessionForm=true in your search controller.

                    try it out...

                    Comment


                    • #11
                      That didn't seem to work either.... it seems like it's really bent on getting "command" from the request for some reason.

                      Just to be sure, in my home controller, I manually set a search string.

                      Code:
                      				SearchForm s = new SearchForm();
                      				s.setSearchString("searchstring");
                      				WebUtils.setSessionAttribute(httpServletRequest, "command", s);
                      And on the tile jsp (where the form is), I printed it out to make sure that part is working. And that worked fine (I commented out the form code).

                      ${sessionScope.command.searchString}

                      But as soon as i put the form code in there

                      Code:
                      	<form:form id="searchForm" commandName="command" name="searchForm" method="post" action="search">
                      		<form:input id="searchString" path="searchString"/>
                              <input type="submit" id="search" name="_eventId_search" value="Search" />    
                      	</form:form>
                      I start to get "Neither BindingResult nor plain target object for bean name 'command' available as request attribute". Completely ignores the "command" object in the session.

                      What is not making sense to me is that how can we just say "action=search". How would it know what the form backing object is (it will after you submit, but when form is loading up)?

                      Usually, we have an action, and we link to that action, so it looks in the config file and sees what the form backing object, and calls formbackingobject. But here we are just using it without ever going to the action in the config file first.

                      Comment


                      • #12
                        looks like the command object has to be in request scope. so you can do a small change
                        Code:
                             <c:set var="command" value="${sessionScope.command}" scope="request"/>
                        Code:
                        <c:if test="${command eq null}">
                             <c:set var="command" value="${sessionScope.command}" scope="request"/>
                        </c:if>
                        
                        <form:form action="search" commandName="command">
                        your seacch form inputs....
                        </form:form>
                        because when we do <c:set var="command" value="${sessionScope.command}"/>
                        the command object is in page scope. so we can set it in request by scope="request"

                        What is not making sense to me is that how can we just say "action=search". How would it know what the form backing object is (it will after you submit, but when form is loading up)?

                        Usually, we have an action, and we link to that action, so it looks in the config file and sees what the form backing object, and calls formbackingobject. But here we are just using it without ever going to the action in the config file first.

                        well looks like form looks for the command object in the request.

                        Comment


                        • #13
                          that seems to be working! thanks for all your help.

                          i wonder if there's a "real" spring way of doing this. but oh well, it is working for now... thanks again.

                          Comment


                          • #14
                            putting the command object in the home controller is not appropriate, so what u can do is you can use a intercepter.,

                            Intercept all requests that has the search form in the view.. and then put the command object if its not in the session.

                            Comment


                            • #15
                              Can you please post full solution to this problem ? (your controller confifuration, tiles and Java code itself). What did you put for formView?

                              Comment

                              Working...
                              X