简体   繁体   中英

How to pass model(bean) value using ModelAndview from controller to jsp page?

I am creating Spring mvc login application, so while registration or login I am simply getting ${attributename} (in my case ${firstname}) without actual attribute value in welcome jsp. Kindly let me know whats the problem with my code. Userbean and welcome jsp has same attibute name.

LoginController :

    @Controller
public class LoginController {

    @Autowired
    private UserService userService;
    @GetMapping ("/login")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mav = new ModelAndView("login");
        mav.addObject("login",new Login());
        return mav;

    }

    @PostMapping(value = "/loginProcess")
    public ModelAndView loginProcess(Model model, HttpServletRequest request, HttpServletResponse response, @ModelAttribute("login") Login login){
         ModelAndView mav = null;
        User user = userService.validateUser(login);
        if(user!=null){
            mav= new ModelAndView("welcome");
            System.out.println("user.getFirstname(): "+user.getFirstname());             
            mav.addObject("firstname", user.getFirstname());
            //model.addAttribute("firstname", "welcome"+user.getFirstname());


        }
        else

            mav = new ModelAndView("login");
        mav.addObject("error", "Username or password is wrong");
        return mav;


    }

}

login jsp:

 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>login</title>
</head>
<body>
<form:form id="loginForm" modelAttribute="login" action="loginProcess" method="post">
                <table align="center">
                    <tr>
                        <td>
                            <form:label path="username">Username: </form:label>
                        </td>
                        <td>
                            <form:input path="username" name="username" id="username" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <form:label path="password">Password:</form:label>
                        </td>
                        <td>
                            <form:password path="password" name="password" id="password" />
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td align="left">
                            <form:button id="login" name="login">Login</form:button>
                        </td>
                    </tr>
                    <tr></tr>
                    <tr>
                        <td></td>
                        <td><a href="home.jsp">Home</a>
                        </td>
                    </tr>
                </table>
            </form:form>
            <table align="center">
                <tr>

             <td style="font-style: italic; color: red;">${param.message}</td>
                </tr>
            </table>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Welcome</title>
</head>
<body>
    <table>
        <tr>
            <td> ${firstname}</td>
        </tr>
        <tr>
        </tr>
        <tr>
        </tr>
        <tr>
            <td><a href="home.jsp">Home</a>
            </td>
        </tr>
    </table>
</body>
</html>

userbean:

public class User {

      private String username;
      private String password;
      private String firstname;
      private String lastname;
      private String email;
      private String address;
      private int phone;
 //getter, setter
}

Can you please modify this in your jsp page?

    <tr>
        <td> "${firstname}"</td>
    </tr>

Just added double quotes. Also add

<%@ page isELIgnored="false" %>

this to your JSP file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM