我正在尝试从由servlet设置和调度的jsp页面访问会话属性,但是我收到错误消息“jsp:attribute必须是标准或自定义操作的子元素”。什么可能是错的,我访问不正确?以下是代码段。 S
Servlet的:
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包含两个私有变量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具有此属性。属性不是私有实例变量。这是一个可通过公共吸烟者访问的信息(和设置者,如果该属性是可写的)。要访问questionPaperID属性,你需要有一个
public SomeType getQuestionPaperID() {
//...
}
你的bean中的方法
一旦你这样做,你可以使用这个代码来显示这个属性的值:
<c:out value="${Questions.questionPaperID}" />
或者,专门针对会话范围的属性(在范围之间发生冲突的情况下):
<c:out value="${sessionScope.Questions.questionPaperID}" />
最后,我建议您将范围属性命名为Java变量:从小写字母开始。
