码迷,mamicode.com
首页 > Web开发 > 详细

servlet和jsp学习指南(五)EL表达式

时间:2016-10-21 13:11:58      阅读:279      评论:0      收藏:0      [点我收藏+]

标签: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表达式

标签:import   and   html   htm   throw   att   image   protected   tutorial   

原文地址:http://www.cnblogs.com/vincentren/p/5984080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!