Spring中如何传递参数的问题

1. 在超链接中传递参数到后台控制器中:需要在映射的路径的名字后面加上?参数的名字=参数值(使用$来取出集合中的属性值)

<a class="rightColContentDelBtn" href="${APP_PATH}/del?stuId=${student.stuId}">删除</a>

del为在控制器中@RequestMapping中的映射的路径的名字

2. 在超链接中传递参数到Jquery中定义的函数中,需要在超链接中绑定单击响应函数然后再括号中传递具体的参数值,Jquery定义的函数中使用参数接收即可对传递过来的数据进行操作

<a class="rightColContentDelBtn" href="#" onclick="del(${student.stuId})">删除</a>

3. 在使用Ajax进行异步刷新页面数据的时候,使用data属性来传递参数,需要注意的是data中参数的名字要与控制器中接收的参数的名字是要一样,否则接收不到数据

data : {
            "stuId" : stuId
}...

    @ResponseBody
    @RequestMapping("/edit")
    public Object edit(Integer stuId){
        AjaxResult result = new AjaxResult();
        Student student = studentService.queryStudentById(stuId);
        if(student != null){
            result.setRes(true);
            result.add("student", student);
        }
        return result;
    }

4. 使用Ajax的时候可以使用url也可以用来传递参数,类似于超链接传递参数也是加上?参数的名字,再加上具体的参数值

$.ajax({
                 type: "POST",
                 data: $("#updateStudent form").serialize(),    
                 url: "${APP_PATH}/updateStudent?updateStuId=" + updateStuId,
                 success: function(result){
                     $("#updateStudent").modal('hide');
                     toCurrentPage();
                 }
 });

5. 在Jquery定义的函数中也可以传递参数,使用window.location.href,用法也是类似于超链接a,需要在映射的路径的名字后面加上?参数的名字=参数值

if(confirm("确认要删除学号为" + stuId + "的信息吗 ?")){
             window.location.href = "${APP_PATH}/del?stuId=" + stuId;
}

猜你喜欢

转载自blog.csdn.net/qq_39445165/article/details/86544885