Announcement Announcement Module
Collapse
No announcement yet.
Edit form data binding query Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Edit form data binding query

    I've got a working update web page form and just noticed a small problem that I hadn't appreciated and now I'm wondering whether I'm doing things correctly.

    In my controller method I load a Person object containing many member variables. I then pass that Person object through to my form where I only display some of the data to update. On submit, the controller method has a Person object with member variables all populated from fields on the form. Unfortunately, any member variables not bound to fields on the form are not populated by this controller.

    What I was probably stupidly assuming was that the Person object was maintained throughout and that data not in fields on the form was untouched but this doesn't look to be true. I can't see how this could be ever true thinking about it. Can anyone confirm this is how Spring works? I can get around it by adding hidden fields to the form. I just have to be careful with my Person save method to make sure it only saves fields from the form otherwise the data will be lost.

    Thanks
    Sean

  • #2
    I suggest a read of the reference guide especially the web chapter.

    In short if you want this either re-retrieve the considered person from the database (@ModelAttribute on a method) or store the person in the session (@SessionAttributes). This is assuming that you use annotation based controllers.

    Comment


    • #3
      Thanks for pointing that out Marten. Unfortunately, the example I used originally didn't use @SessionAttributes or @ModelAttribute.

      Been trying all morning to implement this without success. I just re-read your reply and it looks like you can use either one or the other and so I've just tried @SessionAttributes alone but I seem to always get this error no matter what I try.

      javax.servlet.ServletException: Cannot expose session attribute 'person' because of an existing model object of the same name

      At the top of my controller class I have:
      Code:
      @Controller
      @RequestMapping("/content")
      @SessionAttributes("person")
      public class HomeController extends BaseController {
      My controller method is:

      Code:
      	@RequestMapping(value = "/viewPerson/{personId}", method = RequestMethod.GET)
      	public String viewPerson(@PathVariable String personId,
      			HttpServletRequest httpServletRequest,
      			HttpServletResponse httpServletResponse, Model model) {
      
      		// Load the person from AD. If an LDAP exception display a message to
      		// come back later.
      		try {
      			System.out.println("Displaying form");
      			Person p = personService.getPerson(personId);
      			model.addAttribute("person", p);
      			model.addAttribute("canUpdate", canUpdateUser(personId));
      
      		} catch (Exception e) {
      			logger.error("User logon " + secureUser.getPrincipal() + " failed to retrieve user details. Reason : " + e.getMessage());
      			model.addAttribute("header", "User Details Unavailable");
      			model.addAttribute(
      					"message",
      					"There seems to have been a problem retrieving the user details.  Please try again later or contact the system administrator");
      			e.printStackTrace();
      			return "error";
      		}
      
      		addStdDataToModel(model, httpServletRequest, httpServletResponse);
      
      		// Check whether the person has a photo available, if not replace with
      		// standard image.
      		String photoUrl = getPhotoUrl(personId);
      		model.addAttribute("peoplePhotoUrl", photoUrl);
      
      		return "viewPerson";
      	}
      I've seen some of your other posts showing simple examples but for some reason it won't work for me.

      Any thoughts Marten?

      Comment


      • #4
        It depends if you want to have a stateful or stateless application.

        You use @SessionAttributes and probably you already have put a session attribute named person somewhere else in your session.

        Also if you finished working with the person you should use the SessionStatus object to clear the session.

        I suggest a read of the reference guide which explains SessionAttributes.

        Also you seem to have some method to add standard stuff, I suggest implementing that in a HandlerInterceptor instead of extending a base class. It is less obtrusive.

        Edit: Also please post full stacktrace (using [ code][/code ] tags) as in general there is a lot of useful information in there.

        Comment


        • #5
          Hi Marten

          Still can't get @SessionAttributes to work. This time I've created a simple controller method and a newly named session attribute so that it definitely doesn't clash.

          Code:
          @SessionAttributes({"newperson"})
          My controller method:

          Code:
          	@RequestMapping(value = "/testPerson/{personId}", method = RequestMethod.GET)
          	public String testPerson(@PathVariable String personId,
          			HttpServletRequest httpServletRequest,
          			HttpServletResponse httpServletResponse, Model model) throws NamingException {
          
          			Person p = personService.getPerson(personId);
          			model.addAttribute("newperson", p);
          
          			return "test";
          	}
          My view test is a Velocity template test.vm.

          The exception I'm getting is:

          Code:
          SEVERE: Servlet.service() for servlet appServlet threw exception
          javax.servlet.ServletException: Cannot expose session attribute 'newperson' because of an existing model object of the same name
          	at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:141)
          	at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
          	at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1031)
          	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:815)
          	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
          	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
          	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
          	at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
          	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
          	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:366)
          	at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
          	at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
          	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
          	at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
          	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
          	at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
          	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
          	at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
          	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
          	at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
          	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
          	at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
          	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
          	at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
          	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
          	at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
          	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
          	at uk.co.sjp.CustomLoginFilter.doFilterInternal(CustomLoginFilter.java:66)
          	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
          	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
          	at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
          	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
          	at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
          	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
          	at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:167)
          	at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
          	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
          	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
          	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
          	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
          	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
          	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
          	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
          	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
          	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
          	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
          	at java.lang.Thread.run(Thread.java:619)
          Any ideas why I keep getting this exception trying to create my session attribute?

          Comment


          • #6
            For anyone interested I finally found out what was causing the problem.

            My velocity viewresolver configuration in my servlet-context.xml had the setting

            Code:
            <beans:property name="exposeSessionAttributes" value="true" />
            which caused the problem because @SessionAttributes was exposing the object as well as my line

            Code:
            model.addAttribute("sidebarItem", sidebarItem);
            also adding the object to the model with the same name.

            Just got work out now whether it's best to leave exposeSessionAttributes = true, as I don't know what I may have been using directly from the session elsewhere, or just let @SessionAttributes take care of things and don't add it to the model.

            Comment


            • #7
              In recent times, technology has seen a great expansion for making people to work in an easy manner. And now data binding has come up with the amazing invention.

              Comment

              Working...
              X