EL函数库
来源:互联网 发布:java安卓开发教程 编辑:程序博客网 时间:2024/06/05 09:12
1,JSTL中的常用EL函数
<%@tagliburi="http://java.sun.com/jsp/jstl/functions"prefix="fn"%>
2,fn:toLowerCase
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>
- EL函数库
- EL函数库
- EL函数库
- EL函数库
- EL函数库
- EL函数库
- Java EL函数库
- Java web----EL函数库
- EL和自定义函数库
- 自定义EL函数库
- EL表达式(函数库)
- SUN:EL函数库
- el自定义函数和el函数库介绍
- javaWeb--EL函数库、标签库
- JavaWeb总结三十、EL函数库
- java web学习总结30:EL函数库
- 菜鸟学Android(四十五):EL表达式和EL函数库
- 深入分析JavaWeb Item20 -- EL表达式和EL函数库
- flash批量打印方案
- 博客开启
- iOS UIAppearance使用详解
- 阿里双十一数据库技术
- GPIO的工作原理和配置方法
- EL函数库
- RETENTION POLICY
- GIT配置文件的更改
- Java中的native关键字详解
- openFileOutput()和openFileInput
- java 流写文件
- ubntu 安装配置mysql
- React JS快速入门教程
- ActionContext和ServletActionContext小结