-- 作者:admin
-- 发布时间:2009/5/6 22:50:48
-- JSTL
使用JSTL,需要首先导入jstl.jar包和standard.jar包
下载地址为:http://jakarta.apache.org/site/downloads/downloads_taglibs-standard.cgi
1 基本使用 例子1: index.jsp文件: <%@page c%> <html> <head> <title>Select Your Portal</title> </head> <body> <h1>Select your preferred portal:</h1> <form action="showportal.jsp" method="get"><select name="portchoice"> <option>news</option> <option>weather</option> <option>entertainment</option> </select> <input type="submit" value="Select" /></form> </body> </html>
showportal.jsp文件: <%@ page c%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <c:choose> <c:when test="<%=((String)request.getParameter("portchoice")).equals("news") %>"> <head> <title>News Portal</title> </head> <body> <h1>Welcome to the News Portal!</h1> </body> </c:when> <c:when test="<%=((String)request.getParameter("portchoice")).equals("weather") %>"> <head> <title>Weather Portal</title> </head> <body> <h1>You Get the Latest Weather!</h1> </body> </c:when> <c:when test="<%=((String)request.getParameter("portchoice")).equals("entertainment") %>"> <head> <title>Entertainment Portal</title> </head> <body> <h1>Entertainment News Just for You!</h1> </body> </c:when> <c:otherwise> <head> <title>System Portal</title> </head> <body> <h1>Application logic problem detected!</h1> </body> </c:otherwise> </c:choose> </html>
也可以写为EL的形式: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <c:choose> <c:when test="${param.portchoice == \'news\'}"> <head> <title>News Portal</title> </head> <body> <h1>Welcome to the News Portal!</h1> </body> </c:when> <c:when test="${param.portchoice == \'weather\'}"> <head> <title>Weather Portal</title> </head> <body> <h1>You Get the Latest Weather!</h1> </body> </c:when> <c:when test="${param.portchoice == \'entertainment\'}"> <head> <title>Entertainment Portal</title> </head> <body> <h1>Entertainment News Just for You!</h1> </body> </c:when> <c:otherwise> <head> <title>System Portal</title> </head> <body> <h1>Application logic problem detected!</h1> </body> </c:otherwise> </c:choose> </html>
此时仔细观察JSP的Servlet源代码即可发现EL的本质: _jspx_th_c_when_0.setTest(((String)request.getParameter("portchoice")).equals("news") );
例子2: index.jsp文件: <%@page c%> <html> <head> <title>Select Your Portal</title> </head> <body> <table width="400"> <tr> <td colspan="2" align="center"><b>Portal Selector</b></td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td> <form action="showportal.jsp" method="get"> <table> <tr> <td width="200">Portal Selection</td> <td><select name="portchoice"> <option>news</option> <option>weather</option> <option>entertainment</option> </select></td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Select" /></td> </tr> </table> </form> </td> </tr> </table> </body> </html>
showportal.jsp文件: <%@ page c%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>System Portal-${param.portchoice}</title> </head> <body> <c:choose> <c:when test="${param.portchoice == \'news\'}"> <jsp:include page="news.jsp" /> </c:when> <c:when test="${param.portchoice == \'weather\'}"> <jsp:include page="weather.jsp" /> </c:when> <c:when test="${param.portchoice == \'entertainment\'}"> <jsp:include page="entertain.jsp" /> </c:when> <c:otherwise> <h1>Application logic problem detected!</h1> </c:otherwise> </c:choose> </body> </html>
entertain.jsp文件: <table width="600"> <tr> <td>Entertainment News Just for You!</td> </tr> <tr> <td><span> <jsp:useBean id="newsfeed" class="data.NewsFeed" scope="request"> <jsp:setProperty name="newsfeed" property="topic" value="entertainment" /> <jsp:getProperty name="newsfeed" property="value" /> </jsp:useBean> </span></td> </tr> </table>
weather.jsp文件: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <table width="600"> <tr> <td>You Get the Latest Weather!</td> </tr> <tr> <td><jsp:useBean id="newsfeed" class="data.NewsFeed" scope="request"> <jsp:setProperty name="newsfeed" property="topic" value="weather" /> </jsp:useBean> <table> <c:forEach items="${newsfeed.values}" var="row"> <tr> <td width="200">${row.city}</td> <td>${row.temp}</td> </tr> </c:forEach> </table> </td> </tr> </table>
news.jsp文件: <table width="600"> <tr> <td>Welcome to the News Portal!</td> </tr> <tr> <td><span> <jsp:useBean id="newsfeed" class="data.NewsFeed" scope="request"> <jsp:setProperty name="newsfeed" property="topic" value="news" /> <jsp:getProperty name="newsfeed" property="value" /> </jsp:useBean> </span></td> </tr> </table>
NewsFeed文件: package data;
import java.util.ArrayList; import java.util.HashMap;
public class NewsFeed extends Object implements java.io.Serializable {
private String topic;
private String value;
private ArrayList values;
public NewsFeed() {
}
public void setTopic(String topic) { value = ""; values = null; if (topic.equals("news")) { value = "猪流感"; } if (topic.equals("entertainment")) { value = "刘老根"; } if (topic.equals("weather")) { values = new ArrayList(); HashMap tmap = new HashMap(); tmap.put("city", "曼谷"); tmap.put("temp", "30 C"); values.add(tmap); tmap = new HashMap(); tmap.put("city", "莫斯科"); tmap.put("temp", "18 C"); values.add(tmap); tmap = new HashMap(); tmap.put("city", "上海"); tmap.put("temp", "28 C"); values.add(tmap); } }
public String getValue() { return this.value; }
public ArrayList getValues() { return this.values; }
}
[此贴子已经被作者于2010-12-12 11:00:02编辑过]
|