发布于 2016-10-10 09:37:11 | 98 次阅读 | 评论: 0 | 来源: 网友投递
JSTL JSP 标准标签库
JSP 标准标记库(JSP Standard Tag Library,JSTL)是一个实现 Web 应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断、数据管理格式化、XML 操作以及数据库访问。在 developerworks 上其新系列的第一篇文章中,软件工程师 Mark Kolb 向您展示了如何使用 JSTL 标记来避免在 JSP 页面中使用脚本编制元素。您还将了解如何通过从表示层删除源代码来简化软件维护。最后,您将了解 JSTL 经过简化的表达式语言,它允许在不必使用功能齐全的编程语言的情况下对 JSTL 操作指定动态属性值。
普通方式:
register.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=utf-8"%>
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3 <html>
4 <head>
5 </head>
6
7 <body>
8 <h1>注册用户</h1>
9 <form action="registerdo.jsp" method="post">
10 <table>
11 <tr>
12 <td><label for="txtUserName">用户名:</label></td>
13 <td><input type="text" name="txtUserName" id="txtUserName"/></td>
14 </tr>
15 <tr>
16 <td><label for="txtLoginPwd">密码:</label></td>
17 <td><input type="password" name="txtLoginPwd" id="txtLoginPwd"/></td>
18 </tr>
19 <tr>
20 <td><label for="rdoMan">性别:</label></td>
21 <td><input type="radio" name="rdoSex" checked="checked" value="1" id="rdoMan"/><label for="rdoMan">男士</label>
22 <input type="radio" name="rdoSex" value="0" id="rdoWoman"/><label for="rdoWoman">女士</label>
23 </td>
24 </tr>
25 <tr>
26 <td><label for="selEdu" id="selEdu">学历:</label></td>
27 <td>
28 <select name="selEdu" id="selEdu">
29 <option value="小学">小学</option>
30 <option value="中学">中学</option>
31 <option value="大学">大学</option>
32 </select>
33 </td>
34 </tr>
35 <tr>
36 <td><label for="chkLan">爱好:</label></td>
37 <td>
38 <input type="checkbox" name="chkHibby" id="chkLan" value="篮球"/><label for="chkLan">篮球</label>
39 <input type="checkbox" name="chkHibby" id="chkZu" value="足球"/><label for="chkZu">足球</label>
40 <input type="checkbox" name="chkHibby" id="chkPai" value="排球"/><label for="chkPai">排球</label>
41 </td>
42 </tr>
43 <tr>
44 <td colspan="2" style="text-align:center">
45 <input type="submit" value="注册"/>
46 <input type="reset" value="重填"/>
47 </td>
48 </tr>
49 </table>
50 </form>
51 </body>
52 </html>
registerdo.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=utf-8"%>
2 <%
3 request.setCharacterEncoding("utf-8");
4 String userName = request.getParameter("txtUserName");
5 String loginPwd = request.getParameter("txtLoginPwd");
6 String sex = request.getParameter("rdoSex");
7 sex = sex.equals("1")? "男" :"女";
8 String edu = request.getParameter("selEdu");
9 String[] hibbys = request.getParameterValues("chkHibby");
10 %>
11 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
12 <html>
13 <head>
14 </head>
15 <body>
16 <h1>request.getParameter()</h1>
17 <table>
18 <tr>
19 <td>用户名:</td>
20 <td><%=userName %></td>
21 </tr>
22 <tr>
23 <td>密码:</td>
24 <td><%=loginPwd %></td>
25 </tr>
26 <tr>
27 <td>性别:</td>
28 <td><%=sex %></td>
29 </tr>
30 <tr>
31 <td>学历:</td>
32 <td><%=edu %></td>
33 </tr>
34 <tr>
35 <td>爱好:</td>
36 <td>
37 <%
38 for(int i = 0 ; i < hibbys.length ; i++){
39 String hibby = hibbys[i];
40 out.print(hibby);
41 if(i != hibbys.length-1){
42 out.print(",");
43 }
44 }
45 %>
46 </td>
47 </tr>
48 </table>
49 </body>
50 </html>
EL和JSTL方式:
registerjstl.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3 <html>
4 <head>
5 </head>
6 <body>
7 <h1>注册用户</h1>
8 <form action="registerjstldo.jsp" method="post">
9 <table>
10 <tr>
11 <td><label for="txtUserName">用户名:</label></td>
12 <td><input type="text" name="txtUserName" id="txtUserName"/></td>
13 </tr>
14 <tr>
15 <td><label for="txtLoginPwd">密码:</label></td>
16 <td><input type="password" name="txtLoginPwd" id="txtLoginPwd"/></td>
17 </tr>
18 <tr>
19 <td><label for="rdoMan">性别:</label></td>
20 <td><input type="radio" name="rdoSex" checked="checked" value="1" id="rdoMan"/><label for="rdoWan">男士</label>
21 <input type="radio" name="rdoSex" value="0" id="rdoWoman"/><label for="rdoWoman">女士</label>
22 </td>
23 </tr>
24 <tr>
25 <td><label for="selEdu" id="selEdu">学历:</label></td>
26 <td>
27 <select name="selEdu" id="selEdu">
28 <option value="小学">小学</option>
29 <option value="中学">中学</option>
30 <option value="大学">大学</option>
31 </select>
32 </td>
33 </tr>
34 <tr>
35 <td><label for="chkLan">爱好:</label></td>
36 <td>
37 <input type="checkbox" name="chkHibby" id="chkLan" value="篮球"/><label for="chkLan">篮球</label>
38 <input type="checkbox" name="chkHibby" id="chkZu" value="足球"/><label for="chkZu">足球</label>
39 <input type="checkbox" name="chkHibby" id="chkPai" value="排球"/><label for="chkPai">排球</label>
40 </td>
41 </tr>
42 <tr>
43 <td colspan="2" style="text-align:center">
44 <input type="submit" value="注册"/>
45 <input type="reset" value="重填"/>
46 </td>
47 </tr>
48 </table>
49 </form>
50 </body>
51 </html>
registerjstldo.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="asp"%>
3 <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
4
5 <%
6 request.setCharacterEncoding("utf-8");
7 %>
8
9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10 <html>
11 <head>
12 </head>
13
14 <body>
15 <h1>EL和JSTL</h1>
16 <table>
17 <tr>
18 <td>用户名:</td>
19 <td>${param.txtUserName}</td>
20 </tr>
21 <tr>
22 <td>密码:</td>
23 <td>${param.txtLoginPwd}</td>
24 </tr>
25 <tr>
26 <td>性别:</td>
27 <td>
28 <asp:if test=‘${param.rdoSex=="1"}‘>
29 男
30 </asp:if>
31 <asp:if test=‘${param.rdoSex=="0"}‘>
32 女
33 </asp:if>
34 </td>
35 </tr>
36 <tr>
37 <td>学历:</td>
38 <td>${param.selEdu}</td>
39 </tr>
40 <tr>
41 <td>爱好:</td>
42 <td>
43 <asp:forEach var="hibby" items="${paramValues.chkHibby}" varStatus="item">
44 ${hibby}
45 <asp:if test="${item.index != fn:length(paramValues.chkHibby) - 1 }">
46 ,
47 </asp:if>
48 </asp:forEach>
49 </td>
50 </tr>
51 </table>
52 </body>
53 </html>