Jsp - 两页合二为一
Jsp - two pages in one
所以我刚开始在 jsp 中编写我的第一个代码。
我做了一个登录页面,用户写下他的用户名和密码,如果它们中的任何一个不存在于数据库中(MySQl),它会在同一页面中打印一条消息(用户认为这是同一页)。
我通过将信息发送到文件(在数据库中查找)来做到这一点,如果信息正确则转到下一页,如果错误则转到错误页面。
现在我的问题是,如果我有两个代码相同的页面(Login.jsp 和错误Login.jsp),唯一的区别是额外的错误消息如何加入它们?
我可以避开我的两个相同的页面吗? :/
我的第一个想法是放置一个 if 语句,但它在这种语言中令人困惑。
这是我创建的 2 个文件(我不认为 Validation_Login.jsp 对此很重要,但如果它让我知道上传它)
Login.jsp
<body>
<form action="Validation_Login.jsp">
<font size="+3"> Login </font>
<br/><br/>
<div>
USERNAME: <input type="text" placeholder="Enter Username" name="firstname"/>
<br/><br/>
</div>
<div>
PASSWORD: <input type="password" placeholder="Password" name="Password" />
<br/><br/>
</div>
<div>
<input type="submit" value="login" />
</div>
</form>
<form action="Create_Account.jsp">
<input type="submit" value="Create_Account">
</form>
</body>
错误Login.jsp
<body>
<form action="Validation_Login.jsp" >
<font size="+3"> Login </font>
<br/><br/>
<div>
USERNAME: <input type="text" placeholder="Enter Username" name="firstname"/>
<br/><br/>
</div>
<div>
PASSWORD: <input type="password" placeholder="Password" name="password" />
<br/><br/>
</div>
<div>
<input type="submit" value="login" />
</div>
</form>
<form action="Create_Account.jsp">
<input type="submit" value="Create_Account">
</form>
<div id="message">
Wrong UserName Or Password
<br/>
Please Try Again
</div>
</body>
您可以很容易地做到这一点,方法是在检查登录的代码中设置一些标志(在您未发布的页面中),然后在 login.jsp 中检查此标志以确定是否显示错误信息。
您可以使用 Java 标准标签库来执行此操作。
https://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm
在两个 JSP 中添加指令:
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
登录失败:
<c:set var="loginFailed value="true" scope="request"/>
登录页面:
<c:if test="${loginFailed eq true}">
<div id="message">
Wrong UserName Or Password. Please Try Again
</div>
</c:if>
实现此目的的最佳方法是使用 jstl 标签 "include"
<jsp:include page="./path-to-template/template_name.jsp">
<jsp:param name="content" value="content_page_name"></jsp:param>
</jsp:include>
要做到这一点,"template_name" jsp 页面的内容应该是:
<%@page contentType="text/html" pageEncoding="ISO-8859-6"%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>MyGrowth - Track Education While in Migration</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<body>
<div class="container">
<%@include file="header.jsp" %>
<jsp:include page="../content/${param.content}.jsp"></jsp:include>
</div>
</body>
<%@include file="footer.jsp"%>
</html>
关键部分是在何处包含从参数获得的页面:
<jsp:include page="../content/${param.content}.jsp"></jsp:include>
因此,当我们将名称为 'content_page_name' 的页面传递给模板页面时,该页面将插入 'include' 标签所在的位置,在 'template' 页面中.
*注意:content
页面应仅包含 html 主体元素(因为头部已在模板页面中定义。
所以我刚开始在 jsp 中编写我的第一个代码。
我做了一个登录页面,用户写下他的用户名和密码,如果它们中的任何一个不存在于数据库中(MySQl),它会在同一页面中打印一条消息(用户认为这是同一页)。
我通过将信息发送到文件(在数据库中查找)来做到这一点,如果信息正确则转到下一页,如果错误则转到错误页面。
现在我的问题是,如果我有两个代码相同的页面(Login.jsp 和错误Login.jsp),唯一的区别是额外的错误消息如何加入它们?
我可以避开我的两个相同的页面吗? :/ 我的第一个想法是放置一个 if 语句,但它在这种语言中令人困惑。
这是我创建的 2 个文件(我不认为 Validation_Login.jsp 对此很重要,但如果它让我知道上传它)
Login.jsp
<body>
<form action="Validation_Login.jsp">
<font size="+3"> Login </font>
<br/><br/>
<div>
USERNAME: <input type="text" placeholder="Enter Username" name="firstname"/>
<br/><br/>
</div>
<div>
PASSWORD: <input type="password" placeholder="Password" name="Password" />
<br/><br/>
</div>
<div>
<input type="submit" value="login" />
</div>
</form>
<form action="Create_Account.jsp">
<input type="submit" value="Create_Account">
</form>
</body>
错误Login.jsp
<body>
<form action="Validation_Login.jsp" >
<font size="+3"> Login </font>
<br/><br/>
<div>
USERNAME: <input type="text" placeholder="Enter Username" name="firstname"/>
<br/><br/>
</div>
<div>
PASSWORD: <input type="password" placeholder="Password" name="password" />
<br/><br/>
</div>
<div>
<input type="submit" value="login" />
</div>
</form>
<form action="Create_Account.jsp">
<input type="submit" value="Create_Account">
</form>
<div id="message">
Wrong UserName Or Password
<br/>
Please Try Again
</div>
</body>
您可以很容易地做到这一点,方法是在检查登录的代码中设置一些标志(在您未发布的页面中),然后在 login.jsp 中检查此标志以确定是否显示错误信息。
您可以使用 Java 标准标签库来执行此操作。
https://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm
在两个 JSP 中添加指令:
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
登录失败:
<c:set var="loginFailed value="true" scope="request"/>
登录页面:
<c:if test="${loginFailed eq true}">
<div id="message">
Wrong UserName Or Password. Please Try Again
</div>
</c:if>
实现此目的的最佳方法是使用 jstl 标签 "include"
<jsp:include page="./path-to-template/template_name.jsp">
<jsp:param name="content" value="content_page_name"></jsp:param>
</jsp:include>
要做到这一点,"template_name" jsp 页面的内容应该是:
<%@page contentType="text/html" pageEncoding="ISO-8859-6"%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>MyGrowth - Track Education While in Migration</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<body>
<div class="container">
<%@include file="header.jsp" %>
<jsp:include page="../content/${param.content}.jsp"></jsp:include>
</div>
</body>
<%@include file="footer.jsp"%>
</html>
关键部分是在何处包含从参数获得的页面:
<jsp:include page="../content/${param.content}.jsp"></jsp:include>
因此,当我们将名称为 'content_page_name' 的页面传递给模板页面时,该页面将插入 'include' 标签所在的位置,在 'template' 页面中.
*注意:content
页面应仅包含 html 主体元素(因为头部已在模板页面中定义。