我使用Spring MVC,JPA(Hibernate)和Jsp。
设计了一个Login页面请找到login.jsp: -
src/
profilePage.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>
LoginController.java: -
<%@ 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>
在Login.jsp页面中提供有效详细信息后,它将重定向到profilePage.jsp,但profilePage.jsp中的数据不正确。请帮我理解我在做错的地方。
profilePage.jsp显示如下: -
用户名密码 $ {staff.userName} $ {staff.password}
未显示此变量的值。
答案 0 :(得分:0)
profilePage.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";
}