Using Spring 3 MVC and JSP, I simply want to test if a user is logged in, I’m not interested in using Spring Security currently
<jsp:useBean id="myAppUser" type="com.xxx.MyUser" beanName="myUser" scope="session" />
<c:choose>
<c:when test="myUser.loggedIn">
//dostuff
</c:when>
<c:otherwise>
//dootherstuff
</c:otherwise>
</c:choose>
But the problem is that when there isn’t yet a myAppUser in the session, jsp:useBean throws an exception. Now I realize that I can have the JSP:useBean actually instantiate the object by giving it a class, but I don’t like knowing that somewhere in some JSP fragment I have objects being instantiated and added to my session, so I either want to always set an initial value for that user, and have control over it programatically, or I’d like a way to get that bean that allows it to be null or not exist, if it doesn’t exist just return null
either way would be fine
if my question points to a fundamental misunderstanding in what I should be doing please provide a link to documentation that will thoroughly explain this use case
This is not the right approach, but in fact you could solve the particular issue with the JSTL
<c:catch>.The right approach is described in answer of matt b. You really need to solve it at a higher level. Have a bean something like
UserManagerwhich has theUseras child property along several methods likelogin(User),logout()andisLoggedIn(). If the user is logged in, then theUserought to be non-null. When the user logs out, then theUserought to be set to null.Update as per comments: As an alternative, you can also just get rid of the whole
jsp:useBeandeclaration and intercept on the presence of the MVC-framework injected${myUser}in the session.EL will namely transparently “suppress” potential nullpointerexceptions and just return false.