前端
<body>
<form action="checkVerify" method="post">
姓名:<input type="text" name="name" placeholder="姓名"><br/><p>
密码:<input type="password" name="password" placeholder="密码"><br/>
<div class="row">
<div class="col-xs-6 pull_left">
<div class="form-group">
<input class="form-control" type="tel" id="verify_input" placeholder="请输入验证码" maxlength="4">
</div>
</div>
<div class="col-xs-6 pull_left">
<a href="javascript:void(0);" title="点击更换验证码">
<img id="imgVerify" src="/getVerify" alt="更换验证码" height="36" width="170" onclick="getVerify(this);">
</a>
</div>
<input type="button" onclick="aVerify()" value="提交">
</div>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
function getVerify(obj){
obj.src ="/getVerify?"+Math.random();
}
function aVerify(){
var value =$("#verify_input").val();
$.ajax({
async: false,
type: 'post',
url: '/checkVerify',
dataType: "json",
data: {
verifyInput: value
},
success: function(result){
if(result){
window.location.href = "/login";
}else{
window.location.href = "/page";
}
}
});
}
</script>
</form>
后台
@RequestMapping("/login")
public String login(String name,String password){
User user = userService.login(name, password);
if(user!=null){
return "index";
}else{
return "redirect:/page";
}
}
@RequestMapping(value = "/getVerify")
public void getVerify(HttpServletRequest request, HttpServletResponse response) {
try {
response.setContentType("image/jpeg");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expire", 0);
RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
randomValidateCode.getRandcode(request, response);
} catch (Exception e) {
logger.error("获取验证码失败", e);
}
}
@RequestMapping(value = "/checkVerify", method = RequestMethod.POST,headers = "Accept=application/json")
@ResponseBody
public boolean checkVerify(@RequestParam(required=false) String verifyInput, HttpSession session) {
try{
String inputStr = verifyInput;
String random = (String) session.getAttribute("RANDOMVALIDATECODEKEY");
if (inputStr == null) {
System.out.println(inputStr);
return false;
}
if (inputStr.equals(random)) {
System.out.println(inputStr);
return true;
} else {
System.out.println(inputStr);
return false;
}
}catch (Exception e){
logger.error("验证码校验失败", e);
return false;
}
}
这里的login方法得不到前端name和password的值怎么办