我有一个下一个问题。在 jsp 页面中,我根据角色使用 2 个用户(导师和学生)的菜单,我从菜单文件中选择应包含的内容。像这样
<c:if test="${role eq 'Tutor'}">
<c:import url="/page/menuForTutor.html" charEncoding="UTF-8"/>
</c:if>
<c:if test="${role eq 'Student'}">
<c:import url="/page/menuForStudent.html" charEncoding="UTF-8"/>
</c:if>
我应该使用用户描述符
public class MenuTag extends TagSupport{
private static final String PARAM_ROLE_TUTOR = "Tutor";
private static final String PARAM_ROLE_STUDENT = "Student";
@Override
public int doStartTag(){
HttpServletRequest request = (HttpServletRequest)pageContext.
getRequest();
HttpSession session = request.getSession();
String role = (String) session.getAttribute("role");
if (PARAM_ROLE_TUTOR.equals(role)){
try {
pageContext.getOut().print("<c:import url=\"/page/menuForTutor.html\" charEncoding=\"UTF-8\"/>");
} catch (IOException ex) {
Logger.getLogger(MenuTag.class.getName()).log(Level.SEVERE, null, ex);
}
} else if(PARAM_ROLE_STUDENT.equals(role)){
}
return SKIP_BODY;
}
}
当我使用 debag 时,我看到我执行了所有步骤,但 jsp 上未显示导师菜单。有什么想法吗?
我假设 session("role") 是在另一个代码部分中设置的!
设置 JspWriter out = pageContext.getOut();
类似于代码中的内容
JspFactory factory = JspFactory.getDefaultFactory();
PageContext pageContext = factory.getPageContext(
this,
request,
response,
null, // errorPageURL
false, // needsSession
JspWriter.DEFAULT_BUFFER,
true // autoFlush
);
....
JspWriter out = pageContext.getOut();
HttpSession session = request.getSession();
String role = (String) session.getAttribute("role");
if (PARAM_ROLE_TUTOR.equals(role)){
try {
out.println("<c:import url=\"/page/menuForTutor.html\" charEncoding=\"UTF-8\"/>");
out.flush();
// Evaluate the body if there is one
return EVAL_BODY_BUFFERED;
.......
else return SKIP_BODY;
编辑查看代码:您必须“return EVAL_BODY_BUFFERED;”
我是一名优秀的程序员,十分优秀!