我有下一个问题。在jsp页面中,我使用菜单为2个用户(导师和学生)依赖于我选择的角色,我应该包括菜单文件。像这样的Smth
<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上显示。有什么想法吗?
答案 0 :(得分:1)
我认为会话(“角色”)是在另一个代码部分设置的!
将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;
编辑查看代码:您必须“返回EVAL_BODY_BUFFERED;”