A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

『黑马程序员合肥』EL表达式与JSTL标签的结合用法

EL与JSTL作为JAVA WEB开发者来说,熟练的运用它们,在View模式下效率更高.因为它们的存在,基本上在视图页是不会出现 JAVA 代码的!它们的强大之处将在下面一一列举!至于概念性的就不多说了,就以例子来介绍!
target.jsp:
[AppleScript] 纯文本查看 复制代码
<%@ page language="java" contentType="text/html; charset=GB2312" import="cn.itcast.el.*,
java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'target.jsp.jsp' starting page</title>
  </head>
  
  <body>
   <%--
    Object obj = session.getAttribute("person");
    if(obj != null){
     Person p = (Person)obj;
   %>
    name: <%= p.getName() %><br>
    email: <%= p.getEmail() %><br>
    password: <%= p.getPassword() %>
    <%
     }
    --%>
    
    1. <br>
    name: ${sessionScope.person.name } -- ${sessionScope["person"].name }<br>
    email: ${sessionScope.person.email }<br>
    password: ${sessionScope.person.password }<br><br>
    
    2.
    <%
     //pageContext.setAttribute("username", "Bob");
      
     application.setAttribute("username", "Bob");
    %>
    <br>依次从 page request session application 中寻找指定的属性值<br>
    username: ${username } -- (原理: <%= pageContext.findAttribute("username") %>)
    <br><br>
    
    3.<br>
    <%
     String age = request.getParameter("age");
     out.println("age + 12(error): " + (age + 12) + ", (right): " + (Integer.parseInt(age) +
12));
    %>
    <br>
    age + 12: ${param.age + 12}
    <br><br>
    
    4. <br>
    <%
     Person p = new Person();
     p.setEmail("email123");
     p.setName("Tom123");
     p.setPassword("123456123");
     request.setAttribute("person123", p);
    %>
    requestScope.person.email: ((强转成 javaBean 类型)request.getAttribute("person123")).getEmail
();<br>
    email: ${requestScope.person123.email }
    <br><br>
    
    5. <br>
    <%
     int i = 0;
    %>
    <%= i %>
    name: ${param.name} --- <%= request.getParameter("name") %><br>
    age: ${param.age} --- <%= request.getParameter("age") %><br>
    interesing: ${paramValues.interesing[0] }    ${paramValues.interesing[1] }
                ---
                <%= request.getParameterValues("interesing")[0] %>  
                <%= request.getParameterValues("interesing")[1] %>
    <br><br>
                
    6. <br>
    initParam: ${initParam.initParam } --- <%= application.getInitParameter("initParam") %>
    <br><br>
    
    7. <br>
    method: ${pageContext.request.method } --- <%= ((HttpServletRequest)pageContext.getRequest
()).getMethod() %><br>
    localAddr: ${pageContext.request.localAddr }<br> 
    remoteHost: ${pageContext.request.remoteHost }<br>
    contentType: ${pageContext.response.contentType }<br>
    <br><br>
    
    8. <br>
    <%
     request.setAttribute("number", 100);
    %>
    number + 12: ${requestScope.number + 12 } (不是:  ${requestScope.number} + 12)
    <br><br>
    
    9. <br>
    number > 12: ${requestScope.number > 12 }
    <br><br>
    
    10. <br>
    <%
     List list = new ArrayList();
     request.setAttribute("list", list);
     request.setAttribute("flag", null);
    %>
    empty list: ${empty requestScope.list }<br>
    empty flag: ${empty requestScope.flag }<br>
    <%
     request.setAttribute("flag", "flagValue");
     list.add("^^");
    %>
    empty flag: ${empty requestScope.flag }<br>
    empty list: ${empty requestScope.list }<br>
         和 10 比         :  ${requestScope.number > 10 ? " > 10" : " < 10" }
  </body>
</html>
[AppleScript] 纯文本查看 复制代码
<%@ page language="java" import="java.util.*, cn.itcast.el.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'test.jsp' starting page</title>
  </head>
  
  <body>
    <%
     Person p = new Person();
     p.setEmail("email");
     p.setName("Tom");
     p.setPassword("123456");
      
     session.setAttribute("person", p);
      
     session.setAttribute("username", "Jerry");
    %>
    
    <a href="target.jsp?age=12">TARGET PAGE</a>
    
    <br>
    
    <form action="target.jsp">
     name: <input type="text" name="name" />
     age: <input type="text" name="age" /><br>
     interestings:
     <input type="checkbox" name="interesing" value="sports" />Sports
     <input type="checkbox" name="interesing" value="reading" />Reading
     <input type="checkbox" name="interesing" value="tv" />Tv
     <input type="checkbox" name="interesing" value="party" />Party
     <input type="submit" value="Submit" />
    </form>
  </body>
</html>
  通过上面的例子可以看出,需要JSP用几行代码写完的,EL表达式只要一到两行就写完呢!这个就是EL的强大之处.EL 的固定语法格式是${  语句  };EL里面也有隐含对象,这个和JSP 和差不多.如果我们将EL和JSTL千毫使用的效果更佳!现在我们就来看看JSTL是如何使用的!
[AppleScript] 纯文本查看 复制代码
<%@ page language="java" import="java.util.*, cn.itcast.jstl.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"  %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'testJstl.jsp' starting page</title>
  </head>
  
  <body>
   <%--
   -----------c:redirect-----------<br>
    <c:redirect url="haha.jsp"></c:redirect>
    <br><br>
    --%>
    -----------jstl functions-----------<br>
    ${fn: toLowerCase("HAHAHAHA...") }
    <br><br>
    
    -----------c:out-----------<br>
    <c:out value="${param.name}" default="none" /> -- ${param.name}<br>
    
    <%
     String str = "<<Java>>";
     request.setAttribute("book", str);
    %>
    <c:out value="${book}" /> -- ${book }
    <br><br>
    
    -----------c:set-----------<br>
    <c:set var="book" value="Oracle" scope="session" />
    <%--
     session.setAttribute("book", "oracle");
    --%>
    
    <c:out value="${sessionScope.book}" default="none" /><br>
    
    <jsp:useBean id="person" class="cn.itcast.jstl.Person" scope="page" />
    <c:set target="${pageScope.person}" property="name" value="Jerry" />
    
    <c:out value="${pageScope.person.name}" default="none"></c:out>
    <br><br>
    
    -----------c:remove-----------<br>
    <c:out value="${pageScope.person.name}" default="none"></c:out><br>
    <c:remove var="person" scope="page" />
    <c:out value="${pageScope.person.name}" default="none"></c:out>\
    <br><br>
    
    -----------c:if-----------<br>
    <c:if test="${param.name == 'Tom'}">
     This is TOM!
    </c:if>
    <br>
    
    <c:if test="${param.name == 'Tom'}" var="tomFlag" scope="request" >
     This is TOM!
    </c:if>
    
    <c:out value="${requestScope.tomFlag}" default="none" />
    <br><br>
    
    -----------c:choose-----------<br>
    <c:choose>
     <c:when test="${param.age >= 30}">
      青年
     </c:when>
     <c:when test="${param.age >= 24}">
      少年
     </c:when>
     <c:when test="${param.age >= 8}">
      童年
     </c:when>
     <c:otherwise>
      幼年
     </c:otherwise>
    </c:choose>
    <br><br>
    
    -----------c:forEach-----------<br>
    <c:forEach var="i" begin="1" end="20" step="2">
     ${i }  
    </c:forEach><br>
    
    <%
     List persons = new ArrayList();
     Set set = new HashSet();
     Map map = new HashMap();
      
     for(int i = 0; i < 10; i++){
      Person p = new Person();
      p.setId("100" + i);
      p.setAge(20 + i);
       
      p.setEmail("_" + i + "_email@sina.com");
      p.setName("_" + i + "_name");
       
      persons.add(p);
      set.add(p);
      map.put(i, p);
     }
      
     request.setAttribute("persons", persons);
     pageContext.setAttribute("map", map);
    %>
          遍历 List:<br>
    <c:forEach items="${requestScope.persons}" var="person" begin="0" end="9" step="3">
     ${person.id } -- ${person.name } -- ${person.email } -- ${person.age }<br>
    </c:forEach>
    <br>
          遍历 Set:<br>
    <c:forEach items="<%= set %>" var="person" varStatus="status">
     <c:if test="${status.index % 2 == 0}">
      <font color="blue">${person.id } -- ${person.name } -- ${person.email } -- ${person.age } === ${status.count }, ${status.index }, ${status.first }, ${status.last }</font><br>
     </c:if>
     <c:if test="${status.index % 2 != 0}">
      ${person.id } -- ${person.name } -- ${person.email } -- ${person.age } === ${status.count }, ${status.index }, ${status.first }, ${status.last }<br>
     </c:if>
    </c:forEach>
         遍历 Map:<br>
    <c:forEach items="${pageScope.map}" var="person">
     key: ${person.key} -- value: ${person.value.name } -- ${person.value.email } -- ${person.value.age }<br>
    </c:forEach>
        遍历字符串(使用 , 作为分隔符进行遍历):<br>
    <c:forEach items="a,b,c;d,e;f,g,h,i,j,k" var="str">
     ${str }  
    </c:forEach>
    <br><br>
    
    -----------c:forTokens-----------<br>
    <c:forTokens items="a,b,c;d,e;f,g,h,i,j,k" delims=";" var="str">
     ${str }  
    </c:forTokens>
    <br><br>
    
    -----------c:import-----------<br>
    <c:import url="http://localhost:8080/index.jsp"></c:import>
    <br><br>
    
    -----------c:url-----------<br>
    <c:url value="loginServlet" scope="session" var="loginServletPath">
     <c:param name="name" value="Tom"></c:param>
     <c:param name="email" value="tom@tom.com"></c:param>
    </c:url>
    <c:out value="${sessionScope.loginServletPath}" default="none" ></c:out>
    <br><br>
  </body>
</html>
Person.java:
[AppleScript] 纯文本查看 复制代码
package cn.itcast.jstl;
public class Person {
 private String id;
 private String name;
 private String email;
 private int age;
  
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}
  两个知识点理解起来并不困难的!都是写好了的标签,只要我们自己拿来用在合适的地方就可以,只要是真正的熟练了,那是相当的便捷的!


8 个回复

倒序浏览
虽然看不懂,感觉很用心
回复 使用道具 举报
回复 使用道具 举报
非常棒,总结很完善,我们的老师很负责任
回复 使用道具 举报
很赞
回复 使用道具 举报
辛苦啦~
回复 使用道具 举报
回复 使用道具 举报
好,干得漂亮!
回复 使用道具 举报
好使。。。。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马