记录基于SSH框架下登录验证码模块的实现过程步骤。
1、前端页面代码:
主要以jQuery的ajax异步请求实现。
...
<script type="text/java script" src="${pageContext.request.contextPath }/js/jquery-1.11.3.min.js"></script>
<script type="text/java script">
$(function () {
//验证码图片刷新
$("#vcode").click(function () {
$(this).prop("src","${pageContext.request.contextPath }/user_createVerificationCode?"+new Date());
});
//验证码校验
$("#txtcode").blur(function () {
var url = "${pageContext.request.contextPath }/user_checkVerificationCode"
var code = $(this).val();
$.get(url,{"code":code},function (result) {
if(result != ""){
$("#tip").html(result);
//验证码错误,登录按钮失效
$("#btn").prop("disabled","disabled");
}else{
$("#tip").html("");
$("#btn").removeAttr("disabled");
}
});
});
})
</script>
...
<tr>
<td style="HEIGHT: 28px">验证码:</td>
<td style="HEIGHT: 28px"><input id=txtcode style="WIDTH: 130px" name=txtcode required></td>
<td style="HEIGHT: 18px"><img id="vcode" src="${pageContext.request.contextPath }/user_createVerificationCode" /></td>
<td style="HEIGHT: 28px"> <span id="tip" style="color: red"></span></td>
<tr>
<td></td>
<td>
<input id="btn" value="登录">
</td>
</tr>
...
2、struts.xml
<package name="user" extends="struts-default" namespace="/">
<action name="user_*" class="userAction" method="{1}"></action>
</package>
3、applicationContext.xml
<bean id="userAction" class="com.pri.web.action.UserAction" scope="prototype">
<property name="validateCode" ref="validateCode"/>
</bean>
<!-- 生成验证图片需要为多例,默认情况为单例 -->
<bean id="validateCode" class="cn.dsna.util.images.ValidateCode" scope="prototy