In user descriptor doesn't work pageContext.getOut(“<c:import…>”)
Question
I have a next question. In jsp page I use menu for 2 users ( tutor and student) in dependence on role I choose which from menu files I should include. Smth like this
<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>
I should use user descriptor
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;
}
}
When I use debag I see that I do all steps but menu for tutor wasn't showed on jsp. Any ideas?
Solution
I assume that session("role") is set in another code part!
set JspWriter out = pageContext.getOut();
is something like following in the code
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;
EDIT see code: you have to "return EVAL_BODY_BUFFERED;"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow