Spring MVC Missing request attribute

Julien Knepper :

So i'm currently taking a course on udemy about Spring MVC. In the current section there's a simple form being build to submit firstname and lastname.

Hey user, may i know your name?
<form:form action="hello" modelAttribute="info">
    First Name: <form:input path="firstName" />
    Last Name: <form:input path="lastName" />
    <input type="submit" value="Submit" />
</form:form>

The input gets submitted via an Information Class to the HelloController

@Controller
public class HelloController {

@RequestMapping("/hello")
public ModelAndView helloWorld(@RequestAttribute("info") Information userInfo) {
    ModelAndView model = new ModelAndView("hello");

    model.addObject("firstName", userInfo.getFirstName());
    model.addObject("lastName", userInfo.getLastName());

    return model;
}

@RequestMapping("/")
public ModelAndView homepage() {
    ModelAndView model = new ModelAndView("index", "info", new Information());

    return model;
}

Information Class:

public class Information {
private String firstName;
private String lastName;

public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}   
}

Next the informatiion Class should be forwarded to the view file hello.jsp

<body>
<h2> Hello ${firstName} ${lastName} </h2><br/>

</body>

I thought this is actually rather simple, but after submitting the form i get the exception "Missing request attribute 'info' of type Information". I double-checked my code against the code from the udemy instructor, but couldn't find any errors. Can someone help?

On a sidenote, i don't know if it has anything todo with this error, but after adding @Controllerto the Class, auto-completion in eclipse stops working for this Class. After removing the annotation auto-completion starts towork again.

M. Deinum :

You are using the wrong annotation. @RequestAttribute is for retrieving attributes set on the HttpServletRequest using setAttribute. You however want to bind request parameters to an object, for that you should use the @ModelAttribute annotation instead.

@RequestMapping("/hello")
public ModelAndView helloWorld(@ModelAttribute("info") Information userInfo) { ... }

Changing the annotation will make it work.

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

400 Bad request when validation Model Attribute with Spring MVC

Spring request parameter is missing

Gancho Spring MVC Request Interrupted

Ajax post request no Spring MVC

Spring MVC Request method 'PATCH' not supported

Spring MVC get request composite key

POST request not getting mapped to Spring MVC Controller

Spring MVC Controller firing 5 times per request! Why?

request.getServletPath () retornou nulo de Spring MVC

Spring MVC- REST POST - Bad Request 400

Spring MVC definir o atributo para request / model / modelMap

How to handle a request with multiple parameters on Spring-MVC

No mapping found for HTTP request with URI Spring MVC 4

Spring MVC separate request for controller and static resource or html/jsp page

Spring MVC @ModelAttribute retorna o erro 400 "Bad Request"

Spring Boot "HttpMessageNotReadableException Request body is missing" ao tentar fazer uma pós-solicitação

How to correctly handle this AJAX request that send an array to a Spring MVC controller method? Why it can't work?

Can I specify that a controller level request mapping is not valid for a specific method into a Spring MVC controller class?

Spring MVC @RestController -> Colocando resultados em "400 Bad Request" não importa o que eu faça

400 (Bad Request) Error - Spring MVC File Upload usando JQuery AJAX

Spring MVC - passed http request argument not match the same String value, why?

Spring MVC @RestController -> PUTting results in "400 Bad Request" no matter what I do

Laravel request attribute add not appeard

Request just one attribute of a entity

Ajax Post request MVC

Is controller scaffolding missing in MVC 6?

MVC SSO ADFS Claims missing

MVC - Render Value and Name Attribute

Weka GUI: add attribute is-missing-value