Results 1 to 4 of 4

Thread: Display Form AND Other Data

  1. #1
    Join Date
    Jul 2010
    Posts
    26

    Default Display Form AND Other Data

    Hi,

    I am new to Spring. I am trying to display a form for adding a user and under the form I would like to display a list of existing users. The user should not have to post the form to get the list. I want the list of users to display on the get request.

    I can not figure out how to do this.

    Here is my form controller...

    Code:
    import com.edusoft.inote.domain.User;
    import com.edusoft.inote.service.UserService;
    import com.edusoft.inote.service.UserServiceImpl;
    
    @Controller
    @RequestMapping("/userForm")
    public class UserFormController{
    	
    	@Autowired
    	private UserService userService;
    	
        /** Logger for this class and subclasses */
        protected final Log logger = LogFactory.getLog(getClass());
    	
    	@RequestMapping(method = RequestMethod.GET)
    	public ModelAndView show() {
    		
    		
    		List users = userService.getAllUsers();
    		logger.info("Users size " + users.size() + ".");		
    	
    	    Map model = new HashMap();
    	    model.put("model", users);
    	    
    		return new ModelAndView("userForm","user",new User());
    	}	
    	
    	@RequestMapping(method = RequestMethod.POST)
    	public ModelAndView submit(Object command, @ModelAttribute("user") User user) {		
    
    		logger.info("Adding user name " + user.getFirstName() + ".");
    		userService.addUser(user.getFirstName());		
    		return new ModelAndView("userForm");	
    	}
    
    }

  2. #2
    Join Date
    Mar 2010
    Location
    Boston, MA
    Posts
    316

    Default

    in

    return new ModelAndView("userForm","user",new User());
    You are returning a new User object which is what is going to be used as the model in the view. Change it to return the list of users.

    get rid of

    Code:
      model.put("model", users);
    and return
    Code:
    return new ModelAndView("userForm","users",users);
    and use 'users' to access the model object in the view.

  3. #3
    Join Date
    Jul 2010
    Posts
    26

    Default

    I tried that and got an exception stating...

    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute.


    If I do not return that new Users object I get this error. As far as i can tell there is no way to return "2 models" 1 for the empty form and one to display data. Argggg, Spring is frustrating, thinking about going back to Struts.

  4. #4
    Join Date
    Mar 2010
    Location
    Boston, MA
    Posts
    316

    Default

    In your JSP..the model name must match what you are returning in the controller..in my example the model name is 'users' (i changed it from 'user' because it made more sense).

    And trust me..dont go back to Struts

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •