【问题标题】:Access Session attribute on jstl访问 jstl 上的 Session 属性
【发布时间】:2011-02-03 17:26:17
【问题描述】:

我正在尝试从由 servlet 设置和调度的 jsp 页面访问会话属性,但我收到错误消息“jsp:attribute must be the subelement of a standard or custom action”。可能出了什么问题,我是否错误地访问它?以下是代码sn-p。


小服务程序:

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 包含两个私有变量 questionPaperIDquestion 我在 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)
            ....

【问题讨论】:

    标签: jsp jstl


    【解决方案1】:

    您绝对应该避免使用&lt;jsp:...&gt; 标签。它们是过去的遗物,现在应该始终避免使用。

    使用 JSTL。

    现在,无论您使用 JSTL 还是任何其他标记库,访问 bean property 都需要您的 bean 具有此属性。属性不是私有实例变量。它是可通过公共 getter(和 setter,如果属性是可写的)访问的信息。要访问 questionPaperID 属性,您需要有一个

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

    bean 中的方法。

    一旦你有了它,你就可以使用这个代码来显示这个属性的值了:

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

    或者,专门针对会话范围的属性(以防范围之间发生冲突):

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

    最后,我鼓励您将作用域属性命名为 Java 变量:以小写字母开头。

    【讨论】:

    • 同意。大写字母肯定让我愣了一秒。
    • 是否需要将会话设置为servlet中的属性?
    • @gmustudent:会话属性应该用于属于整个会话的信息。如果它们仅与当前请求相关,则应使用请求属性。
    • 所以我可以获取用户会话。从会话中的对象获取数据。在 servlet 中更新该会话对象。然后返回视图并使用该数据。通过像上面那样拨打电话?
    【解决方案2】:

    如果您已经有一个控制器来准备模型,则不需要jsp:useBean 来设置模型。

    只需通过 EL 直接访问它:

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

    如果您想对值进行 HTML 转义,或者当模板文本中尚不支持 EL 时仍在使用旧版 Servlet 2.3 容器或更早版本时,也可以使用 JSTL &lt;c:out&gt; 标签:

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

    另见:


    与问题无关,通常的做法是顺便将属性名称以小写开头,就像您使用普通变量名称一样。

    session.setAttribute("questions", questions);
    

    并相应地更改 EL 以使用 ${questions}

    另请注意,您的代码中没有任何JSTL 标记。都是纯 JSP。

    【讨论】:

    • 这应该是JSP中的EL,如果我在我的页面上限制EL,还有其他方法可以实现吗?
    • 我不明白这条评论。请修改您的术语。给定的答案应该可以正常工作并且是正确的方法。在远古时代,您也可以在 JSP 中使用 scriptlets,但十年来不鼓励这样做。
    • @Vijay:你为什么要避免使用 EL?你喜欢事情比必要的更难吗? BalusC 是对的:EL 的存在是有充分理由的。使用它。
    • 谢谢,我同意你的观点,我并不反对使用 EL,但如果我的 DD 上有 true 或我的老板不同意使用它那我该如何实现呢?我正在寻找 JSTL 和自定义标签是否有帮助。
    • 啊,是的,这边。如果 EL 被 DD 忽略,那么你已经不能使用 JSTL。你最好的方法是&lt;jsp:xxx&gt; 标签,就像你已经拥有或只是老式的scriptlets(或寻找另一个工作/项目)。顺便说一下,您遇到的异常与代码 sn-p 无关。你在错误的地方有一个&lt;jsp:attribute&gt;。确保您正在运行您认为正在运行的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2013-04-17
    • 1970-01-01
    相关资源
    最近更新 更多