JSTL not being read properly in JSP

2019-09-02 15:11发布

Good day!

I tried using JSTL in java but there's an error:

exception
javax.servlet.ServletException: java.lang.InstantiationException: class session.Item : java.lang.InstantiationException: session.Item

root cause
java.lang.InstantiationException: class session.Item : java.lang.InstantiationException: session.Item

My code is as follows:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <title>DISPLAY ITEM</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>

    <jsp:useBean id="invenItem" class="session.Item" scope="request"/>  
    <c:choose>
        <c:when test="${invenItem != null}">
             <h1>Inventory Item Details: JSP Tester</h1>
             Stock ID  : <jsp:getProperty name="invenItem" property="stockID" /><br/>
             Name      : <jsp:getProperty name="invenItem" property="itemName" /><br/>
             Unit Price: <jsp:getProperty name="invenItem" property="unitPrice" /><br/>
             On Stock  : <jsp:getProperty name="invenItem" property="onStock" />
            <h1> Inventory Item Details Tester: EL Method </h1>
            Stock ID  : ${invenItem.stockID} <br/>
            Name      : ${invenItem.itemName}<br/>
            Unit Price: ${invenItem.unitPrice}<br/>
            On Stock  : ${invenItem.onStock}
        </c:when>
        <c:otherwise>
            <%@ include file ="DataForm.html" %><br>
            Item not existing!<br>
            <%@ include file ="ItemEntry.html" %>
        </c:otherwise>
    </c:choose>

</body>
</html>

I've read this JLPT already and followed all the instructions there. Please help. Thank you.

EDIT...

I resolved the error as suggested, but still the JSTL (if else logic) is not working. HELP PLEASE!!!!

标签: java jstl
2条回答
beautiful°
2楼-- · 2019-09-02 15:51

it seems that you forgot to reference the jstl core taglib at the beginning:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
查看更多
叼着烟拽天下
3楼-- · 2019-09-02 16:10

If invenItem already exists as a request attribute, then you don't need to use <jsp:useBean> to retrieve it, you can just use it directly in your JSP, so just remove the <useBean> line completely.

If invenItem doesn't already exist as a request attribute, then <jsp:useBean> will create one for you, but whatever you put in the class attribute must be the fully-qualified name of a class with a public, default constructor, else you'll get the exception you saw.

查看更多
登录 后发表回答