- 方式1–使用HTTPServletRequest
@RequestMapping("/param1")
public Object testParam1(HttpServletRequest request){
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username+"---"+password);
return null;
}
- 方法2的参数名和页面参数一致
@RequestMapping("/param2")
public Object testParam2(String username1,String password1){
System.out.println(username1+"---"+password1);
return null;
}
- 方式3–方法参数名和页面参数名称不一致 (但是又想拿到 页面参数)
@RequestMapping("/param3")
public Object testParam3(@RequestParam(name="username") String username1,
@RequestParam("password") String password1){
System.out.println(username1+"---"+password1);
return null;
}
- 方式4–参数过多使用对象作为参数
@RequestMapping("/param4")
public Object testParam4(User u){
System.out.println(u);
return null;
}
- 方式5–包含时间类型,需要类型转换
@RequestMapping("/param5")
public Object testParam5(User u){
System.out.println(u);
return null;
}
前5种是前台jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<fieldset>
<legend>参数传递方式1--使用HTTPServletRequest</legend>
<form action="param/param1" method="get">
账号:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
<input type="submit" value="提交"><br>
</form>
</fieldset>
<fieldset>
<legend>参数传递方式2--方法的参数名和页面参数一致</legend>
<form action="param/param2" method="post">
账号:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
<input type="submit" value="提交"><br>
</form>
</fieldset>
<fieldset>
<legend>参数传递方式3--方法参数名和页面参数名称不一致</legend>
<form action="param/param3" method="post">
账号:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
<input type="submit" value="提交"><br>
</form>
</fieldset>
<fieldset>
<legend>参数传递方式4--参数过多使用对象作为参数</legend>
<form action="param/param4" method="post">
账号:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
<input type="submit" value="提交"><br>
</form>
</fieldset>
<fieldset>
<legend>参数传递方式5--包含时间类型,需要类型转换</legend>
<form action="param/param5" method="post">
账号:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
日期:<input type="text" name="time"><br>
<input type="submit" value="提交"><br>
</form>
</fieldset>
</body>
</html>
方法5的日期转换类
package cn.hp.utils;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeUtils implements Converter<String, Date> {
@Override
public Date convert(String s) {
if(StrUtil.isEmpty(s)){
throw new RuntimeException("日期不能为空");
}
DateTime time = DateUtil.parse(s, "yyyy-MM-dd");
return time;
}
}
- restful风格api
@GetMapping("/param6/{id}")
public Object testParam6(@PathVariable("id") Integer abc){
System.out.println("根据id删除==="+abc);
return null;
}
- 该方法用于获取json数据 并返回到 前台
@RequestMapping("/param7")
@ResponseBody
public Object testJson(String username,String password){
Map<String,Object> map = new HashMap<>();
if(StrUtil.isEmpty(username)){
map.put("type","error");
map.put("msg","用户名不能为空");
return map;
}
if(StrUtil.isEmpty(password)){
map.put("type","error");
map.put("msg","密码不能为空");
return map;
}
map.put("type","success");
map.put("msg","登陆成功");
return map;
}
异步提交json的jsp页面
<%--
Created by IntelliJ IDEA.
User: yuan
Date: 2020/2/23
Time: 22:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>异步提交</title>
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script>
$(function () {
$("#btn").click(function () {
var name = $("#name").val();
var pass = $("#pass").val();
$.ajax({
url:'param/param7',
data:{username123:name,"password":pass},
type:'post',
dataType:'json',
success:function (data) {
console.log(data);
if(data.type=='success'){
alert("跳转到首页");
}
else{
alert("登陆失败");
}
}
});
});
});
</script>
</head>
<body>
<!--在前端页面发送一个异步请求到 后台 -->
<fieldset>
<legend>异步提交</legend>
<div>
账号:<input type="text" name="username" id="name"><br>
密码:<input type="text" name="password" id="pass"><br>
<input type="button" id="btn" value="提交"><br>
</div>
</fieldset>
</body>
</html>