我想用一個jsp檔案來注冊客戶、供應商和托運人,但是當我用<c:if>標簽運行程式時,雖然System.out.print(type)顯示值為 "供應商",但所有表單都被顯示出來。
我怎樣才能解決這個問題呢?
下面我展示了 Supplier.jsp 和 Sign_in.jsp 的代碼。對于客戶和托運人的注冊,以Supplier.jsp同樣的方式實作了一個jsp,只用字串 "customer "和 "shipper "修改了型別變數的值。這些頁面將顯示客戶、托運人和供應商類中每一個的更具體的值。
Supplier.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"/span> prefix="c"/span>%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Bienvenido! </title>
</head>
<body>
<h2>登錄</h2>
<a href="Sign_in.jsp">還沒有賬戶?請登錄</a>。
<% request.getSession().setAttribute("type", "supplier"); %>
</body>
</html>
Register.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"/span> prefix="c"/span>%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>登錄</title>
</head>
<body>
<h1>登錄</h1>
<% String type = (String) request.getSession().getAttribute("type"/span>) 。
String supplier = "supplier"。
String shipper = "shippper";
String client = "client";
%>
<% System.out.println(type.equals(supplier)); %>
<c:if test="${type == supplier}"/span>>
<h2>我是一個供應商</h2>。
<form action="RegisterServlet" method="post" >
<input type="email" name="email" placeholder="Email">
<input type="password" name="password" placeholder="密碼">
<按鈕型別="提交">注冊</按鈕>。
</form>
</c:if>
<c:if test="${type == shipper}">
<h2>我是一個發貨人</h2>。
<form action="RegisterServlet"/span> method="post"/span>>
<input type="email" name="email" placeholder="email">。
<input type="text" name="user" placeholder="user">
<輸入型別="密碼" name="密碼" placeholder="密碼">
<按鈕型別="submit">注冊</按鈕>。
</form>
</c:if>
<c:if test="${type == client}"/span>>
<h2>我是一個客戶</h2>。
<form action="RegisterServlet" method="post" >
<input type="email" name="email" placeholder="Email">
<input type="text" name="user" placeholder="User">
<input type="password" name="password" placeholder="Password">
<input type="text" name="city" placeholder="City">
<input type="text" name="address" placeholder="Address">
<輸入型別="文本" name="國家" placeholder="國家">
<按鈕型別="提交">注冊</按鈕>。
</form>
</c:if>
</body>
</html>
uj5u.com熱心網友回復:
你需要改變你的test條件,以使用常量作為
<c:if test="${type == 'supplier'}"/span>>
<c:if test="${type == 'shipper'}"/span>>
<c:if test="${type == 'client'}"/span>>
因為supplier、shipper和client是區域變數而不是范圍屬性。
如果在這一改變后沒有一個表單呈現,那么你的type屬性就沒有被正確設定。檢查你的hrefs,看看你是否導航到了正確的JSP頁面。你正在除錯Register.jsp,但你的鏈接卻指向Sign_in.jsp,所以這也可能是另一個問題。
@MauricePerry提出了一個很好的觀點,你應該避免使用<% scriptlets %>,因為你已經在使用JSTL標簽。例如,你的代碼添加了一個session屬性。
<% request.getSession().setAttribute("type", "supplier"); %>
可以用<c:set> JSTL標簽代替。
<c:set var="type" value="supplier" scope="session" />
在view層中使用<% scriptlets %>是相當不受歡迎的,所以,盡量將其使用限制在快速除錯上。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/315169.html
標籤:
