Results 1 to 5 of 5

Thread: Problem in passing url parameters

  1. #1

    Default Problem in passing url parameters

    Hi,

    I am learning Spring MVC. I am trying to pass a url parameter from a page to itself when a form is submitted. Here is the code:

    Code:
    @Controller
    @RequestMapping(value = "/home.html")
    public class HomeController {
    
    	@RequestMapping(method = RequestMethod.GET)
    	public String showForm(
    			@RequestParam(value = "var", required = false) String var,
    		return "home";
    	}
    
    	@RequestMapping(method = RequestMethod.POST)
    	public ModelAndView addFeed(
    			@RequestParam(value = "var", required = false) String var,
    			@Valid FeedDomain feedDomain, BindingResult result, HttpServletRequest request,  
                HttpServletResponse response) throws Exception {       
    
    		System.out.println("VAlue obained " + var);
    		System.out.println(request.getParameter("var"));
    		
    		try {
    			if (result.hasErrors()) {
    				// if validator failed
    				return new ModelAndView("home");
    			} else {
    				ModelAndView modelAndView = new ModelAndView(new RedirectView("home?var=done"));
    				return modelAndView;
    			}
    		} catch (Exception exception) {
    			exception.printStackTrace();
    			throw exception;
    		}
    	}
    
    }
    But when I submit the form, I am getting null value for var variable. Am I passing the variable correctly?

  2. #2

    Default

    Actually I am not able to pass parameter to view from a controller. I have tried redirect also but I ma gettign same null value. Please help me out here...

  3. #3

    Default

    Done....i need to pass modelandview data type with var as attribute added in it. That variable can be accessed in jsp page.


    Quote Originally Posted by kshitiz agarwal View Post
    Hi,

    I am learning Spring MVC. I am trying to pass a url parameter from a page to itself when a form is submitted. Here is the code:

    Code:
    @Controller
    @RequestMapping(value = "/home.html")
    public class HomeController {
    
    	@RequestMapping(method = RequestMethod.GET)
    	public String showForm(
    			@RequestParam(value = "var", required = false) String var,
    		return "home";
    	}
    
    	@RequestMapping(method = RequestMethod.POST)
    	public ModelAndView addFeed(
    			@RequestParam(value = "var", required = false) String var,
    			@Valid FeedDomain feedDomain, BindingResult result, HttpServletRequest request,  
                HttpServletResponse response) throws Exception {       
    
    		System.out.println("VAlue obained " + var);
    		System.out.println(request.getParameter("var"));
    		
    		try {
    			if (result.hasErrors()) {
    				// if validator failed
    				return new ModelAndView("home");
    			} else {
    				ModelAndView modelAndView = new ModelAndView(new RedirectView("home?var=done"));
    				return modelAndView;
    			}
    		} catch (Exception exception) {
    			exception.printStackTrace();
    			throw exception;
    		}
    	}
    
    }
    But when I submit the form, I am getting null value for var variable. Am I passing the variable correctly?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    For starters your controller is mapped to /home.html but you are redirecting to home... Next you shouldn't put the variable in your url but pass it as part of the model. So in short fix your code.

    Code:
    ModelAndView mav = new ModelAndView("redirect:home.html");
    mav.addObject("var", "done");
    return mav;
    Edit: For some reason this post hanged for a couple of days :S...
    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

  5. #5
    Join Date
    Jan 2013
    Location
    London, UK
    Posts
    1

    Default

    I have similiar problem..Have you solved the problem?

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
  •