为什么我不能从模型中检索属性但我能从JSP中获取?

时间:2017-11-08 21:21:34

标签: java spring spring-mvc

我正在学习spring mvc并理解Model和ModelAttribute的使用。但是,我无法检索Model的属性,因此我使用了JSP param值。我究竟做错了什么?我检查了模型仍然使用@ModelAttribute("username") User user / user.username得到了属性的值,而且确实如此。

控制器

 package login.user;

 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.RequestMapping;

 @Controller
 public class LoginUser {

    @RequestMapping("/")
    public String showMenu()
    {
        return "menu";
    }

    @RequestMapping("login")
    public String loginUser(Model model)
    {
        User newUser = new User();
        model.addAttribute("user", newUser);
        return "login-user";
    }

    @RequestMapping("processUser")
    public String processUser(Model model)
    {
        Option newOption = new Option();
        model.addAttribute("option", newOption);
        return "process-login";
    }
}

用户

package login.user;

public class User {

    private String username;
    private char password[];

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public char[] getPassword() {
        return password;
    }
    public void setPassword(char[] password) {
        this.password = password;
    }

}

登录-user.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<link href="<c:url value="/resources/css/style.css" />" rel="stylesheet"  type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login User</title>
</head>
<body>
    <div>
        <form:form action="processUser" modelAttribute="user">
            <table>
            <tr>
                <td>Username:</td>
                <td>
                     <form:input path="username" />
                </td>
            </tr>
            <tr>
                <td>Password:</td>
                <td>
                    <form:input type="password" path="password" />
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" name="Login" />
                </td>
            </tr>
        </table>
        </form:form>
    </div>

</body>
</html>

过程的login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form:form modelAttribute="option">
        <h3>Welcome ${param.username}.  Please choose an activity below</h3>  <---- changed from ${user.username}
        <div>   
            <form:select path="option">
                <form:option value="Email" label="Email"></form:option>
                <form:option value="Enter Recipe" label="Enter Recipe"></form:option>
                <form:option value="Retrieve Recipe" label="Retrieve Recipe"></form:option>
            </form:select>
        </div>
    </form:form>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

spring模型数据存储在标准Java请求范围中。如果您尝试获取用户名,则需要再次将其添加到请求范围中。因此,您可以按如下方式编写processUser方法。

@RequestMapping("processUser")
    public String processUser(@ModelAttribute("user") User user,Model model)
    {
        Option newOption = new Option();
         // do stuff with user data
        newOption.setUsername(user.getUsername()); 
        model.addAttribute("option", newOption);
        return "process-login";
    }

所以,你应该能够在jsp中以${option.username}的形式获得它。

答案 1 :(得分:1)

只需更改上一个@RequestMapping方法,如下所示:

@RequestMapping("processUser")
    public String processUser(@ModelAttribute("user") User user, Model model)
    {
        Option newOption = new Option();
        model.addAttribute("user", user);
        model.addAttribute("option", newOption);
        return "process-login";
    }

现在在jsp。

中使用$ {user.username}