Results 1 to 3 of 3

Thread: Controllers in MVC

  1. #1
    Join Date
    Dec 2011
    Posts
    5

    Default Controllers in MVC

    Hi,

    I have a controller to show a jsp page:
    Code:
    @Controller
    public class NodoController {
    	
    	@RequestMapping(value="nodoak.htm")
    	public ModelAndView handleRequest(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
     
    		NodoManager nodoManager = new NodoManager();
    		List<Nodo> nodoLista = nodoManager.getNodoList();
    		Nodo nodo = nodoLista.get(0);
    		ModelAndView modelAndView = new ModelAndView("nodoak");
    		modelAndView.addObject("nodo", nodo);
    
    		
    		return modelAndView;
    	}
    }
    In that view, "nodoak" I've got a form with a submit button. And another controller for the form:

    Code:
    @Controller
    @SessionAttributes
    public class FormController {
    
    	@RequestMapping(value="/nodoak.htm", method=RequestMethod.POST)
    	public ModelAndView onSubmit(FormObject formObject, BindingResult result){
    		
    		ModelAndView modelAndView= new ModelAndView("nodoak");
    		
    		Nodo nodo = new Nodo();
    		NodoManager nodoManager= new NodoManager();
    		nodo=nodoManager.getNodoList().get(formObject.getId());
    		modelAndView.addObject(nodo);
    			
    		return modelAndView;
    	}
    }
    It doesn't work and I'm getting this error:

    java.lang.IllegalStateException: Cannot map handler [nodoController] to URL path [/nodoak.htm]: There is already handler [com.kbp.sim.web.NodoController@18bddf0] mapped.

    What's happening, I am telling Spring to use two controllers for the same page? How can I do what I'm trying to do? A controller to show the page comming from a link, and another controller to control the form. And that controller has to send me to the same page, but with another model.

    Help please! I hope I've been clear.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Well the message is quite clear isn't it... You cannot have 2 controllers mapped to the same url only a single controller can be mapped to a url. So you have to put both methods in a single controller. Although this might work with Spring 3.1 with @MVC (RequestMappingHandlerMapping) not the default implementation (DefaultAnnotationHandlerMapping). So either switch or create a single controller.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Dec 2011
    Posts
    5

    Default

    Quote Originally Posted by Marten Deinum View Post
    Well the message is quite clear isn't it... You cannot have 2 controllers mapped to the same url only a single controller can be mapped to a url. So you have to put both methods in a single controller. Although this might work with Spring 3.1 with @MVC (RequestMappingHandlerMapping) not the default implementation (DefaultAnnotationHandlerMapping). So either switch or create a single controller.
    Thank you Marten. So I'll create different controllers for different requests... I was trying to do some crazy thing and I think I have to follow the common sense.

Tags for this Thread

Posting Permissions

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