Testing if the user is registered through JSP / Spring -MVC

Using Spring 3 MVC and JSP, I just want to check if the user is registered, I am not interested in using Spring Security now

<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 myAppUser is not in the session yet, jsp: useBean throws an exception. Now I understand that I can have a JSP: useBean actually creates an instance of the object, giving it a class, but I don’t like to know that somewhere in some fragment of the JSP I have objects that are created and added to my session, so I either want to always set the initial value for this user and control it programmatically, or I would like to get a bean method that allows it to be null or not exist if it does not exist, just returns null

anyway will be fine

if my question indicates a fundamental misunderstanding of what I should do, provide a link to documentation that fully explains this use case

+3
2

, JSTL <c:catch>.

<c:catch var="e">
    <jsp:useBean id="myAppUser" type="com.xxx.MyUser" beanName="myUser" scope="session" />
</c:catch>
<c:choose>
    <c:when test="${empty e && myUser.loggedIn}">Logged in</c:when>
    <c:otherwise>Bean doesn't exist or user is not logged in</c:otherwise>
</c:choose>

b. . bean - UserManager, User , login(User), logout() isLoggedIn(). , User . , User null.


. jsp:useBean MVC-, ${myUser}.

<c:choose>
    <c:when test="${myUser.loggedIn}">Logged in</c:when>
    <c:otherwise>Bean doesn't exist or user is not logged in</c:otherwise>
</c:choose>

EL "" nullpointer false.

+4

- " ?" Session "" , .

, JSTL , jsp:useBean , , isLoggedIn .

  • , , isLoggedIn true false.
  • ${isLoggedIn}.

, / .

+2

Source: https://habr.com/ru/post/1748551/


All Articles