web-dev-qa-db-ja.com

jstlのアクセスセッション属性

サーブレットによって設定およびディスパッチされたjspページからセッション属性にアクセスしようとしていますが、「jsp:attributeは標準アクションまたはカスタムアクションのサブ要素でなければなりません」というエラーメッセージが表示されます。何が間違っている可能性がありますか、間違ってアクセスしていますか?以下はコードスニペットです。


サーブレット:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    HttpSession session = request.getSession(); 
    session.setAttribute("Questions", getQuestion());
    System.out.println(session.getAttribute("Questions"));
    RequestDispatcher req = request.getRequestDispatcher("DisplayQuestions.jsp");
    req.forward(request, response);
}

private QuestionBookDAO getQuestion(){
    QuestionBookDAO q = new QuestionBookDAO();
    q.setQuestion("First Question");
    q.setQuestionPaperID(100210);
    q.setSubTopic("Java");
    q.setTopic("Threads");
    return q;
}

セッション属性を正常に設定できました。しかし、jspファイルで同じものにアクセスしようとすると(以下)、ランタイムエラーが発生します

    <jsp:useBean id="Questions" type="com.cet.evaluation.QuestionBook" scope="session">
    <jsp:getProperty property="Questions" name="questionPaperID"/>
    <jsp:getProperty property="Questions" name="question"/>
    </jsp:useBean>

Bean QuestionBookには2つのプライベート変数questionPaperIDおよびquestionが含まれています。Tomcatでアプリケーションを実行すると、以下のエラーがスローされます。

type Exception report

message 

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception 

    org.Apache.jasper.JasperException: /DisplayQuestions.jsp(15,11) jsp:attribute must be the subelement of a standard or custom action
        org.Apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.Java:40)
        org.Apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.Java:407)
        org.Apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.Java:88)
        org.Apache.jasper.compiler.Parser.parseStandardAction(Parser.Java:1160)
        org.Apache.jasper.compiler.Parser.parseElements(Parser.Java:1461)
        org.Apache.jasper.compiler.Parser.parseBody(Parser.Java:1670)
        org.Apache.jasper.compiler.Parser.parseOptionalBody(Parser.Java:1020)
            ....
16
Vijay Selvaraj

<jsp:...>タグの使用は絶対に避けてください。それらは過去の遺物であり、現在は常に避けるべきです。

JSTLを使用します。

これで、JSTLまたは他のタグライブラリを使用する場合、Beanにアクセスするにはpropertyにはこのプロパティが必要です。プロパティはプライベートインスタンス変数ではありません。これは、パブリックゲッター(およびプロパティが書き込み可能な場合はセッター)を介してアクセス可能な情報です。 questionPaperIDプロパティにアクセスするには、こうする必要があります

public SomeType getQuestionPaperID() {
    //...
}

beanのメソッド。

それができたら、次のコードを使用してこのプロパティの値を表示できます。

<c:out value="${Questions.questionPaperID}" />

または、セッションスコープアトリビュートを具体的にターゲットにするには(スコープ間で競合が発生した場合):

<c:out value="${sessionScope.Questions.questionPaperID}" />

最後に、スコープ属性にJava variables:小文字で始まる名前を付けることをお勧めします。

35
JB Nizet

モデルを準備するコントローラーが既にある場合、モデルを設定するためにjsp:useBeanは必要ありません。

ELでプレーンにアクセスするだけです。

<p>${Questions.questionPaperID}</p>
<p>${Questions.question}</p>

または、値をHTMLエスケープしたい場合、またはテンプレートテキストでELがまだサポートされていない以前のServlet 2.3コンテナ以上で作業している場合は、JSTL <c:out>タグによって:

<p><c:out value="${Questions.questionPaperID}" /></p>
<p><c:out value="${Questions.question}" /></p>

こちらもご覧ください:


問題とは無関係に、通常の方法は、通常の変数名で行うように、小文字で属性名を開始する方法です。

session.setAttribute("questions", questions);

${questions}を使用するようにELを適宜変更します。

また、コードに [〜#〜] jstl [〜#〜] タグがないことに注意してください。すべて単純なJSPです。

15
BalusC