jsp 标签库使用小结 JSTL EL表达式 JSP的标签分类

                                                                       jsp标签库的使用


标签: 可以理解成标记,或是变量,小学数学里的X

JSP的标签分类:

          JSP基本标签:<%!%>.......

          JSP指令标签:<%@ page import........%>、<%@ include file=""%>
          JSP动作标签:
                       包含标签
                         <jsp:include page=""/>----
          JSP指令标签:<%@ include file=""%>


.简单的使用范例

JSP----2.jsp

<%
    request.getParameter
    //jdbc......
    //request.getRequestDispatcher("ok.jsp").forward(req,re...);
    //pageContext.forward("ok.jsp");
    try
    {
    }
    catch(SQLException e)
    {

%>

// 动作标签   之   跳转标签

<jsp:forward page="addUser.jsp">
                <jsp:param name="msg" value="DB error...."/>  //此处还可以设置参数,(是可选项)
</jsp:forward>

<%
     }
%>
<jsp:forward page="ok.jsp"/>



<%
   String msg = request.getParameter("msg");
   if(msg!=null && msg.length()>0)
   {
%>
<%=msg%>
<%
    }
%>
<form ..


<%
   List list = request.getAttribute("list");

%>

// 动作标签   之   配置userBean标签

<jsp:useBean id="list" class="java.util.List" scope="page|request|session|application"/>


<%
    User user = session.getAttribute("user");
%>
<td><%=user.getUserName() %></td>


<jsp:useBean id="user" class="com.bluedot.domain.User" scope="request"/>

<%=user.getUserName() %>

// 动作标签   之   获得属性的标签

<jsp:getProperty name="user" property="userName"/>

// 动作标签   之   设置属性的标签

<jsp:setProperty name="" property="" value=""/>

<%
    String userName = request.getParameter();
    String age = request.getParameter();

    User user = new User();
%>
<jsp:setProperty name="user" property="userName" value="<%=userName%>"/>

<%

    UserManager m = new UserManagerImpl();
    m.addUserInfo(user);
%>






                                                                EL表达式:Expression Language
<%
  User user = request.getAttribute("user");
%>
<%=user.getUserName() %>


格式:${表达式语言}


${user.userName}
${user["userName"]}
${requestScope.user["userName"]}
${sessionScope|applicationScope|pageScope}

head.jsp
${sessionScope.}


<%
   Cookie[] cs = request.getCookies();
   for(Cookie c:cs)
   {
      if("colorName".equals(c.getName()))
      {
%>
<%=c.getValue() %>
<%
    break;
       }
    }
%>

<%
   String str = request.getHeader("User-Agent");
%>
<%%>

${header["User-Agent"]}


2.jsp
<%
  String str = request.getMethod();
  String str2 = request.getQueryString();
%>



                                                                      JSTL:Java Standard Tag Libary

Java的标准标签库

自定子标签。


要在jsp文件的开头出写如下语句,表示引入标签

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

<%
   int i=10;
   User user = new User();
   user.setName("zhangsan");
   user.setAge(20);

   //session.setAttribute("user",user);
%>
<c:set var="i" value="10"/>
<c:set var="user" value="${pageScope.user}" scope="session"/>



User request.setAttribute("user",user);


<td>${requestScope.user.userName}</td>
<td>

 

<c:if test="${requestScope.user.sex eq 1}">

     Male

  </c:if>


  <c:if test="${requestScope.user.sex eq 0}">
     Female
  </c:if>

</td>


// 注意 下面的c标签是一组,缺一不可
<c:choose>
  <c:when test="${requestScope.user.sex eq 1}">
     Male
  </c:when>

  <c:otherwise>
    Female
  </c:otherwise>
</c:choolse>
上一篇:大厂都在用EhCache,它到底比Redis强在哪里?


下一篇:实践了5千万的数据表和重建索引,学到了!