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

  • Wrong view

    I have a controller. The correct method gets called byt the view is incorrect.

    Before I submit a form my url are.

    This is the code from my form
    Code:
    var expform=document.createElement("form");
    	expform.method="POST";
    	expform.action="cvedit/addProjectExperience.htm";
    Code:
    http://localhost:8080/cvedit.htm?personid=2&cvid=3
    When I submit the form I end up in
    Code:
    http://localhost:8080/cvedit/addProjectExperience.htm
    I would like to return to the same page. Why is this not working? I have a view called cvedit.jsp.



    Looks like my messed up cvedit.jsp but no buttons or forms are working.

    So how do I get back to my initial page?

    I have another method with exactly the same parameters and this one works. value="/cvedit" is the big difference. I can use the same value.


    Code:
    	/** 
    	 * Add experience
    	 */
    	@RequestMapping(value="/cvedit", params={"exptype","!experienceid"})	
    	public ModelAndView addExperience(
    Code:
    @Controller
    @RequestMapping("/cvedit")
    public class CVEditController {}
    Code:
    	@RequestMapping(method=RequestMethod.POST, value="addProjectExperience")	
    	public ModelAndView addProjectExperience(
    			@RequestParam(value="cvid", required=false) String cvid,
    			@RequestParam("exptype") String exptype,
    			@RequestParam("header") String header,
    			@RequestParam("description") String description,
    			@RequestParam("roles") String roles,
    			@RequestParam("technologies") String technologies) {
    	
    	Systemout.println("Hello World ");
    		return new ModelAndView("cvedit", "cv", new CV());
    	}

  • #2
    The problem here is in your controller. See, when you're using the @MVC model, you cannot return ModelAndView. Spring doesn't recognize that as containing a view name. Instead, it treats it as just another object to be added to the model, at which point it selects a view based on the request URL (hence why you're getting "addProjectExperience.htm").

    The solution is pretty easy. Just rework your controller to return the view name:

    Code:
    @RequestMapping(method=RequestMethod.POST, value="addProjectExperience")	
    public ModelAndView addProjectExperience(@RequestParam(value="cvid", required=false) String cvid,
                                             @RequestParam("exptype") String exptype,
                                             @RequestParam("header") String header,
                                             @RequestParam("description") String description,
                                             @RequestParam("roles") String roles,
                                             @RequestParam("technologies") String technologies,
                                             Model model) {
        System.out.println("Hello World ");
    
        model.addAttribute("cv", new CV());
        return "cvedit";
    }
    (Notice the additional Model parameter. Put your model variables in here)

    Hope this helps
    - Don

    Comment


    • #3
      Thank you very much!

      Comment


      • #4
        Originally posted by phe View Post
        Thank you very much!
        Sorry. Now I am doing as described but I am still getting this.

        http://localhost:8080/cvedit/addProjectExperience.htm

        Comment

        Working...
        X