SSM-登录注册01

这篇博客详细介绍了SSM框架下登录注册的实现过程,包括使用JSTL的c:if进行登录状态判断,如何定义拦截器,以及在注册过程中遇到的form submit失效和Ajax异步校验用户名问题的解决方案。同时,还讨论了如何通过设置Ajax的async属性为false来获取返回值,并提到了注册Controller的相关方法。
摘要由CSDN通过智能技术生成

SSM-登录注册01

JSTL c:if 判断登录/注册,用户名显示/注销登录

c:if可以嵌套使用

<!-- 如果用户的session为空则显示登录注册 -->
						<c:if test="${AlxUser_Session.alx_uname==null }">
							<li><a
								href="${pageContext.request.contextPath }/toAlxregister.action">注册</a></li>
							<li><a
								href="${pageContext.request.contextPath }/toAlxlogin.action">登陆</a></li>
						</c:if>
						<!-- 如果用户的session不为空则显示用户信息及注销登录 -->
						<c:if test="${AlxUser_Session.alx_uname!=null }">
							<!-- 如果用户昵称不为空则显示用户昵称 -->
							<c:if test="${AlxUser_Session.alx_ualias!=null }">
								<li>欢迎您!<span style="color: hotpink;">${AlxUser_Session.alx_ualias }</span></li>
							</c:if>
							<!-- 如果用户昵称为空或为空串则显示用户名 -->
							<c:if
								test="${AlxUser_Session.alx_ualias==null||AlxUser_Session.alx_ualias==''}">
								<li>欢迎您!<span style="color: hotpink;">${AlxUser_Session.alx_uname }</span></li>
							</c:if>
							<li><img alt="${AlxUser_Session.alx_uicon }"
								src="${AlxUser_Session.alx_uicon }"></li>

							<li><a
								href="${pageContext.request.contextPath }/alx/alxlogout.action">注销登录</a></li>
						</c:if>

拦截器

拦截器定义

1.创建一个包interceptor
2.创建一个类实现了HandlerInterceptor接口的方法
3.默认方法


public class test implements HandlerInterceptor {
   

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
   
		// TODO 自动生成的方法存根

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
   
		// TODO 自动生成的方法存根

	}

	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
   
		// TODO 自动生成的方法存根
		return false;
	}

}

4.需要在springmvc-config.xml中配置

<!-- 配置拦截器 -->
	<mvc:interceptors>
		<mvc:interceptor>
		<!-- 最先配置拦截所有路径 -->
			<mvc:mapping path="/**" />
			<!-- 根据需求配置,配置不拦截路径 -->
			<mvc:exclude-mapping path="/toAlxindex.action" />
            <mvc:exclude-mapping path="/toAlxregister.action" />
            <mvc:exclude-mapping path="/alxregist.action" />
            <mvc:exclude-mapping path="/alx/checkUname.action" />
            <mvc:exclude-mapping path="/alx/alxregist.action" />
			<bean class="com.ttms.core.interceptor.LoginInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>

5.完善拦截方法

@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
   
		//获取请求的URL
		String url = request.getRequestURI();
		//判断该请求是否包含toAlxlogin.action,或alxlogin.action如果包含则放行,如果不包含则全部拦截
		//这里也可以通过if判断选择拦截路径,但是代码不方便,条件会很多,通过xml的方式拦截
		if(url.indexOf("/toAlxlogin.action")>=
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值