<div id="appname">
<h1>${headerText}</h1>
</div>
<div id="userinfo">
<sec:authorize access="isAuthenticated()">${labelWelcome}
<sec:authentication property="principal.username" />
<br/>
<a href="${logoutUrl}">${labelLogout}</a>
</sec:authorize>
</div>
</div>
In Listing 17-60, the changes from the previous version are highlighted in bold. First, the tag library
with the prefix sec is added for Spring Security tag library. Then, a <div> section with the
<sec:authorize> tag is added to detect whether the user is logged in. If yes (i.e., the isAuthenticated()
expression returns true), the user name will be displayed, as well as a logout link.
Listing 17-61 shows the revised menu.jspx file, which has the login form added; the New Contact
option will display only if the user is logged in.
Listing 17-61. Display Login Form
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div id="menu" xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:spring="http://www.springframework.org/tags"
xmlns:sec="http://www.springframework.org/security/tags"
version="2.0">
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<jsp:output omit-xml-declaration="yes" />
<spring:message code="menu_header_text" var="menuHeaderText"/>
<spring:message code="menu_add_contact" var="menuAddContact"/>
<spring:url value="/contacts?form" var="addContactUrl"/>
<spring:message code="label_login" var="labelLogin"/>
<spring:url var="loginUrl" value="/j_spring_security_check" />
<h3>${menuHeaderText}</h3>
<sec:authorize access="hasRole('ROLE_USER')">
<a href="${addContactUrl}"><h3>${menuAddContact}</h3></a>
</sec:authorize>
<sec:authorize access="isAnonymous()">
<div id="login">
<form name="loginForm" action="${loginUrl}" method="post">
<table>
<caption align="left">Login:</caption>
<tr>
<td>User Name:</td>
<td><input type="text" name="j_username"/></td>
</tr>
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home