0

I have a page where I will use multiple @ModelAttribute on the same @RequestParameter. Is there any other way to solve this? Instead of making another JSP for each form?

As you can see in my controller, the "/acct" takes the @ModeAttribute "wish". What I want is to use the @RequestMapping "/account" for both of my form.

Here is the code.

@Controller

public class UserController {

@Autowired
private UserService userService;

@Autowired
private BlogService blogService;

@Autowired
private WishlistService wishlistService;

@ModelAttribute("blog")
public Blog constructItem(){
    return new Blog();
}

@ModelAttribute("wish")
public Wishlist constructWish(){
    return new Wishlist();
}

@RequestMapping("/account")
public String account(Model model, Principal principal){
    String name = principal.getName();
    model.addAttribute("user", userService.findOneWithBlogs(name));
    return "account";
}

@RequestMapping(value="/account", method=RequestMethod.POST)
public String doAddBlog(Model model, @Valid @ModelAttribute("blog") Blog blog, Principal principal, BindingResult result){

    if(result.hasErrors()){
        return account(model, principal);
    }
    String name = principal.getName();
    blogService.save(blog, name);
    return "redirect:/account.html";
}

@RequestMapping("/blog/remove/{id}")
public String removeBlog(@PathVariable int id){
    Blog blog = blogService.findOne(id);
    blogService.delete(blog);
    return "redirect:/account.html";
}

@RequestMapping("/acct")
public String acct(Model model, Principal principal){
    String name = principal.getName();
    model.addAttribute("user", userService.findOneWithWish(name));
    return "account";
}

@RequestMapping(value={"/acct"}, method=RequestMethod.POST)
public String doAddWish(Model model, @ModelAttribute("wish") Wishlist wish, Principal principal, BindingResult result){
    if(result.hasErrors()){
        return acct(model, principal);
    }
    String name = principal.getName();
    wishlistService.save(wish, name);
    return "redirect:/account.html";
}

}

  • i think this link can help you http://stackoverflow.com/questions/13242394/spring-mvc-multiple-modelattribute-on-the-same-form – abdotalaat Jan 04 '15 at 20:54
  • Nice! I just added an action to the form to direct it to the @RequestMapping "/acct". Thank you for your reply. – Ralph Herrera Jan 05 '15 at 05:28

0 Answers0