今天在运行书里的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
package com.controller;
import java.io.IOException;
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;
import java.util.List;
import java.util.ArrayList;
import com.model.Book;
//@WebServlet("/BooksServlet")
public class BooksServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Book> books = new ArrayList<Book>();
Book book1 = new Book("978-7-302-23059-5","Java语言程序设计",45.00);
Book book2 = new Book("978-7-302-21540-0","Java Web编程技术",39.00);
Book book3 = new Book("978-7-302-24130-0","C #入门经典",99.80);
books.add(book1);
books.add(book2);
books.add(book3);
request.setAttribute("books", books);
RequestDispatcher rd = request.getRequestDispatcher("/books.jsp");
rd.forward(request, response);
}
}
2.Book.java(javaBean类)
package com.model; public class Book {
private String isbn;
private String title;
private double price;
public Book(String isbn, String title, double price) {
super();
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;
} }
3.books.jsp
package com.model; public class Book {
private String isbn;
private String title;
private double price;
public Book(String isbn, String title, double price) {
super();
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;
} }
4.配置文件.xm
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Ex_JSTL_tag</display-name>
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>books.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
5.运行时注意加入 http://localhost/Ex_JSTL_tag/BooksServlet
只有在url中加入/BooksServlet才能调用到servlet想jsp传参数
截图:
原创不易,转载请声明原文地址。