简体   繁体   English

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

[英]Why can't I retrieve an attribute from the Model but I'm able to from JSP?

I'm learning spring mvc and understand the use of the Model and ModelAttribute. 我正在学习spring mvc,并了解Model和ModelAttribute的用法。 However, I can't retrieve the Model's attribute so I resorted to using the JSP param value. 但是,我无法检索Model的属性,因此我求助于使用JSP param值。 What am I doing wrong? 我究竟做错了什么? I checked the model still had a value for the attribute using @ModelAttribute("username") User user / user.username and sure enough it does. 我使用@ModelAttribute("username") User user / user.username检查模型是否仍然具有该属性的值,并确保足够。

Controller 调节器

 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";
    }
}

User 用户

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;
    }

}

login-user.jsp 登录-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>

process-login.jsp 过程的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>

The spring model data is stored in the standard Java request scope. 弹簧模型数据存储在标准Java请求范围中。 If you are trying to get the username, you need to add it in the request scope again. 如果尝试获取用户名,则需要再次将其添加到请求范围中。 So, you can write the processUser method as follows. 因此,您可以编写如下的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";
    }

So, you should be able to get it in the jsp as ${option.username} . 因此,您应该能够在jsp中将其作为${option.username}

Just change your last @RequestMapping method as follows: 只需更改您的最后一个@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";
    }

Now use ${user.username} in jsp. 现在在jsp中使用$ {user.username}。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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