Java 之 Web前端(四)

1.EL表达式

  a.语法:

<%pageContext.setAttribute("page","page") %>

${page}

  b.适用:pageContext > request > session > application (优先级)

  c.隐式对象:pageScope、requestScope、sessionScope、applicationScope、pageContext、param、paramValues

  d.对象属性的获取:

${user.userName}    //方法一

${user['userName']}    //方法二

 

 

2.自定义标签

  a.创建一个类

public class MyTag extends TagSupport{

    //重写doEndTag方法
    public int doEndTag() throws JspException{
        JspWriter out = pageContext.getOut();
        out.println("hello tag");
        return SKIP_BODY;
    }
}

 

  b.建一个tld文件(WEB-INF)

<tag>
    <name>hello</name>
    <tag-class>com.tag.MyTag</tag-class>
</tag>

 

  c.使用

<%@ taglib uri="lovo.myTag" prefix="m" %>

<m:hello></m:hello>

 

 

3.标准标签库

  a.引入:

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

 

  b.set(声明变量)

<c:set var="name" value="zhangsan"></c:set>

 

  c.out(输出)

<c:out value="1"></c:out>

 

  d.if

<c:if test="true">
    <!-- 执行语句 -->
</c:if>

 

  e.choose(类似else if)

<c:choose>
    <c:when test="${score>=80}">良好</c:when>
    <c:when test="${score>=60}">及格</c:when>
    <c:otherwise}">不及格</c:otherwise>
</c:choose>

 

  f.forEach

    ①普通for循环

<c:forEach var="i" begin="0" end="10"  step="1" >
    ${i}
</c:forEach>

 

    ②for-each

<c:forEach var="user" items="${users}" >
    ${user.userName}
</c:forEach>

 

 

4.格式化标签库

  a.引入

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

 

  b.国际化资源标签

    ①建一个file,并命名为messages.properties,书写:

        userName = user name

       建一个file,并命名为messages_zh_CN.properties,书写:

        userName = 用户名

    ②使用

<fmt:bundle basename="msessge">
    <fmt:message key="userName"/>
</fmt:bundle>

 

  c.格式化数字、日期

    ①日期:

<c:set var="now" value="<%=new java.util.Date()%>">

<fmt:formatDate value="${now}" type="time">
<fmt:formatDate value="${now}" type="date">
<fmt:formatDate value="${now}" type="both">
<fmt:formatDate value="${now}" pattern="yyyy-MM-dd">

 

    ①数字:

<fmt:setlocale value="zh_cn"/>
<fmt:formatNumber value="13456.78"/>
<fmt:formatNumber value="0.3" type="number"/>
<fmt:formatNumber value="0.3" type="currenvy"/>
<fmt:formatNumber value="0.3" type="percent"/>
<fmt:formatNumber value="0.3" type=".00"/>
<fmt:formatNumber value="0.3" type="###.##E0"/>

 

 

 

5.数据库标签库(不安全,不建议使用)

  a.引入

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

 

  b.使用

<sql:setDateSource driver="com.jdbc.mysql.Driver" url="jdbc:mysql://127.0.0.1:3306/test134" user="root" password="root"></sql:setDateSource>

<sql:query var="rs">select * from User<sql:query>

 

 

6.函数库(类似java中String的方法)

  a.引入

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

 

  b.使用

<c:set var="context" value="zxczvsqwqrqr">

<c:out value="${fn:substring(context,6,-1)}">

 

posted @ 2017-03-05 19:30  晨M风  阅读(266)  评论(0编辑  收藏  举报