JSTL标签出错:<c:forEach var="book" items="${requestScope.books}" varStatus="status">
今天在运行书里的JSTL标签代码的时候出错,总结一下:
问题1.The JSP specification requires that an attribute name is preceded by whitespace
解决:<c:forEach var="book" items="${requestScope.books}"varStatus="status">出错
改为:<c:forEach var="book" items="${requestScope.books}" varStatus="status">
原因:varStatus="status"前加空格
------------------------------------------------------------------
问题2.Property 'isbn' not found on type java.lang.String
解决:<c:forEach var="book" items=" ${requestScope.books}" varStatus="status">出错
改为:<c:forEach var="book" items="${requestScope.books}" varStatus="status">
原因:${requestScope.books}前去掉空格
------------------------------------------------------------------
问题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传参数
截图:
原创不易,转载请声明原文地址。