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

  • 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
    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.

    Comment


    • #3
      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.

      Comment

      Working...
      X