• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

SSM通过URL实现单值和多值传参的两种方式

武飞扬头像
叼着奶嘴上太空
帮助1

这里主要有@PathVariable和@RequestParam注解两种传参方式
本文的单值传参是根据查询功能写的,多值传参是根据更新功能写的,这里的更新功能中含有复选框的级联操作,需要的小伙伴可以去这里(复选框的级联操作
看看喔~~
如果本文对您有帮助的话请点个小心心,谢谢撒!!!

1.@PathVariable注解传参:接收请求路径中占位符的值

单值传递:
前端:

function queryById(hosrId) {
        $.ajax({
            type: "GET",
            url: "/register/" hosrId, //RESTful风格的API定义
            data: "",
            success: function (vo) {
                ······
            }
        });
    }

后端:
Service层:

@Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Hosregister queryByHosId(int hosrId){
        Hosregister hosregister = registerMapper.selectByPrimaryKey(hosrId);
        hosregister.setDoctor(doctorMapper.selectByPrimaryKey(hosregister.getdId()));
        return hosregister;
    }

Controller层:

@RequestMapping(value = "{id}",method = RequestMethod.GET)
    public ResultVO<Hosregister> queryByHosId(@PathVariable("id") Integer hosrId){
        Hosregister hosregister = registerService.queryByHosId(hosrId);
        return new ResultVO<>(hosregister);
    }

多值传递:
前端:

$(function (){
        //根据地址获取teamId,如果null则执行增加,否则执行更新
        let url = document.location.toString();//获取URL
        let hosrId=url.GetValue("hosrId");
        queryById(hosrId);
        queryKeShi();
        //提交按钮的单击事件--实现真正的更新
        $("#btnUpdate").click(function () {
            let myselect1=document.getElementById("dep");
            let index1=myselect1.selectedIndex ;
            let depId=myselect1.options[index1].value;

            let myselect2=document.getElementById("doc");
            let index2=myselect2.selectedIndex ;
            let dId=myselect2.options[index2].value;
            $.ajax({
                type: "POST",//这里必须写POST
                url: "/register/load/" hosrId "/" depId "/" dId,
                data: $("#myForm").serialize() "&_method=PUT",
                dataType:"json",
                success: function(vo){
                    if(vo.code==200) {
                        window.location.href = "https://blog.csdn.net/pages/registration/index.html";
                    }else{
                        alert("更新失败!" vo.msg);
                    }
                }
            });
        });
    });
学新通

后端:
Service层:

@Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateRegister(Hosregister hosregister){
        return registerMapper.updateByPrimaryKeySelective(hosregister);
    }

Controller层:

@RequestMapping(value = "load/{hosrId}/{depId}/{dId}",method = RequestMethod.PUT)
    public ResultVO<Hosregister> updateRegister(@PathVariable("hosrId") Integer hosrId,
                                                @PathVariable("depId") Integer depId,
                                                @PathVariable("dId") Integer dId,
                                                Hosregister hosregister){
        hosregister.setHosrId(hosrId);
        hosregister.setdId(dId);
        hosregister.setDepId(depId);
        int i = registerService.updateRegister(hosregister);
        if (i == 1){
            return new ResultVO<Hosregister>();
        }
        return new ResultVO<>(500,"更新失败,服务器内部异常,请稍后再试!");
    }

2.@RequestParam注解传参:将请求参数区数据映射到功能处理方法的参数上(注意:请求路径的等号不能丢!!!)

单值传递:
前端:

function queryById(hosrId) {
        $.ajax({
            type: "GET",
            url: "/register" "?hosrId=" hosrId,
            data: "",
            success: function (vo) {
                ......
            }
        });
    }

后端:
Service层:

@Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Hosregister queryByHosId(int hosrId){
        Hosregister hosregister = registerMapper.selectByPrimaryKey(hosrId);
        hosregister.setDoctor(doctorMapper.selectByPrimaryKey(hosregister.getdId()));
        return hosregister;
    }

Controller层:

@RequestMapping(value = "",method = RequestMethod.GET)
    public ResultVO<Hosregister> queryByHosId(@RequestParam("hosrId") Integer hosrId){
        Hosregister hosregister = registerService.queryByHosId(hosrId);
        return new ResultVO<>(hosregister);
    }

多值传递:
前端:

$(function (){
        //根据地址获取teamId,如果null则执行增加,否则执行更新
        let url = document.location.toString();//获取URL
        let hosrId=url.GetValue("hosrId");
        queryById(hosrId);
        queryKeShi();
        //提交按钮的单击事件--实现真正的更新
        $("#btnUpdate").click(function () {
            let myselect1=document.getElementById("dep");
            let index1=myselect1.selectedIndex ;
            let depId=myselect1.options[index1].value;

            let myselect2=document.getElementById("doc");
            let index2=myselect2.selectedIndex ;
            let dId=myselect2.options[index2].value;
            $.ajax({
                type: "POST",//这里必须写POST
                url: "/register/load" "?hosrId=" hosrId "&depId=" depId "&dId=" dId,
                data: $("#myForm").serialize() "&_method=PUT",
                dataType:"json",
                success: function(vo){
                    if(vo.code==200) {
                        window.location.href = "https://blog.csdn.net/pages/registration/index.html";
                    }else{
                        alert("更新失败!" vo.msg);
                    }
                }
            });
        });
    });
学新通

后端:
Service层:

@Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateRegister(Hosregister hosregister){
        return registerMapper.updateByPrimaryKeySelective(hosregister);
    }

Controller层:

@RequestMapping(value = "load",method = RequestMethod.PUT)
    public ResultVO<Hosregister> updateRegister(@RequestParam("hosrId") Integer hosrId,
                                                @RequestParam("depId") Integer depId,
                                                @RequestParam("dId") Integer dId,
                                                Hosregister hosregister){
        hosregister.setHosrId(hosrId);
        hosregister.setdId(dId);
        hosregister.setDepId(depId);
        int i = registerService.updateRegister(hosregister);
        if (i == 1){
            return new ResultVO<Hosregister>();
        }
        return new ResultVO<>(500,"更新失败,服务器内部异常,请稍后再试!");
    }

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaejcg
系列文章
更多 icon
同类精品
更多 icon
继续加载