EL函数库

来源:互联网 发布:java安卓开发教程 编辑:程序博客网 时间:2024/06/05 09:12

1,JSTL中的常用EL函数

¨由于在JSP页面中显示数据时,经常需要对显示的字符串进行处理,SUN公司针对于一些常见处理定义了一套EL函数库供开发者使用。
¨这些EL函数在JSTL开发包中进行描述,因此在JSP页面中使用SUN公司的EL函数库,需要导入JSTL开发包,并在页面中导入EL函数库,如下所示:
在页面中使用JSTL定义的EL函数:

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


2,fn:toLowerCase

¨fn:toLowerCase函数将一个字符串中包含的所有字符转换为小写形式,并返回转换后的字符串,它接收一个字符串类型的参数,例如
fn:toLowerCase("Www.IT315.org")的返回值为字符串“www.it315.org”
fn:toLowerCase("")的返回值为空字符串
3,fn:toUpperCase

¨fn:toUpperCase函数将一个字符串中包含的所有字符转换为大写形式,并返回转换后的字符串,它接收一个字符串类型的参数。例如:
fn:toUpperCase("Www.IT315.org")的返回值为字符串“WWW.IT315.ORG”
fn:toUpperCase("")的返回值为空字符串
4,fn:trim

¨fn:trim函数删除一个字符串的首尾的空格,并返回删除空格后的结果字符串,它接收一个字符串类型的参数。需要注意的是,fn:trim函数不能删除字符串中间位置的空格。
例如,fn:trim("  www.it315.org ") 的返回值为字符串“www.it315.org”
5,fn:length  

¨fn:length函数返回一个集合或数组大小,或返回一个字符串中包含的字符的个数,返回值为int类型。fn:length函数接收一个参数,这个参数可以是<c:forEach>标签的items属性支持的任何类型,包括任意类型的数组、java.util.Collection、java.util.Iterator、java.util.Enumeration、java.util.Map等类的实例对象和字符串。
¨如果fn:length函数的参数为null或者是元素个数为0的集合或数组对象,则函数返回0;如果参数是空字符串,则函数返回0。
6,fn:split

¨fn:split函数以指定字符串作为分隔符,将一个字符串分割成字符串数组并返回这个字符串数组。
¨
¨fn:split函数接收两个字符串类型的参数,第一个参数表示要分割的字符串,第二个参数表示作为分隔符的字符串。
¨
¨例如,fn:split("www.it315.org",".")[1]的返回值为字符串“it315”。
7,fn:join

¨fn:join函数以一个字符串作为分隔符,将一个字符串数组中的所有元素合并为一个字符串并返回合并后的结果字符串。fn:join函数接收两个参数,第一个参数是要操作的字符串数组,第二个参数是作为分隔符的字符串。
¨如果fn:join函数的第二个参数是空字符串,则fn:join函数的返回值直接将元素连接起来。例如:
假设stringArray是保存在Web域中的一个属性,它表示一个值为{"www","it315","org"}的字符串数组,则fn:join(stringArray,“.")返回字符串“www.it315.org”
fn:join(fn:split("www,it315,org",","), ".") 的返回值为字符串“www.it315.org”
8,fn:indexOf  

¨fn:indexOf函数返回指定字符串在一个字符串中第一次出现的索引值,返回值为int类型。fn:indexOf函数接收两个字符串类型的参数,如果第一个参数字符串中包含第二个参数字符串,那么,不管第二个参数字符串在第一个参数字符串中出现几次,fn:indexOf函数总是返回第一次出现的索引值;如果第一个参数中不包含第二个参数,则fn:indexOf函数返回-1。如果第二个参数为空字符串,则fn:indexOf函数总是返回0。例如:
¨fn:indexOf("www.it315.org","t3")的返回值为5
9,fn:contains

¨fn:contains函数检测一个字符串中是否包含指定的字符串,返回值为布尔类型。fn:contains函数在比较两个字符串是否相等时是大小写敏感的。
¨fn:contains函数接收两个字符串类型的参数,如果第一个参数字符串中包含第二个参数字符串,则fn:contains函数返回true,否则返回false。如果第二个参数的值为空字符串,则fn:contains函数总是返回true。实际上,fn:contains(string,substring)等价于fn:indexOf(string, substring) != -1。
¨
¨忽略大小的EL函数:fn:containsIgnoreCase
10,fn:startsWith

¨fn:startsWith函数用于检测一个字符串是否是以指定字符串开始的,返回值为布尔类型。
¨
¨fn:startsWith函数接收两个字符串类型的参数,如果第一个参数字符串以第二个参数字符串开始,则函数返回true,否则函数返回false。如果第二个参数为空字符串,则fn:startsWith函数总是返回true。例如:
¨fn:startsWith("www.it315.org","it315")的返回值为false
¨与之对应的EL函数:fn:endsWith
11,fn:replace

¨fn:replace函数将一个字符串中包含的指定子字符串替换为其它的指定字符串,并返回替换后的结果字符串。fn:replace方法接收三个字符串类型的参数,第一个参数表示要操作的源字符串,第二个参数表示源字符串中要被替换的子字符串,第三个参数表示要被替换成的字符串。例如:
¨
¨fn:replace("www it315 org"," ", ".")的返回值为字符串“www.it315.org”
12,fn:substring

¨fn:substring函数用于截取一个字符串的子字符串并返回截取到的子字符串。fn:substring函数接收三个参数,第一个参数是用于指定要操作的源字符串,第二个参数是用于指定截取子字符串开始的索引值,第三个参数是用于指定截取子字符串结束的索引值,第二个参数和第三个参数都是int类型,其值都从0开始。例如:
fn:substring("www.it315.org",4, 9) 的返回值为字符串“it315”
13,fn:substringAfter

¨fn:substringAfter函数用于截取并返回一个字符串中的指定子字符串第一次出现之后的子字符串。fn:substringAfter函数接收两个字符串类型的参数,第一个参数表示要操作的源字符串,第二个参数表示指定的子字符串,例如:
¨
¨fn:substringAfter(“www.it315.org”,“.”)的返回值为字符串“it315.org”。
¨
¨
¨与之对应的EL函数为:fn:substringBefore
14,案例

Book为文件名

1,action包里的BookServlet

package action;

import java.io.IOException;
import java.util.List;

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

import dao.BookDao;
import model.Book;

public class BookServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        //中文转换
                req.setCharacterEncoding("UTF-8");
        //1从页面接收参数
//        5
        int type=1;
        if(req.getParameter("type")!=null)
        {
        type=Integer.parseInt(req.getParameter("type"));
        }
        //        2填充一个book
        Book book=new Book();
        if(type==2){
            
//        6
            if(req.getParameter("name")!=null){
                book.setId(Integer.parseInt(req.getParameter("name")));
            }
            
        }
//        7
        else if(type==3){
            book.setName(req.getParameter("name"));
        }
//        8
        else if(type==4){
            book.setPublish(req.getParameter("name"));
        }
//        9
        else if(type==5){
            book.setAuthor(req.getParameter("name"));
        }
//        3实例化dao,调用查询方法
        BookDao dao=new BookDao();
        List<Book> books=dao.findBookByBook(type, book);
//        4传递list到下一个页面,单个页面用setAttribute
        req.setAttribute("books", books);
        
        req.getRequestDispatcher("result.jsp").forward(req, resp);
    
    }
}
2,BookDao
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.DBcon;
import model.Book;

public class BookDao {
//    1type查询类型 1-5分别表示全部查询、按编号查询、按图书名称、按出版社和按作者
//    name查询值
    public List<Book> findBookByBook(int type,Book book){
        
        List<Book> books=new ArrayList<Book>();
//        2声明sql
        String sql="select * from book";
//        3构建statement
        Connection con=DBcon.getCon();
        try {
//            4
            PreparedStatement pt=null;
//            5
            if(type==1){
                 pt=con.prepareStatement(sql);
            }else if(type==2){
//                6
                sql=sql+" where id like ?";
                 pt=con.prepareStatement(sql);
//                7 填充占位符
                 pt.setInt(1, book.getId());
            }
            else if(type==3){
//                6
                sql=sql+" where name like ?";
                 pt=con.prepareStatement(sql);
//                7 填充占位符
                 pt.setString(1, book.getName()+"%");
            }
            else if(type==4){
//                6
                sql=sql+" where publish like ?";
                 pt=con.prepareStatement(sql);
//                7 填充占位符
                 pt.setString(1, book.getPublish()+"%");
            }
            else if(type==5){
//                6
                sql=sql+" where author like ?";
                 pt=con.prepareStatement(sql);
//                7 填充占位符
                 pt.setString(1, book.getAuthor()+"%");
            }
//            8.生成结果集
            ResultSet rs=pt.executeQuery();
            
//            填充对象
            while(rs.next()){
                Book b=new Book();
                b.setId(rs.getInt(1));
                b.setName(rs.getString(2));
                b.setPublish(rs.getString(3));
                b.setPrice(rs.getFloat(4));
                b.setAuthor(rs.getString(5));
                books.add(b);
                
            }
            rs.close();
            pt.close();
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return books;
        
    }
    
//    测试
    public static void main(String[] args) {
        BookDao dao=new BookDao();
        Book book=new Book();
        book.setPublish("电子");
        List<Book> lists=dao.findBookByBook(4, book);
        for(Book b:lists){
            System.out.println(b.getName());
        }
    }
}

3,Book

package model;

public class Book {

    private int id;
    private String name;
    private String publish;
    private float price;
    private String author;
    
    
    
    public Book() {
        super();
        // TODO Auto-generated constructor stub
    }


    public Book(int id, String name, String publish, float price, String author) {
        super();
        this.id = id;
        this.name = name;
        this.publish = publish;
        this.price = price;
        this.author = author;
    }


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }


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


    public String getPublish() {
        return publish;
    }


    public void setPublish(String publish) {
        this.publish = publish;
    }


    public float getPrice() {
        return price;
    }


    public void setPrice(float price) {
        this.price = price;
    }


    public String getAuthor() {
        return author;
    }


    public void setAuthor(String author) {
        this.author = author;
    }


    @Override
    public String toString() {
        return "book [id=" + id + ", name=" + name + ", publish=" + publish
                + ", price=" + price + ", author=" + author + "]";
    }
    
    
    
    
}

4,package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBcon {

    //返回一个可用的Connection
    public static Connection getCon(){
        Connection con=null;
        //1,注册驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            //2,创建链接
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","root");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;
    }
    public static void main(String[] args) {
        System.out.println(DBcon.getCon()+"!!!");
    }
}

5,web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Book</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
<servlet>
<servlet-name>book</servlet-name>
<servlet-class>action.BookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>book</servlet-name>
<url-pattern>/find</url-pattern>
</servlet-mapping>
</web-app>

6,result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
   
    <title>My JSP 'result.jsp' starting page</title>
    
    

  </head>
 
  <body>
       <center>
           <table border="1">
             <tr>
                 <th>编号</th>
                 <th>名称</th>
                 <th>出版社</th>
                 <th>价格</th>
                 <th>作者</th>
             </tr>
             <c:forEach items="${books}" var="book">
             <tr>
                     <td>${book.id }</td>
                     <td>${book.name}</td>
                     <td>${book.publish}</td>
                     <td>${book.price}</td>
                     <td>${book.author}</td>
             </tr>
             </c:forEach>
           </table>
       </center>
  </body>
</html>

7,<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title>My JSP 'search.jsp' starting page</title>
    
    

  </head>
 
  <body>
               
               <center>
                   
                       <form action="find" method="post">
                           <table border="1">
                               <tr>
                                   <td>查询类型</td>
                                   <td>
                                           <select name="type">
                                               <option value="1">查询全部</option>
                                               <option value="2">查询ID</option>
                                               <option value="3">查询名称</option>
                                               <option value="4">查询出版社</option>
                                               <option value="5">查询作者</option>
                                           </select>
                                   </td>
                               </tr>
                               <tr>
                                   <td>查询条件</td>
                                   <td>
                                       <input type="text" name="name"/>
                                   </td>
                               </tr>
                               <tr>
                                 <td><input type="submit" value="提交"/></td>
                                 <td><input type="reset" value="重置"/></td>
                               
                               </tr>
                           </table>
                       </form>
                   
               </center>
  </body>
</html>


0 0
原创粉丝点击