In my servlet I setup a request attribute with its name coming from another class.
In my JSP I want to access that request attribute using EL but how to tell EL to look for the name of the request attribute as a constant field of another class?
This is my example:
I have a class RoleDTO
:
public class RoleDTO implements Serializable {
private int roleId;
private String roleDescription;
public int getRoleId() {
return roleId;
}
public void setRoleId(int roleId) {
this.roleId = roleId;
}
public String getRoleDescription() {
return roleDescription;
}
public void setRoleDescription(String roleDescription) {
this.roleDescription = roleDescription;
}
}
I have a class RoleConstant
which has a constant defined:
public class RoleConstant {
public static final String ROLE_LIST = "roleDTOs";
}
I have a servlet RoleServlet
where I am creating a List of RoleDTO
objects and setting it to request object as an attribute with the attribute name same as the constant defined in RoleConstant
class, before forwarding to role.jsp
:
public class RoleServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<RoleDTO> roleDTOList = new ArrayList<RoleDTO>();
RoleDTO roleDTO1 = new RoleDTO();
roleDTO1.roleId = 1;
roleDTO1.roleDescription = "Administrator";
roleDTOList.add(roleDTO1);
RoleDTO roleDTO2 = new RoleDTO();
roleDTO2.roleId = 2;
roleDTO2.roleDescription = "Guest";
roleDTOList.add(roleDTO2);
request.setAttribute(RoleConstant.ROLE_LIST, roleDTOList);
RequestDispatcher view = request.getRequestDispatcher("role.jsp");
view.forward(request, response);
}
}
In the role.jsp
I access the request attribute by name to get the List of RoleDTO
objects and enable or disable a <select>
html element based on its contents:
<c:if test="${empty Test05Constant.ATTRIBUTE_ROLE_LIST}">
<select id="select_role" name="select_role" disabled="disabled">
</select>
</c:if>
<c:if test="${not empty Test05Constant.ATTRIBUTE_ROLE_LIST}">
<select id="select_role" name="select_role">
</select>
</c:if>
But the above code does not work and the <select>
html element is shown as disabled even when there is List of RoleDTO
objects.
If I hardcode the request attribute name like this:
<c:if test="${empty roleDTOs}">
<select id="select_role" name="select_role" disabled="disabled">
</select>
</c:if>
<c:if test="${not empty roleDTOs}">
<select id="select_role" name="select_role">
</select>
</c:if>
then it works and the <select>
html element is shown as enabled when there is List of RoleDTO
objects
I cannot do hardcoding in this case. Can anybody tell me how to get this working?
Thanks