JSTL(Java Server Page Standalone Tag Library)
1.servlet的xml配置:在action引用中一定要加上根目录(/project/servlet/Hello)
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>mypack.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/servlet/Hello</url-pattern>
</servlet-mapping>
2.JSP2.0新特性:EL(Expression Language)语言:只能在标签开始和标签结束之间
Eg:name:${param.name } ${paramValues.habit[0]} ${paramValues.habit[1]}
<%request.setAttribute("title","book"); %> ${requestScope.title }
3.JSTL(Java Server Page Standalone Tag Library):本身就是一种强标记,比JSP动作更强
a.一般用途和条件标签:
1.<c:out value="hello" default="ol" escapeXml="false"> body:can not see</c:out>
2.<c:set scope="page" value="nihao" var="name1"/> <c:out value="${name1}"/>
3.<jsp:useBean id="User" class="mypack.User"></jsp:useBean>
<c:set property="name" target="${User}" value="k" > value is default</c:set>
<c:out value="${User.name}"/> //输出k
4.<c:set var="count" value="100" scope="page"/> <c:if test="${count>78}">ok </c:if>
5.<c:remove var="count" scope="page"/>
6.<c:catch var="e"><%Integer.parseInt("m");%></c:catch> <c:out value="e.message"/>
6.<c:choose>
<c:when test="${count<=0}"> <font color="bule"> </c:when>
<c:otherwise> <font color="green"> </c:otherwise> </c:choose>
the value count is:<c:out value="${count}"/>
b.迭代循环标签:<jsp:useBean id="test" class="mypack.User"></jsp:useBean>
<% Collection users_c=new ArrayList();
for(int i=0;i<3;i++) { test.setName("foo"+i); users_c.add(test); }
session.setAttribute("attr",users_c); %>//只有在一定范围内才能找到
<c:forEach var="each" items="${attr}"> <c:out value="${each.name}"/></c:forEach>
<c:forEach var="s" begin="50" end="60" step="2"><c:out value="${s}"/></c:forEach>
c.URL标签:<c:import url="test1.jsp" charEncoding="gb2312"></c:import>
<c:url value="test2.jsp" var="nx" scope="request">//url重定义
<c:param name="user" value="liming"></c:param> </c:url> <c:redirect url="${nx}"/>
d:SQL标签:<sql:setDataSource var="db1" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb" user="root" password="root" scope="session" />
<sql:query var="query" dataSource="${db1}" sql="SELECT * FROM mytable"/>
<table border="1"> <c:forEach var="row" items="${query.rows}">//要迭代的集合
<tr> <td>name:<c:out value="${row.name}"/></tr></c:forEach> </table>
<sql:update dataSource="${db1}" sql="update mytable set age='22' where id=1" var="update1"/>
//要导入<%@ taglib prefix="sql|c "uri="http://*/sql|core"%>