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

  • ModelAttribute problem

    Hi,
    I have a problem with the @ModelAttribute and @SessionAttributes usages.
    Basically, I have a JSP with a form and a controller with the related form backing bean and I want to modify the value in the form, refresh the page (with Javascript) and show the new value in the same page.

    The problem is that the value of the form backing bean does not change if I modify the value in the form and I refresh the page (I have always the first value)...

    This is my controller:
    Code:
    @Controller
    @SessionAttributes(value = {"value"})
    public class FindValuesForm {
    
    private final ValueDto value;
    
    public FindValuesForm(){
      this.value = new ValueDto();
    }
    
    ...
    @RequestMapping(value = "/find.do", method = RequestMethod.GET)
    public String setupForm(ModelMap model){
      ...
      model.addAttribute("value", this.value);
      return "find";
    }
    
    @RequestMapping(value = "/find.do", method = RequestMethod.POST)
    public String processSubmit(
    @ModelAttribute("value") ValueDto value, BindingResult result,  ModelMap model) {
      ...
    // Use the value object
    }
    And my JSP looks like:

    Code:
    <form:form name="form1" modelAttribute="value">
     ...
      <c:out value="value: ${value.id_value}"/>
    </form:form>
    I don't know how to solve this problem...
    Any idea?
    Thanks!

    BYE
    Last edited by andrew007; Dec 29th, 2008, 12:56 PM.

  • #2
    I'm trying to find a solution...
    What do I need to do is to reuse the same form backing bean object when I refresh the page...
    I set the backing object with:

    Code:
    @ModelAttribute("value")
    public ValueDto formBindingObject() {
      final ValueDto value = new SearchValueDto();
      logger.info(value);
      return (value);
    }
    But every time I refresh the page, this method is called and I have every time a new backing object...

    Is there any way to create the backing object only once and to take the object from the session??
    Thanks...
    BYE!

    Comment


    • #3
      I think your mixing up some uses here. The @ModelAttribute("value") should be your reference data right?

      Your formbacking object would be called through

      Code:
      @RequestMapping(value = "/find.do", method = RequestMethod.GET)
      public String setupForm(ModelMap model){
        ...
        model.addAttribute("value", this.value);
        return "find";
      }
      It looks like your duplicating work. Cant you just use the @Requestmapping above but inspect the request to decide if you want an object from the dB or if its a new object?

      Comment


      • #4
        It looks like your duplicating work. Cant you just use the @Requestmapping above but inspect the request to decide if you want an object from the dB or if its a new object?
        Thank you for your reply....
        I don't need an object from the dB, but I have to take the object from the request (FORM) and reuse the same object when I refresh the page...

        I did something like:
        Code:
        @Controller
        @SessionAttributes(value = {"value"})
        public class FindValuesForm {
        
        private final ValueDto value;
        
        
        ...
        @RequestMapping(value = "/find.do", method = RequestMethod.GET)
        public String setupForm(ModelMap model){
          ...
        
          if(value==null){ 
            value = new ValueDto();
            model.addAttribute("value", this.value);
          } else
          {
            model.addAttribute("value", this.value);
          }
        
          return "find";
        }
        
        @RequestMapping(value = "/find.do", method = RequestMethod.POST)
        public String processSubmit(
        @ModelAttribute("value") ValueDto value, BindingResult result,  ModelMap model) {
          ...
        // Use the value object
        }
        But it does not work and the backing object is not refreshed...

        Comment

        Working...
        X