Common grammar and usage in JSP JSTL tag libraries

concept

JSTL: write java code jsp pages
J AVA S erver T AG L ibrary the JSP Standard Tag Library

That is, within the tag library contains a lot of tag library
JSTL is open source and free jsp tag library provided by the Apache organization
for java code on simplifying and replace jsp page
java code to simplify the preparation of the jsp replace <%%> wording and EL expression with

usage

  • 1, into javax.servlet.jsp.jstl.jar and jstl-impl.jar package
  • 2, use the page <%@ taglib %>to introduce the tag library
  • 3, using labels

The introduction of tag library:
In the upper part of the JSP page plus taglib tag
syntax:<%@taglib prefix="标签库的别名" uri="标签库的路径"%>

The introduction of version 1.1 core path
that is http://java.sun.com/jsp/jstl/core
because the 1.0 version of the core does not support EL expressions

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

Commonly used tags

c : if

Java equivalent of the if statement to
determine whether the request field set list is empty or null if the display set and traverse

<%
	List list=new ArrayList();
	list.add("我是list中的内容");
	request.setAttribute("list",list);
%>

<c:if test="${!empty requestScope.list}">
	${requestScope.list}
</c:if>

test attribute for receiving a boolean expression if it is true then displays <c:if>the content tag to false if the body is not displayed
using test values with the general property of binding EL expression

Note: c: if there is no else you want to achieve the effect else can write a <c:if>tag

<c:if test="${requestScope.number%2!=0}">
        ${number}为奇数
</c:if>
<c:if test="${requestScope.number%2==0}">
        ${number}为偶数
</c:if>

var properties in the result represents the result in the text will be stored EL expression to result in variable
and default to save it pageContext domain which
of course also be used scope attribute domains represented e.g. hosted scope = "session" is to show session to keep the domain


c : choose

== "c: when and c: otherwise
corresponds in Java switch statement determines
switch statement i.e. if ... else ... a simplified form of the statement is a judgment statement

<%
	request.setAttribute("number",1);
%>

<c:choose>
	<c:when test="${number==1}">星期一</c:when>
	<c:when test="${number==2}">星期二</c:when>
	<c:when test="${number==3}">星期三</c:when>
	<c:when test="${number==4}">星期四</c:when>
	<c:when test="${number==5}">星期五</c:when>
	<c:when test="${number==6}">星期六</c:when>
	<c:when test="${number==7}">星期日</c:when>
	
	<%--表示其他情况--%>
	<c:otherwise>数字输入有误</c:otherwise>
</c:choose>

c : forEach

Equivalent in Java for loop

Use a: traverse the contents of the container

<%
	List list=new ArrayList();
	list.add("111");
	list.add("222");
	list.add("333");
	request.setAttribute("list",list);
%>
<c:forEach items="${list}" var="num" varStatus="s">
	${num} == ${s.index} == ${s.count}<br>
</c:forEach>

The code is used to traverse the items in the expressions of the EL pageContext in each Item in the list
items in the EL expression must be filled because the object can only be obtained from the EL expression
and each traversed things var in the number stored in the variable in

You can write in the html code therefore very convenient

operation result:

111 == 0 == 1
222 == 1 == 2
333 == 2 == 3

items attributes: container objects
var properties: temporary variable elements in the container
varStatus properties: cycle state object
portion circulatory state properties of the object:
index: represents the current cycle is the first of several (i.e. the current index index) from 0
count: the number represents the current loop starting at 1

Usage of Two: Repeat the operation is completed

<c:forEach begin="1" end="10" var="i" step="2" varStatus="s">
	${i} == ${s.index} == ${s.count}<br>
</c:forEach>

The code number of steps for the step 10 is circulated from 1 to begin traversing the end of each iteration 2
var where i represents a traversal that the results will be stored in the variable i

You can write in the html code therefore very convenient

operation result:

1 == 1 == 1
3 == 3 == 2
5 == 5 == 3
7 == 7 == 4
9 == 9 == 5

begin attribute: start value (comprising)
End properties: end value (contained)
var properties: corresponds to the temporary variable I
STEP properties: i.e., how much each step decrease
varStatus properties: cycle status object
circulatory state object part properties:
index: representative of the current cycle is the first of several (i.e. the current index subscript) starts from 0
COUNT: representing the current number of cycles from a start


c : set

usage:

<c:set var="name" value="Mike" scope="session"></c:set>

Declare that an object corresponding to the name attribute value of Mike
and default to a stored pageContext domain which
of course can also be used to represent the scope herein scope hosted domain = "session" is stored in the session table domain


JSP action tags

1、jsp:include

<jsp:include page="test.jsp"></jsp:include>

Here it is dynamically included
shall not contain all the elements of a page of labels used to output its operating results but to bring output

Jsp stages of implementation:
Jsp => the Java file => class file => execution
here is included runtime

The tag <% @ include%> difference:
<jsp:include page="<%=url %>"></jsp:include>can be determined at run time which is variable
and <%@ include file="include.jsp" %>there have to write specific page can not be replaced with a variable


2、jsp:forward

<jsp:forward page="test.jsp"></jsp:forward >

Shall request forwarding
equivalent torequest.getRequestDispatcher("test.jsp").forward(request,response)

With a request to forward the content of this page will not display also does not perform


3、jsp:param

<jsp:forward page="test.jsp">
	<jsp:param value="beijing" name="address">
</jsp:forward >

Containing a page or a page when jumping to join the parameters
used request.getParameter("name属性的值")to obtain


Published 174 original articles · won praise 5 · Views 240,000 +

Guess you like

Origin blog.csdn.net/Piconjo/article/details/104919815