jsp - 访问 jSTL 上的 Session 属性

标签 jsp jstl

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

小服务程序:

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;
}

我能够成功设置 session 属性。但是,当我尝试在我的 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 包含两个私有(private)变量 questionPaperID 问题
我在 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。

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

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

bean 中的方法。

一旦你有了它,你可以使用这个代码显示这个属性的值:
<c:out value="${Questions.questionPaperID}" />

或者,专门针对 session 范围的属性(如果范围之间发生冲突):
<c:out value="${sessionScope.Questions.questionPaperID}" />

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

关于jsp - 访问 jSTL 上的 Session 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4889431/

相关文章:

JavaScript 未执行

java - JSP 标签问题 - Java 代码作为值

javascript - 如何设置<c :forEach begin ="1" end ="?"> tag based on Selected DropDown的结束值

java - 如何使用 JSTL 交替表格行背景颜色?

java - Displaytag 库 - 检测分页请求

gwt - 如何在 GWT 项目中使用 JSTL?

java - 尝试从 servlet 调用 JDBC 时抛出 NullPointerException

java - .setProperty 100% 的时间都不起作用

mysql - Jsp 与 MySql 显示未找到列

java - 在构建 JSTL 经典 Select 标签后,我应该将 null 设置为大项吗