尚学堂Struts学习笔记2:JSTL_格式化库及函数库

jstl标签库的配置
 * 将jstl.jar和standard.jar拷贝到WEB-INF/lib下(如果使用el表达式,不用拷贝这两个jar)
 
 注意:jstl必须在能够支持j2ee1.4/servlet2.4/jsp2.0版本上的容器才能运行,这个环境
      是目前较为常用的环境

     
标签库的使用
 * 采用taglib指令引入
 <%@ taglib prefix="c"  uri="
http://java.sun.com/jsp/jstl/core"%
 <%@ taglib prefix="fmt" uri="
http://java.sun.com/jsp/jstl/fmt"%>
 
  
自定义函数库:
 1、定义类和方法(方法必须是public static) 
 2、编写自定义tld文件,并且将此文件放到WEB-INF或WEB-INF任意子目录下
 3、在jsp中采用taglib指令引入自定义函数库
 4、采用 前缀+冒号+函数名 调用即可 

 

测试EL表达式:

jstl_el.jsp:

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>测试EL表达式</title>
</head>
<body>
 <h1>测试EL表达式</h1><br>
 <hr>
 <li>普通字符串</li><br>
 hello(jsp脚本):<%=request.getAttribute("hello") %><br>
 hello(el表达式,el表达式的使用方法$和{}):${hello }<br>
 hello(el表达式,el的隐含对象pageScope,requestScope,sessionScope,applicationScope,<br> 如果未指定scope,它的搜索顺序为pageScope~applicationScope):${requestScope.hello }<br>
 hello(el表达式,scope=session):${sessionScope.hello }<br>
 <p>
 <li>结构,采用.进行导航,也称存取器</li><br>
 姓名:${user.username }<br>
 年龄:${user.age }<br>
 所属组:${user.group.name }<br>
 <p>
 <li>输出map,采用.进行导航,也称存取器</li><br>
 mapvalue.key1:${mapvalue.key1 }<br>
 mapvalue.key2:${mapvalue.key2 }<br>
 <p>
 <li>输出数组,采用[]和下标</li><br>
 strarray[2]:${strarray[1] }<br>
 <p>
 <li>输出对象数组,采用[]和下标</li><br>
 userarray[3].username:${users[2].username }<br>
 <p>
 <li>输出list,采用[]和下标</li><br>
 userlist[5].username:${userlist[4].username }<br>
 <p>
 <li>el表达式对运算符的支持</li><br>
 1+2=${1+2 }<br>
 10/5=${10/5 }<br>
 10 div 5=${10 div 5 }<br>
 10%3=${10 % 3 }<br>
 10 mod 3=${10 mod 3 }<br>
 <!--
   ==/eq
   !=/ne
   </lt
   >/gt
   <=/le
   >=/ge
   &&/and
   ||/or
   !/not
   //div
   %/mod
  --> 
  <li>测试empty</li><br>
  value1:${empty value1 }<br>
  value2:${empty value2 }<br>
  value3:${empty value3 }<br>
  value4:${empty value4 }<br>
  value4:${!empty value4 }<br>
</body>
</html>

JstlElAction.java:

package com.bjsxt.struts;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * 测试EL表达式
 * @author Administrator
 *
 */
public class JstlElAction extends Action {

 @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  //普通字符串
  request.setAttribute("hello", "hello world");
  
  //结构
  Group group = new Group();
  group.setName("尚学堂");
  
  User user = new User();
  user.setUsername("张三");
  user.setAge(18);
  user.setGroup(group);
  
  request.setAttribute("user", user);
  
  //map
  Map mapValue  = new HashMap();
  mapValue.put("key1", "value1");
  mapValue.put("key2", "value2");
  
  request.setAttribute("mapvalue", mapValue);
  
  //字符串数组
  String[] strArray = new String[]{"a", "b", "c"};
  request.setAttribute("strarray", strArray);
  
  User[] users = new User[10];
  for (int i=0; i<10; i++) {
   User u = new User();
   u.setUsername("U_" + i);
   users[i] = u;
  }
  request.setAttribute("users", users);
  
  List userList = new ArrayList();
  for (int i=0; i<10; i++) {
   User uu = new User();
   uu.setUsername("UU_" + i);
   userList.add(uu);
  }
  request.setAttribute("userlist", userList);
  
  //empty
  request.setAttribute("value1", null);
  request.setAttribute("value2", "");
  request.setAttribute("value3", new ArrayList());
  request.setAttribute("value4", "123456");
  return mapping.findForward("success");
 }


}

效果呈现:

测试jstl格式化库:

jstl_fmt.jsp:

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>测试jstl格式化库</title>
</head>
<body>
 <h1>测试jstl格式化库</h1>
 <hr>
 <li>测试日期的格式化</li><br>
 today(default):<fmt:formatDate value="${today}"/><br>
 today(type="date"):<fmt:formatDate value="${today}" type="date"/><br>
 today(type="time"):<fmt:formatDate value="${today}" type="time"/><br>
 today(type="both"):<fmt:formatDate value="${today}" type="both"/><br>
 today(dateStyle="short"):<fmt:formatDate value="${today}" dateStyle="short"/><br>
 today(dateStyle="medium"):<fmt:formatDate value="${today}" dateStyle="medium"/><br>
 today(dateStyle="long"):<fmt:formatDate value="${today}" dateStyle="long"/><br>
 today(dateStyle="full"):<fmt:formatDate value="${today}" dateStyle="full"/><br>
 today(pattern="yyyy/MM/dd HH:mm:ss"):<fmt:formatDate value="${today}" pattern="yyyy/MM/dd HH:mm:ss"/><br>
 today(pattern="yyyy/MM/dd HH:mm:ss"):<fmt:formatDate value="${today}" pattern="yyyy/MM/dd HH:mm:ss" var="d"/><br>
 ${d }<br>
 <p>
 <li>测试数字的格式化</li><br>
 n(default):<fmt:formatNumber value="${n}"/><br>
 n(pattern="###,###.##"):<fmt:formatNumber value="${n}" pattern="###,###.##"/><br>
 n(pattern="###,###.0000"):<fmt:formatNumber value="${n}" pattern="###,###.0000"/><br>
 n(groupingUsed="false"):<fmt:formatNumber value="${n}" groupingUsed="false"/><br>
 n(minIntegerDigits="10"):<fmt:formatNumber value="${n}" minIntegerDigits="10"/><br>
 n(type="currency"):<fmt:formatNumber value="${n}" type="currency"/><br>
 n(type="currency"):<fmt:formatNumber value="${n}" type="currency" currencySymbol="$"/><br>
 n(type="percent"):<fmt:formatNumber value="${p}" type="percent" maxFractionDigits="2" minFractionDigits="2"/><br>
</body>
</html>

JstlFmtAction.java:

package com.bjsxt.struts;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * 测试jstl格式化库
 * @author Administrator
 *
 */
public class JstlFmtAction extends Action {

 @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  request.setAttribute("today", new Date());
  
  request.setAttribute("n", 123456.123);
  
  request.setAttribute("p", 0.12345);
  return mapping.findForward("success");
 }
 
}

效果呈现:

测试jstl函数库:

jstl_fn.jsp:

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>  
<%@ taglib prefix="my" uri="http://www.bjsxt.com/functions"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>测试jstl函数库</title>
</head>
<body>
 <h1>测试jstl函数库</h1>
 <hr>
 hello.length=(jsp脚本):<%=((String)request.getAttribute("hello")).length() %><br>
 hello.length(jstl函数库,函数调用必须在el表达式中 前缀+冒号+函数名):${fn:length(hello) }<br>
 list.length:${fn:length(list) }<br>
 <p>
 <li>测试自定义函数库</li><br>
 ${my:sayHello(name) }<br>
</body>
</html>

JstlFnAction.java:

package com.bjsxt.struts;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * 测试jstl函数库
 * @author Administrator
 *
 */
public class JstlFnAction extends Action {

 @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  
  request.setAttribute("hello", "hello world");
  
  List list = new ArrayList();
  list.add("t1");
  list.add("t2");
  
  request.setAttribute("list", list);
  
  request.setAttribute("name", "Tom");
  return mapping.findForward("success");
 }

}

效果呈现:

用到的几个类:

User.java:

package com.bjsxt.struts;

public class User {
 
 private String username1;
 
 private int age;
 
 private Group group;

 public String getUsername() {
  return username1;
 }

 public void setUsername(String username) {
  this.username1 = username;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public Group getGroup() {
  return group;
 }

 public void setGroup(Group group) {
  this.group = group;
 }
}
Group.java:

package com.bjsxt.struts;

public class Group {
 
 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
}

MyFunctions.java:

package com.bjsxt.struts;

public class MyFunctions {
 
 /**
  * 方法必须是public static
  * @param name
  * @return
  */
 public static String sayHello(String name) {
  return "Hello " + name;
 }
}
在WEB-INF下面的:

myfunctions.tld:

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  version="2.0">
   
  <description>my functions library</description>
  <display-name>my functions</display-name>
  <tlib-version>1.0</tlib-version>
  <short-name>my</short-name>
  <uri>http://www.bjsxt.com/functions</uri>
 
  <function>
    <name>sayHello</name>
    <function-class>com.bjsxt.struts.MyFunctions</function-class>
    <function-signature>java.lang.String sayHello(java.lang.String)</function-signature>
  </function>
</taglib>
struts-config.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

 <action-mappings>
  <action path="/jstlel"
    type="com.bjsxt.struts.JstlElAction"
  >
   <forward name="success" path="/jstl_el.jsp"/>
  </action>
  
  <action path="/jstlcore"
    type="com.bjsxt.struts.JstlCoreAction"
  >
   <forward name="success" path="/jstl_core.jsp"/>
  </action>
  
  <action path="/jstlfmt"
    type="com.bjsxt.struts.JstlFmtAction"
  >
   <forward name="success" path="/jstl_fmt.jsp"/>
  </action>
  
  <action path="/jstlfn"
    type="com.bjsxt.struts.JstlFnAction"
  >
   <forward name="success" path="/jstl_fn.jsp"/>
  </action>
  
 </action-mappings>
</struts-config>

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>测试JSTL</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
    <h1>测试JSTL</h1>
    <hr>
    <a href="jstlel.do">测试EL表达式</a><br>
    <a href="jstlcore.do">测试jstl核心库</a><br>
    <a href="jstlfmt.do">测试jstl格式化库</a><br>
    <a href="jstlfn.do">测试jstl函数库</a>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值