表单传入
前端代码:
<form method="POST" id="user_login_submit"> <div class="form-group"> <label for="email">E-Mail Address</label> <input id="email" type="email" class="form-control" name="email" value="" required autofocus> </div> <div class="form-group"> <label for="password">Password <a href="forgot.html" class="float-right"> Forgot Password? </a> </label> <input id="password" type="password" class="form-control" name="password" required data-eye> </div> <div class="form-group"> <label> <input type="checkbox" name="remember"> Remember Me </label> </div> <div class="form-group no-margin"> <button id="login_btn" type="submit" class="btn btn-primary btn-block"> Login </button> </div> <div class="margin-top20 text-center"> Don't have an account? <a href="register.html">Create One</a> </div> </form>
$(function () { $("#user_login_submit").submit(function () { $.ajax({ url: "/bengmall/login", type: "post",//方法类型 dataType: "json",//预期服务器返回的数据类型 success: function (data) { console.log("提交成功"); }, error: function (data) { alert("登录過程中出現錯誤!"+data.result["result"].message); } }) }); });
前端中传入了一个name 为 email , 一个为 password , 后端使用了一个类来接收数据
@RequestMapping(value = "/login") public String login(User user, HttpServletRequest request, HttpServletResponse response) { ... }
public class User { private String email; private String password; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
URL 路径
当传递一些不重要的数据时,只是为了查询使用时,可以通过URL 路径中放置参数查询。
@PathVariable
1 //http://weibo.com/user1,http://weibo.com/user2
2 @RequestMapping("/usermanager/{username}")
3 public String userManager(@PathVariable("username")String name )
4 {
5 return "usermanager";
6 }
7
1 //http://weibo.com/user1,http://weibo.com/user2 2 @RequestMapping("/usermanager/{username}") 3 public String userManager(@PathVariable("username")String name ) 4 { 5 return "usermanager"; 6 } 7
@RequestParam
1 // http://localhost:8080/springmvc/hello/101?param1=10¶m2=20 2 public String getDetails( 3 @RequestParam(value="param1", required=true) String param1, 4 @RequestParam(value="param2", required=false) String param2){ 5 ... 6 }