JSTL標籤出錯:

今天在運行書裏的JSTL標籤代碼的時候出錯,總結一下:html

問題1.The JSP specification requires that an attribute name is preceded by whitespace java

解決:<c:forEach var="book" items="${requestScope.books}"varStatus="status">出錯web

改成:<c:forEach var="book" items="${requestScope.books}" varStatus="status">編程

緣由:varStatus="status"前加空格app

------------------------------------------------------------------ jsp

問題2.Property 'isbn' not found on type java.lang.String ide

解決:<c:forEach var="book" items=" ${requestScope.books}" varStatus="status">出錯ui

改成:<c:forEach var="book" items="${requestScope.books}" varStatus="status">this

緣由:${requestScope.books}前去掉空格url

------------------------------------------------------------------ 

問題3.程序能運行了,jsp能訪問,可是EL表達式讀取不到想要的信息   

解決:沒有在.xml配置文件添加servlet的<url-pattern>信息,沒法調用servlet向jsp傳對象。

在.xml文件中添加:

<servlet>
   <servlet-name>BooksServlet</servlet-name>
   <servlet-class>com.controller.BooksServlet</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>BooksServlet</servlet-name>
   <url-pattern>/BooksServlet</url-pattern>
</servlet-mapping>

------------------------------------------------------------------ 

jstl標籤練習代碼:

1.BooksServlet.java

 1 package com.controller;
 2 import java.io.IOException;
 3 import javax.servlet.RequestDispatcher;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 import java.util.List;
10 import java.util.ArrayList;
11 import com.model.Book;
12 //@WebServlet("/BooksServlet")
13  public class BooksServlet extends HttpServlet {
14     @Override
15     public void doGet(HttpServletRequest request, HttpServletResponse response) 
16     throws ServletException, IOException {
17        List<Book> books = new ArrayList<Book>();
18        Book book1 = new Book("978-7-302-23059-5","Java語言程序設計",45.00);
19        Book book2 = new Book("978-7-302-21540-0","Java Web編程技術",39.00);
20        Book book3 = new Book("978-7-302-24130-0","C #入門經典",99.80);
21        books.add(book1);
22        books.add(book2);
23        books.add(book3);
24        request.setAttribute("books", books);
25        RequestDispatcher rd = request.getRequestDispatcher("/books.jsp");
26        rd.forward(request, response);
27       }
28 }

2.Book.java(javaBean類)

 1 package com.model;
 2 
 3 public class Book {
 4    private String isbn;
 5    private String title;
 6    private double price;
 7    public Book(String isbn, String title, double price) {
 8     super();
 9     this.isbn = isbn;
10     this.title = title;
11     this.price = price;
12     }
13     public String getIsbn() {
14         return isbn;
15     }
16     public void setIsbn(String isbn) {
17         this.isbn = isbn;
18     }
19     public String getTitle() {
20         return title;
21     }
22     public void setTitle(String title) {
23         this.title = title;
24     }
25     public double getPrice() {
26         return price;
27     }
28     public void setPrice(double price) {
29         this.price = price;
30     }
31         
32    
33 }

3.books.jsp

 1 package com.model;
 2 
 3 public class Book {
 4    private String isbn;
 5    private String title;
 6    private double price;
 7    public Book(String isbn, String title, double price) {
 8     super();
 9     this.isbn = isbn;
10     this.title = title;
11     this.price = price;
12     }
13     public String getIsbn() {
14         return isbn;
15     }
16     public void setIsbn(String isbn) {
17         this.isbn = isbn;
18     }
19     public String getTitle() {
20         return title;
21     }
22     public void setTitle(String title) {
23         this.title = title;
24     }
25     public double getPrice() {
26         return price;
27     }
28     public void setPrice(double price) {
29         this.price = price;
30     }
31         
32    
33 }

4.配置文件.xm

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>Ex_JSTL_tag</display-name>
 4   <servlet>
 5     <servlet-name>BooksServlet</servlet-name>
 6     <servlet-class>com.controller.BooksServlet</servlet-class>
 7   </servlet>
 8   <servlet-mapping>
 9     <servlet-name>BooksServlet</servlet-name>
10     <url-pattern>/BooksServlet</url-pattern>
11   </servlet-mapping>
12   <welcome-file-list>
13     <welcome-file>index.html</welcome-file>
14     <welcome-file>index.htm</welcome-file>
15     <welcome-file>books.jsp</welcome-file>
16     <welcome-file>default.htm</welcome-file>
17     <welcome-file>default.jsp</welcome-file>
18   </welcome-file-list>
19 </web-app>

5.運行時注意加入   http://localhost/Ex_JSTL_tag/BooksServlet

只有在url中加入/BooksServlet才能調用到servlet想jsp傳參數

截圖:

 

 

原創不易,轉載請聲明原文地址。

相關文章
相關標籤/搜索