關於表單頁面的小結

最近一直在學習心得知識點,對之前學習表單等基礎知識忘記了許多,在此做一個簡單的總結,方便以後學習查閱。

1.想要做一個如下圖所示效果的表單頁面

這裏寫圖片描述

2.大體的框架需要以下標籤

這裏寫圖片描述

這裏寫代碼片

3.表單提交的servlet標準寫法

這裏寫圖片描述

<form action="${pageContext.request.contextPath}/addCustomerServlet" method="post">

4.數據回顯的判斷,

*首先需要引入標籤庫,函數庫

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

在轉發之前把數據封裝到request裏面

// 把郵箱存在的錯誤信息轉發出去
request.setAttribute("old", c);// 如果錯誤把錯誤信息保存起來
request.setAttribute("email_exist_error", "郵箱已經存在");
// 如果郵箱錯誤,信息回顯
request.getRequestDispatcher("/addCustomer.jsp").forward(request, response);

*1.文本框的回顯

<input type="text" name="name" value="${old.name }">

*2.單選框的回顯
利用標籤進行判斷,checked=”checked”可以簡寫成CHECKED;

<tr>
<td>選擇性別:</td>
<td><input type="radio" name="gender" value="男"
<c:if test="${old.gender=='男'}" > CHECKED</c:if>>男 
<input type="radio" name="gender" value="女"
<c:if test="${old.gender=='女'}" > CHECKED</c:if>>女</td>
</tr>

*3.多選框的回遷
方式一:利用jstl中的contains函數

<tr>
<td>客戶愛好:</td>

<td><input type="checkbox" name="hobby" value="籃球"
<c:if test="${fn:contains(old.hobby,'籃球')}" > CHECKED</c:if>>籃球

<input type="checkbox" name="hobby" value="足球"
<c:if test="${fn:contains(old.hobby,'足球')}" > CHECKED</c:if>>足球
<input type="checkbox" name="hobby" value="乒乓球"
<c:if test="${fn:contains(old.hobby,'乒乓球')}" > CHECKED</c:if>>乒乓球

<input type="checkbox" name="hobby" value="檯球"
<c:if test="${fn:contains(old.hobby,'檯球')}" > CHECKED</c:if>>檯球

<input type="checkbox" name="hobby" value="電影"
<c:if test="${fn:contains(old.hobby,'電影')}" > CHECKED</c:if>>電影
<input type="checkbox" name="hobby" value="看書"
<c:if test="${fn:contains(old.hobby,'看書')}" > CHECKED</c:if>>看書</td>
</tr>

方式二:利用標籤庫的 切割 c:forTokens
根據jdbcUtil封裝實體對象的,字符串連接的方式
這裏寫圖片描述

3.下拉框的回顯

<tr>
<td>客戶類型:</td>
<td><
select name="type">

<option value="普通客戶"
<c:if test="${old.type == '普通客戶'}" > SELECTED</c:if>>普通客戶</option>

<option value="黃金客戶"
<c:if test="${old.type == '黃金客戶'}" > SELECTED</c:if>>黃金客戶</option>

<option value="鑽石客戶"
<c:if test="${old.type == '鑽石客戶'}" > SELECTED</c:if>>鑽石客戶</option>

</select>
</td>
</tr>

總結利用web域對象傳遞數據的時候,起名字一定要有意義,有規範,同一個項目中一般不要出現相同的名字。
後續再jsp頁面中可以直接省略前面的域對象,直接使用old.name;這樣的方式,tomcat自動會從小到大範圍尋找,所以後面寫的就簡單多了!!!

request.setAttribute("old", c);// 如果錯誤把錯誤信息保存起來
request.setAttribute("email_exist_error", "郵箱已經存在");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章