標籤:import and html htm throw att image protected tutorial
EL運算式的具體用法我暫時先不說了
這裡就簡單說說注意點
package test.com.servlet.jstl;public class Book { private String isbn; private String title; private Double price; public Book(String isbn,String title,double price) { this.isbn = isbn; this.title = title; this.price = price; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } }
package test.com.servlet.jstl;import java.io.IOException;import java.util.ArrayList;import java.util.List;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet(urlPatterns = {"/books"})public class BooksServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = -8775932947610791148L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Book> books = new ArrayList<Book>(); Book book1 = new Book("978-213213213", "Java 7:A Beginner‘s Tutorial", 45.00); Book book2 = new Book("978-446767456", "Struts 2 Design and Programming: A Tutorial", 49.95); Book book3 = new Book("978-545644452", "Dimensional Data Warehousing with MySQL: A Tutorial", 39.95); books.add(book1); books.add(book2); books.add(book3); request.setAttribute("books", books); RequestDispatcher rd = request.getRequestDispatcher("/books.jsp"); rd.forward(request, response); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!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=UTF-8"><title>Book List</title></head><body>Books in Simple Table<table> <tr> <td>ISBN</td> <td>Title</td> </tr> <c:forEach var = "book" items="${books }"> <tr> <td>${book.isbn }</td> <td>${book.title }</td> </tr> </c:forEach></table></body></html>
訪問後頁面如下
${book.isbn }這個怎麼取值的?
其實是通過Book的getIsbn()
不信咱們把getTitle()方法注釋起來再訪問下試試
結果就成這樣了
javax.el.PropertyNotFoundException: Property ‘title‘ not readable on type test.com.servlet.jstl.Book at javax.el.BeanELResolver$BeanProperty.read(BeanELResolver.java:357) at javax.el.BeanELResolver$BeanProperty.access$000(BeanELResolver.java:305) at javax.el.BeanELResolver.getValue(BeanELResolver.java:97)......
所以簡單理解,他是通過實體屬性的get方法獲得的
servlet和jsp學習指南(五)EL運算式