c 标签

1 . c:out  -- Like <%= ... >, but for expressions. =out.println(String)
<c:out value="hello world!" default="" escapeXml=""></c:out>
attribute description
value Expression to be evaluated.
required:true
rtexprvalue:true
default Default value if the resulting value is null.
required:false
rtexprvalue:true
escapeXml  Determines whether characters <,>,&,'," in the resulting string should be converted to their corresponding character entity codes. Default value is true.(是否按纯文本显示value的值,默认true,此时不能识别HTML标签;设为false时可以识别HTML标签)
required:false
rtexprvalue:true

等价于<% out.println("hello world!");%>
用于输出内容,如何输入域对象中的内容呢?(request,session,application,pageContext)
     <%
          request.setAttribute("a""你好1"); 
          session.setAttribute("a""你好2"); 
          application.setAttribute("a""你好3"); 
          pageContext.setAttribute("a""你好4"); 
      %>
    <c:out value="${a}" default="null value." escapeXml=""></c:out>
${a} 域对象中存在属性名相同时,搜索的顺序(优先级):pageContext > request > session > application,顾输出:你好4
获取对象(User有两个属性:uname,pwd):
      <%
          User user = new User();
          user.setUname("zfq");
          user.setPwd("123");
          session.setAttribute("u", user);
       %>
    <!-- User的uanme和pwd必须有get方法 -->
    <c:out value="${u.uname}" default="null value." escapeXml="false"></c:out>
    <c:out value="${u.pwd}" default="null value." escapeXml="false"></c:out>
    ${u.uname }  ${u.pwd }    <!-- el表达式,可在里面做运算 -->
 
2.c:set --Sets the result of an expression evaluation in a 'scope'. =setAttribute(String,Object)
<c:set property="" value="中国" scope="request" target="" var="china"></c:set>
<c:set property="" value="中国1" scope="session" target="" var="china"></c:set>
<c:out value="${china }"></c:out>
输出:中国
c:set执行的内容多在控制器中就完成了,所以该标签用的时候不多
attribute description
var Name of the exported scoped variable to hold the value specified in the action. The type of the scoped variable is whatever type the value expression evaluates to.
required:false
rtexprvalue:false
value Expression to be evaluated.
required:false
rtexprvalue:true
target Target object whose property will be set. Must evaluate to a JavaBeans object with setter property property, or to a java.util.Map object.
required:false
rtexprvalue:true
property Name of the property to be set in the
target object.
required:false
rtexprvalue:true
scope
Scope for var.(request/session)
不能是application和pageContext
(指定域对象,默认)
required:false
rtexprvalue:false
 
3.c:remove  -- Removes a scoped variable (from a particular scope, if specified).=removeAttribute(String,Object)
<%request.setAttribute("attr""Hello"); %>
<c:out value="${attr }"></c:out><br>
<c:remove var="attr" scope="request"/>
<c:out value="${attr }" default="null value"></c:out>
输出:Hello
      null value
attribute description
var Name of the scoped variable to be
removed.
required:true
rtexprvalue:false
scope Scope for var.未指明则删除所有域对象的内容
required:false
rtexprvalue:false
 
4. c:catch  -- Catches any Throwable that occurs in its body and optionally exposes it.
<c:catch var="myException">
        <%  
           int i = 8/0; 
        %>
    </c:catch>
    <c:out value="${myException.message }"></c:out>
输出:/ by zero
attribute description
var Name of the exported scoped variable for the exception thrown from a nested
action. The type of the scoped variable
is the type of the exception thrown.
required:false
rtexprvalue:false
 
5. c:if  -- Simple conditional tag, which evalutes its body if the supplied condition is true and optionally exposes a Boolean scripting variable representing the evaluation of this condition
       <%
          request.setAttribute("attr""zfq");
          request.setAttribute("num""25");
       %>
       <c:if test="${attr=='zfq'}" var="cif" scope="session">
           <c:out value="ok"></c:out>
       </c:if>
       <br>
       <c:if test="${cif }">
           <c:out value="not ok"></c:out>
       </c:if>
       <br>
       <c:if test="${num > 12 }">
           <c:out value="num > 12"></c:out>
       </c:if>
输出:ok
      not ok
      num > 12
attribute description
test The test condition that determines whether or not the body content
should be processed.
required:true
rtexprvalue:true
type:boolean
var Name of the exported scoped variable for the resulting value of the test condition. The type of the scoped variable is Boolean.
(把判断结果(bool值)放入域对象)
required:false
rtexprvalue:false
scope Scope for var.
required:false
rtexprvalue:false
 
6. c:choose -- Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>
 
attribute description
null null
 
7. c:when -- Subtag of <choose> that includes its body if its condition evalutes to 'true'
 
attribute description
test The test condition that determines
whether or not the body content should be processed.
required:true
rtexprvalue:true
type:boolean
 
8. c:otherwise -- Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'
 
attribute description
null null
 
6、7、8例子:
     <%
        Rat rat = new Rat();
        rat.setName("Jerry");
        rat.setAge(21);
        request.setAttribute("rat", rat);
     %>
      <c:choose>
     <c:when test="${rat.age <= 1 }"> <font color="red">老鼠太小不能吃!</font> </c:when>
     <c:when test="${rat.age > 1 and rat.age <= 4}"> <font color="green">老鼠正在适吃年龄!</font> </c:when>
     <c:when test="${rat.age > 4 and rat.age <= 20 }"> <font color="red">老鼠太老不好吃!</font> </c:when>
     <c:otherwise><font color="red">老鼠已成精,敢吃吗?</font></c:otherwise>
    </c:choose>
输出:老鼠已成精,敢吃吗?
 
9. c:foreach -- The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality
    <%
        ArrayList<Rat> rats = new ArrayList<Rat>();
        for(int i = 0;i < 5;i++){
            Rat rat = new Rat();
            rat.setAge(i+1);
            rat.setName("rat"+(i+1));
            rats.add(rat);
        }
        request.setAttribute("attr_rats", rats);
     %>
    <c:forEach begin="0" end="4" items="${attr_rats}" step="1" var="rat" varStatus="">
        <c:out value="${rat.name }    ${rat.age }"></c:out><br>
    </c:forEach>
输出:rat1 1
      rat2 2
      rat3 3
      rat4 4
      rat5 5

attribute description
items Collection of items to iterate over.
required:false
rtexprvalue:true
type:java.lang.Object
deferred-value:java.lang.Object
begin If items specified:
Iteration begins at the item located at
the specified index. First item of the
collection has index 0.
If items not specified:
Iteration begins with index set at the
value specified.
required:false
rtexprvalue:true
type:int
end If items specified:
Iteration ends at the item located at the
specified index (inclusive).
If items not specified:
Iteration ends when index reaches the value
specified.
required:false
rtexprvalue:true
type:int
step Iteration will only process every step items
of the collection, starting with the first one.
required:false
rtexprvalue:true
type:int
var Name of the exported scoped variable for the
current item of the iteration. This scoped
variable has nested visibility. Its type
depends on the object of the underlying
collection.
required:false
rtexprvalue:false
varStatus Name of the exported scoped variable for the
status of the iteration. Object exported is of type
javax.servlet.jsp.jstl.core.LoopTagStatus. This scoped
variable has nested visibility.
required:false
rtexprvalue:false
 
10. c:forTokens -- Iterates over tokens, separated by the supplied delimeters(分界符)
       <%
          String s = "a,b,c";
          request.setAttribute("str", s);
       %>
      
    <c:forTokens items="${str }" delims="," var="c">
        ${c}
    </c:forTokens>
输出:a b c
attribute description
items String of tokens to iterate over.
required:true
rtexprvalue:true
type:java.lang.String
deferred-value:java.lang.String
delims The set of delimiters (the characters that
separate the tokens in the string).
required:true
rtexprvalue:true
type:java.lang.String
begin Iteration begins at the token located at the
specified index. First token has index 0.
required:false
rtexprvalue:true
type:int
end Iteration ends at the token located at the
specified index (inclusive).
required:false
rtexprvalue:true
type:int
step Iteration will only process every step tokens
of the string, starting with the first one.
required:false
rtexprvalue:true
type:int
var Name of the exported scoped variable for the
current item of the iteration. This scoped
variable has nested visibility.
required:false
rtexprvalue:false
varStatus Name of the exported scoped variable for the
status of the iteration. Object exported is
of type javax.servlet.jsp.jstl.core.LoopTag
Status. This scoped variable has nested
visibility.
required:false
rtexprvalue:false
 
 
 
 
 
11. c:redirect -- Redirects to a new URL.
<c:redirect context="" url="http://www.baidu.com"></c:redirect>
跳转到百度首页
attribute description
url The URL of the resource to redirect to.
required:false
rtexprvalue:true
context Name of the context when redirecting to a relative URL
resource that belongs to a foreign context.
required:false
rtexprvalue:true
 
12. c:import -- Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'.
        <c:import url="../a.jsp">
            <c:param name="uname" value="zfq"></c:param>
        </c:import>
      JSP页面中访问参数:       
        <body>
            A jsp page.<br>
            ${param.uname}
        </body>
attribute description
url The URL of the resource to import.
required:true
rtexprvalue:true
var Name of the exported scoped variable for the
resource's content. The type of the scoped
variable is String.
required:false
rtexprvalue:false
scope Scope for var.
required:false
rtexprvalue:false
varReader Name of the exported scoped variable for the
resource's content. The type of the scoped
variable is Reader.
required:false
rtexprvalue:false
context Name of the context when accessing a relative
URL resource that belongs to a foreign
context.
required:false
rtexprvalue:true
charEncoding Character encoding of the content at the input
resource.
required:false
rtexprvalue:true
 
13. c:url -- Creates a URL with optional query parameters.
 
attribute description
var Name of the exported scoped variable for the
processed url. The type of the scoped variable is
String.
required:false
rtexprvalue:false
scope Scope for var.
required:false
rtexprvalue:false
value URL to be processed.
required:false
rtexprvalue:true
context Name of the context when specifying a relative URL
resource that belongs to a foreign context.
required:false
rtexprvalue:true
 
14. c:param -- Adds a parameter to a containing 'import' tag's URL.
 
attribute description
name Name of the query string parameter.
required:true
rtexprvalue:true
value Value of the parameter.
required:false
rtexprvalue:true
 
注解:
(1)rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否能够利用JSP表达式




原文地址:https://www.cnblogs.com/shadowwalker/p/3601342.html