SpringMVC 获取前端值的方式

来源:互联网 发布:安装ubuntu出错 编辑:程序博客网 时间:2024/06/04 19:44

1 @PathVariable

@pathVarible注解加载方法的参数前面,获取jsp中通过url传递过来的参数
    @RequestMapping(value="/client_listpage/{currentPage}")    public String getClientByPage2(@PathVariable Integer currentPage,Model model) {        int allRecord=clientService.getCount();        Page page=new Page(currentPage, allRecord);        List<Client> clients=clientService.getClientByPage(page.getStartIndex());        model.addAttribute("page", page);        model.addAttribute("clients",clients);        return "ClientListPage";    }

如上所示,带有pathVariable注解的参数,会自动获取通过url传递过来的参数的值

<a href="<c:url value='/client_listpage/${page.currentPage-1}'/>">上一页</a>

jsp中通过在url后面加上你想要传递的参数即可

2@RequestParam(“key”)
也是加载方法参数上的注解,可以接受同过from表单传递过来的值,当没有实现表单绑定时,可以直接获取input属性中 name的值
用法:

    public String selectClientBy(@RequestParam("id")Integer id,@RequestParam("name")String name, Model model) {.....}
<form action="client_selectby">    id:<input type="text" name="id" >    name:<input type="text" name="name">    <input type="submit"> </form>

@requestParam(“参数”) 这个参数为jsp中from属性的input元素的name值,可以直接获取到这个值,赋值给加@requestParam的参数

3@ModelAttribute
可以直接获取一个模块的值,实体类定义get set方法后,modelAttribute可以直接将页面传来的值绑定在实体类中
实现:

@RequestMapping(value="/client_save")    public String saveClient(@ModelAttribute Client client,Model model,BindingResult result){        if (result.hasErrors()) {            model.addAttribute("error",result.getAllErrors());            return "ClientAdd";        }        clientService.insertClient(client);        return "redirect:/client_listpage";    }
<form:form action="client_save" commandName="client" >   <form:input path="mobile" id="mobile" value="${error.mobile}"/>

通过commandName 与实体类对象进行绑定
path与实体类属性进行绑定

这样就能获取一组绑定的值了

获取多个值,ModelAndView
页面:

<input type="text" name="p_name" value="aileen"><input type="text" name="p_age" value="12"><input type="text" name="p_school" value="育红小学">

后台

Map map = WebUtils.getParametersStartingWith(request, "p_");

得到的map 键是 search_ 后边的内容, 值是: value的内容
这个页面提交的内容得到的map内容应该是:{name=aileen,age=12,school=育红小学}
Map filter=WebUtils.getParametersStartingWith(request, “p_”)

获取form表单中所有含有name属性的 name值,在通过request.getparameter(namekey)获取值    protected String getParam(HttpServletRequest request, String name)    {        String val = request.getParameter(name);        if(val != null)            return val;        for(Enumeration names = request.getParameterNames(); names.hasMoreElements();)        {            String n = (String)names.nextElement();            if(n.equalsIgnoreCase(name))                return request.getParameter(n);        }        return null;    }

spring-Boot中

@GetMapping @PostMapping
原创粉丝点击