Announcement Announcement Module
Collapse
No announcement yet.
Cannot get request.getParameter in onSubmit method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cannot get request.getParameter in onSubmit method

    I'm using latest Spring3.1

    In jsp, it sends an argument to UserFormController.java as follow.
    Following is parts of jsp code
    Code:
    <input type="button" value="Add" onclick="location.href='/springapp/userForm.htm?method=Add'"  />
    <display:column property="id" escapeXml="true" sortable="true" titleKey="<form:input path='users.id' />"
    title="id" style="width: 25%" url="/userForm.htm?method=Edit" paramId="id" paramProperty="id"/>
    In UserFormController.java,
    I can get the value of argument method as a Add or Edit in showUserForm method.
    But I cannot get the value of argument 'method' as a Add or Edit in onSubmit method.

    I want very simple - I want to insert or update user info. according to the value of 'method' argument
    sent from the jsp.

    Moreover, lots of methods are deprecated such as onSumit, processFormSubmit etc in Spring 3.1 API
    Are there any methods which replace these deprecated methods that I can usefully use in the controller?
    Is there any material regarding it?

    UserFormController.java,
    ---------------------------------------------------------------------------------------------------------------------------------------
    Code:
    @Controller
    @RequestMapping("/userForm*")
    @SessionAttributes("user")
    public class UserFormController {
    	protected final Log logger = LogFactory.getLog(getClass());
    	private UserDao userDao;
    	
    	@Autowired
    	public void setUserDao(UserDao userDao) {
    		this.userDao = userDao;
    	}
    
    	@RequestMapping(method = RequestMethod.GET)
    	public User showUserForm(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
    		User user = new User();
    		model.addAttribute(user);
    		
    		String method = request.getParameter("method");   // This is working! OK!
    		if(method != null)
    			System.out.println("[ showUserForm --> GET --> METHOD ] " + method);
    		
    		if(request.getParameter("id") != null ) {
    			String id = request.getParameter("id");
    			return userDao.getUser(id);
    		} else	
    			return user;
    	}
    
    	@RequestMapping(method=RequestMethod.POST)
    	public String onSubmit(@ModelAttribute("user") User user, BindingResult errors, 
    			HttpServletRequest request, HttpServletResponse response) {
    		
    		String method = request.getParameter("method");   // This is NOT working, so print nothing the following 2 lines.
    		if(method != null)         
    			System.out.println("[ showUserForm --> POST --> METHOD ] " + method);
    
    		// Save to DB
    		if(method != null && method.equalsIgnoreCase("Edit")) {
    			userDao.modifyUser(user);
    		} else {
    			userDao.addUser(user);
    		}
    		
    		return "redirect:/users.htm";
    	}
    ---------------------------------------------------------------------------------------------------------------------------------------
    Last edited by eva500; Mar 26th, 2012, 04:38 PM.

  • #2
    Please use [ code ][/code ] tags when posting code that way it remains readable.

    Why should the parameter be available? There is no POST form submit going to happen so of course it isn't there.

    Comment

    Working...
    X