java 模型数据未传输到jsp页面

u0njafvf  于 2023-02-02  发布在  Java
关注(0)|答案(2)|浏览(131)

我设计了一个使用Spring MVC,JPA(Hibernate)和Jsp的登录页面。
请查找login.jsp:-

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <body>
        <h2>Login page</h2>
        <form:form method="POST" modelAttribute="loginBean" action="showProfile">
            <table>
                <tr>
                    <td>UserName :<form:input path="userName"/></td>
                </tr>
                <tr>
                    <td>Organization Id :<form:input path="OrgId" /></td>
                </tr>
                <tr>
                    <td>Password : <form:input path="password" /></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="Submit" /></td>
                </tr>
            </table>
        </form:form>
    </body>
    </html>

配置文件页面. jsp:-

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!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>Insert title here</title>
    </head>
    <body>
        <table border="2" width="70%" cellpadding="2">
            <tr>
                <th>UserName</th>
                <th>Password</th>
            </tr>
            <c:forEach var="staff" items="${staffData}">
                <tr>
                    <td>${staff.userName}</td>
                    <td>${staff.password}</td>
                </tr>
            </c:forEach>
        </table>
    </body>
    </html>

LoginController.java:-

@Controller
public class LoginController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String showLoginPage(Model model) {
        model.addAttribute("loginBean", new LoginBean());
        return "login";
    }

    @RequestMapping(value = "/showProfile", method = RequestMethod.POST)
    public ModelAndView redirectToprofile(@ModelAttribute("loginBean") LoginBean loginBean) {
        StaffServiceImpl staffServiceImpl = new StaffServiceImpl();
        Staff staff = staffServiceImpl.authenticateStaff(loginBean);
        if (null != staff) {
            return new ModelAndView("redirect:/profilePage","staffData",staff);
        }
        return new ModelAndView("profileNotFound");
        }
@RequestMapping(value = "/profilePage", method = RequestMethod.GET)
    public String showProfilePage() { 
        return "profilePage";
    }

在Login.jsp页面中提供有效的详细信息后,它将重定向到profilePage.jsp,但profilePage.jsp中的数据不正确。请帮助我了解我犯错误的地方。
profilePage.jsp显示如下:-
用户名密码${人员.用户名} ${人员.密码}
未显示此变量的值。

vltsax25

vltsax251#

配置文件页面. jsp:在页面指令中添加isELIgnored="false"

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1" isELIgnored="false"%>

使用以下内容更新控制器:

@Controller
public class LoginController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String showLoginPage(Model model) {
        model.addAttribute("loginBean", new LoginBean());
        return "login";
    }

    @RequestMapping(value = "/showProfile", method = RequestMethod.POST)
    public String redirectToprofile(@ModelAttribute("loginBean") LoginBean loginBean , Model model) {
        StaffServiceImpl staffServiceImpl = new StaffServiceImpl();
        Staff staff = staffServiceImpl.authenticateStaff(loginBean);
        if (null != staff) {
           model.addAttribute("staffData",staff);
           return "profilePage";
        }
        return "profileNotFound";
    }
7z5jn7bk

7z5jn7bk2#

以上答案是正确,下面是一个解释:isELIgnored属性使您能够禁用JSP 2.0中引入的表达式语言(EL)表达式的求值。
该属性的默认值为true,表示表达式${...}将按照JSP规范的规定进行计算。如果该属性设置为false,则不计算表达式,而是将其视为静态文本。源:在此处输入链接说明

相关问题