0

私はJavaサーブレットを持っています

package com.nh.bookapp;

import com.books.Book;

import java.util.*;
import java.io.IOException;
import java.io.PrintWriter;

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;

/**
 * Servlet implementation class BookApp
 */




@WebServlet("/BookApp")
public class BookApp extends HttpServlet {

    private static final long serialVersionUID = 1L;
    List <Book> Books = new ArrayList<Book>();

    /*List<String> BookNames = new ArrayList<String>();
    List<String> Authors = new ArrayList<String>();
    List<String> Costs = new ArrayList<String>();*/

    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookApp() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        String bookName = request.getParameter("bookname");
        String author = request.getParameter("authorname");
        String bookCost = request.getParameter("cost");
        String url = ("");
        /*BookNames.add(bookName);
        Authors.add(author);
        Costs.add(cost);*/
        Book newBook = new Book();
        if(bookName.length()!=0&&author.length()!=0&&bookCost.length()!=0)
        {
            newBook.authorName=author;
            newBook.name=bookName;
            newBook.cost = Float.parseFloat(bookCost);
            Books.add(newBook);
            request.setAttribute("Book", newBook);
            url =("/displayBook.jsp");
        }
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);

    }

}

クラス Book は次のように定義されます。

public class Book {
        public String name;
        public String authorName;
        public Float cost;
}

index.html は次のとおりです。

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Create a book entry</title>
</head>
<body>
<form name="BookForm" id="fBook" action="BookApp" method="post">
Name:
<input type="text" name="bookname" id="tbBook">
<br>
Author:
<input type="text" name="authorname" id="tbAuthor">
<br>
Cost:
<input type="text" name="cost" id="tbCost">
<br>

<input type ="submit" value="Create">
</form>

</body>
</html>

そして、displayBook.jsp は次のとおりです。

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Create a book entry</title>
</head>
<body>
<form name="BookForm" id="fBook" action="BookApp" method="post">
Name:
<input type="text" name="bookname" id="tbBook">
<br>
Author:
<input type="text" name="authorname" id="tbAuthor">
<br>
Cost:
<input type="text" name="cost" id="tbCost">
<br>

<input type ="submit" value="Create">
</form>

</body>
</html>

サーバーを実行すると、根本原因の下に次のようなエラーが発生します

javax.el.PropertyNotFoundException: Property [name] not found on type [com.books.Book]

JSP ファイルに必要なインポートがあり、変数がクラスで宣言されていますが、公開されているにも関わらずアクセスできません。

どこで何が問題なのかわかりません。使ってみました

 <c:out value="${Book.name}" />

別の回答で提案されたように、うまくいきませんでした。

4

0 に答える 0