使用Spring MVC、Spring Security和FreeMarker可以实现在每个页面上显示用户名的功能。下面是具体的步骤:
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".ftl"/>
</bean>
<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/views/"/>
</bean>
<security:http>
<security:intercept-url pattern="/secure/**" access="hasRole('ROLE_USER')" />
<security:form-login login-page="/login" default-target-url="/secure/home" authentication-failure-url="/login?error" />
<security:logout logout-success-url="/login?logout" />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="admin" password="admin" authorities="ROLE_USER" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
${pageContext.request.userPrincipal.name}
获取当前登录用户的用户名,并将其显示在页面上。可以参考以下示例代码:<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome, ${pageContext.request.userPrincipal.name}!</h1>
</body>
</html>
通过以上步骤,就可以在每个页面上显示当前登录用户的用户名了。
推荐的腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云