@RequestAttribute in springMVC is not binding Obje

2019-09-21 02:36发布

This question is an exact duplicate of:

I am new to spring,i am trying to develop basic java project with spring MVC using Annotations,i am trying to create an object of my Entity class(Information.java) in the controller using @RequestAttribute and send it to the view,my code as follows

My controller class

 package org.practice.spring;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView

    @Controller()
    public class HelloController
    {

        @RequestMapping("/hello")
        public ModelAndView helloWorld(@RequestAttribute 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;
        }
    }

My Entity class

package org.practice.spring;

public class Information {
    private String firstName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    private String lastName;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
   }

Index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>Hello,Please Enter Name</h3>
<form:form action="hello" modelAttribute="info">
First Name:<form:input path="firstName"/><br>
Last Name:<form:input path="lastName"/><br>
<input type="submit" value="Submit">

</form:form>
</body>
</html>

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
Hello World
<h3>Hello ${firstName} ${lastName}</h3>

</body>
</html>

and Tomcat is giving me this

org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver logException WARNING: Resolved [org.springframework.web.bind.ServletRequestBindingException: Missing request attribute 'userInfo' of type Information]

When i run the application browser is giving me this

HTTP Status 400 – Bad Request Type Status Report

Message Missing request attribute 'userInfo' of type Information

Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

So far i dont have any syntax errors in my code,but my code is not working,i struck here for almost 2 days,did alot of googling but no luck,any help would be appreciated.

1条回答
Lonely孤独者°
2楼-- · 2019-09-21 03:24

You can try this: @RequestAttribute(name="info").

查看更多
登录 后发表回答