前端页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册页面</title>
<link rel="stylesheet" href="<%=basePath%>css/bootstrap.min.css">
<script src="<%=basePath%>js/jquery-1.11.0.min.js"></script>
<script src="<%=basePath%>js/bootstrap.min.js"></script>
</head>
<body>
<script language="javascript">
function Register(){
var username=$('#username').val();
var password=$('#psw').val();
var password1=$('#psw1').val();
if(password!=password1){
alert("两次密码不一致!")
}else
$.ajax({
type: "post",
url: "/register",
data :JSON.stringify({username:username,password:password}),
contentType : "application/json;charset=UTF-8",
dataType: "json",
success : function(data){
alert("注册成功!");
window.location.href = "/z";
}
})
}
</script>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">
login
</h3>
</div>
<div class="panel-body">
<div class="col-md-12 column">
<form class="form-horizontal" role="form" action="" method="post">
<div class="form-group">
<label class="col-sm-2 control-label">用户名:</label>
<div class="col-sm-10">
<input type="text" class="form-control " name="username" id="username" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">密码:</label>
<div class="col-sm-10">
<input type="password" class="form-control" name="password" id="psw" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">确认密码:</label>
<div class="col-sm-10">
<input type="password" class="form-control" name="password" id="psw1" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label><input type="checkbox" />Remember me</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" class="btn btn-default" onclick="Register()" >注册</button>
<a href="/">已有账号?返回登陆</a>
</div>
</div>
</form>
</div>
</div>
<div class="panel-footer">
Panel footer
</div>
</div>
</div>
</div>
<div class="row clearfix">
</div>
</div>
</body>
</html>
控制层:
@RequestMapping(value = "/register")
@ResponseBody
public User register( @ModelAttribute User user, Model model) {
model.addAttribute("user",user);
return user;
}