0

If I have a Spring form as follows:

<c:url value="/user/profile?id=${user.id}" var="profileURL" />  
    <form:form action="${profileURL}" method = "get">                       
        <input type="submit" value="My profile">                           
    </form:form>   

Spring complains that:

Your page request has caused a MissingServletRequestParameterException: Required String parameter 'id' is not present error:

However, in the controller method, the parameter is present:

@RequestMapping(value = "/profile", method = RequestMethod.GET)
public String getProfile(@RequestParam("id") String id, Model model) {    

    User user = userService.get(Integer.parseInt(id));         
    model.addAttribute("user", user);
    return "profile";
}

Can anyone advise why this is?

If I use a simple form field like:

<input name="id" type="hidden" value="=${user.id}">  

Everything is good. But I prefer to use a Spring field, hidden of necessary.

At run-time the form looks like this:

<form id="command" action="/nameOfapplication/user/profile?id=3" method="get">                       
        <input type="submit" value="My profile">                           
    </form>   

This causes the following messages in the stacktrace:

Your page request has caused a MissingServletRequestParameterException: Required String parameter 'id' is not present error:

org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:255)

Mr Gwent
  • 57
  • 1
  • 8
  • 1
    Can you open your browser's network console and check what is sent in the failing request? Also what does the action attribute look like when rendered? – Sotirios Delimanolis Jun 17 '14 at 14:09
  • I've added some details. The form renders correctly. – Mr Gwent Jun 17 '14 at 14:18
  • Aha, it's simply how HTML specifies the behavior of a get request from a form. It clears anything after a `?` in the `action` url. – Sotirios Delimanolis Jun 17 '14 at 14:22
  • Alright. I wasn't aware that GET cleared the any parameters in the query string. But are you able to advise concerning use of a hidden spring field in the form's body, i.e. the Spring equivalent of: at all? – Mr Gwent Jun 17 '14 at 14:29
  • There's ``. Is that what you're looking for? – Sotirios Delimanolis Jun 17 '14 at 14:30
  • I think so but I've not been able to get it to work. I've tried things like: but it rejects because of Neither BindingResult nor plain target object for bean name 'command' available as request attribute. id in this context is not part of a bean being validated. – Mr Gwent Jun 17 '14 at 14:33
  • Yeah. Spring's `form` is usually used to construct forms from model objects (form backing objects), not simple request parameters. If you're not doing that, a normal `` is fine. – Sotirios Delimanolis Jun 17 '14 at 14:38
  • That is a possible flaw in Spring because I wanted to try to use a validator on another question: http://stackoverflow.com/questions/24253556/how-to-validate-requestparam-in-spring-to-use-controller-and-validator which is similar where I'm trying to validate an entered id but before it is used as a bean attribute. – Mr Gwent Jun 17 '14 at 14:40
  • You could wrap the `id` in a `IdHolder` object and validate that...Seems excessive. Your form would then be backed by a `IdHolder` object. – Sotirios Delimanolis Jun 17 '14 at 14:46
  • I thought of that but like the method as it stands, but yes, it does seem excessive. – Mr Gwent Jun 17 '14 at 14:47

0 Answers0