LYNX

Links

Tags

Categories

spring笔记

页面跳转

TODO

参数传递

前端->后端

url参数传递

会伴随页面跳转

  • 前端

访问url:/index?aid=111

  • 后端
@RequestMapping(...)
public String app(@RequestParam(value = "aid", required = false) String aid) {
;
}

form表单参数传递

会伴随页面跳转

  • 前端
<form action="...">
<input name="uid"></input>
</form>
  • 后端
@RequestMapping(...)
public String app(String uid) {
;
}

或者

class User {
String uid;
public void setUid(String uid) {
this.uid = uid;
}
}

@RequestMapping(...)
public String app(User user) {
String uid = user.uid;
}

ajax参数传递

页面不会跳转

  • 前端
$.ajax({ // 获取用户所有app
type: "post",
url: "/action/sign_in",
data: {
"uid": uid,
"password": password
},
success: function (get) {
console.log("signin: " + get);
}
});
  • 后端
@RequestMapping("/action/sign_in")
public String app(HttpServletRequest request) {
String uid = request.getParameter("uid");
}

后端->前端

url参数传递

前提要发生页面跳转

  • 后端
@RequestMapping(...)
public String app() {
return "redirect:/?uid=1";
}
  • 前端
function getRequest() { // 获取url中"?"符后的字串
var url = location.search;
var request = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i ++) {
request[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
return request;
} else {
return null;
}
}

model参数传递

  • 后端
@RequestMapping(...)
public String app(Model model) {
model.addAttribute("user_name", curUser);
}
  • 前端
<p th:text="${user_name}">原来的文本</p>

ajax参数传递

  • 后端
@RequestMapping(...)
@ResponseBody// 必须要有
public List<...> app() {
List<...> tmp = new List<...>();
return tmp;
}
  • 前端
$.ajax({
type: "post",
url: "/user/enterprise",
data: {},
success: function(get) {
for (var i = 0; i < get.size(); i ++) {
console.log(get[i]);
}
}
{);

数据库

TODO

Tags

sql

Categories

1 / 1