我试图从一个由servlet设置和调度的jsp页面访问会话属性,但是我收到错误消息"jsp:attribute必须是标准或自定义操作的子元素".可能有什么问题,我是否错误地访问了它?以下是代码段.
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;
}
Run Code Online (Sandbox Code Playgroud)
我能够成功设置会话属性.但是当我尝试在我的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>
Run Code Online (Sandbox Code Playgroud)
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)
....
Run Code Online (Sandbox Code Playgroud)
JB *_*zet 36
你绝对应该避免使用<jsp:...>标签.它们是过去的遗物,现在应该永远避免.
使用JSTL.
现在,您使用JSTL或任何其他标记库,访问bean 属性需要您的bean具有此属性.属性不是私有实例变量.它是通过公共getter(和setter,如果属性是可写的)可访问的信息.要访问questionPaperID属性,您需要有一个
public SomeType getQuestionPaperID() {
//...
}
Run Code Online (Sandbox Code Playgroud)
你bean中的方法.
完成后,您可以使用以下代码显示此属性的值:
<c:out value="${Questions.questionPaperID}" />
Run Code Online (Sandbox Code Playgroud)
或者,专门针对会话范围属性(如果范围之间存在冲突):
<c:out value="${sessionScope.Questions.questionPaperID}" />
Run Code Online (Sandbox Code Playgroud)
最后,我建议您将范围属性命名为Java变量:以小写字母开头.
Bal*_*usC 15
jsp:useBean如果您已经有一个准备模型的控制器,则无需设置模型.
只需通过EL访问它:
<p>${Questions.questionPaperID}</p>
<p>${Questions.question}</p>
Run Code Online (Sandbox Code Playgroud)
或者通过JSTL <c:out>标记,如果您想要HTML转义值,或者当您仍在使用旧版Servlet 2.3容器或更旧时,当模板文本中不支持EL时:
<p><c:out value="${Questions.questionPaperID}" /></p>
<p><c:out value="${Questions.question}" /></p>
Run Code Online (Sandbox Code Playgroud)
与问题无关,通常的做法是使用小写字母启动属性名称,就像使用普通变量名称一样.
session.setAttribute("questions", questions);
Run Code Online (Sandbox Code Playgroud)
并相应地改变EL使用${questions}.
另请注意,代码中没有任何JSTL标记.这都是普通的JSP.
| 归档时间: |
|
| 查看次数: |
110708 次 |
| 最近记录: |