Announcement Announcement Module
Collapse
No announcement yet.
Problem in passing url parameters Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


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


      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?

      Comment


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

        Comment


        • #5
          I have similiar problem..Have you solved the problem?

          Comment

          Working...
          X