Java---学习(5)

来源:互联网 发布:php namespace use 编辑:程序博客网 时间:2024/04/30 11:33

                    web中都有对接受页面请求参数和把后台信息显示的前台页面,也就是向前台页面传递参数,这一节我们看看spring MVC中怎样通过页面来传递参数,传递参数有哪些控制以及属性设置等。可以通过RequestParam,RequestMapping,URL模板对进行灵活设置

                   1.常规参数属性设置

                       常规参数接受我们只需要在Action中定义接受的参数变量名称,就可以获取URL中参数传递的值,也可以使用HttpServletRequest,从中获取参数值

                     

[html] view plain copy
print?
  1. @RequestMapping(value=“Detail”)  
  2.     public String Detail(int id)  
  3.     {  
  4.         // id必须传值,而且必须是数字,负责会报异常 页面URL格式如下,从URL中获取id, http://localhost:8092/springmvcfirst/Product/Detail?id=2  
  5.         System.out.print(id);  
  6.         return “Detail”;  
  7.     }  
  8.       
  9.     @RequestMapping(value=“Detail3”)  
  10.     public String Detail3(HttpServletRequest request)  
  11.     {  
  12.         // 页面URL格式如下,从URL中获取id, http://localhost:8092/springmvcfirst/Product/Detail3?id=2  
  13.         System.out.print(request.getParameter(“id”));  
  14.         return “Detail3”;  
  15.     }  
  16.       
@RequestMapping(value="Detail")    public String Detail(int id)    {        // id必须传值,而且必须是数字,负责会报异常 页面URL格式如下,从URL中获取id, http://localhost:8092/springmvcfirst/Product/Detail?id=2        System.out.print(id);        return "Detail";    }    @RequestMapping(value="Detail3")    public String Detail3(HttpServletRequest request)    {        // 页面URL格式如下,从URL中获取id, http://localhost:8092/springmvcfirst/Product/Detail3?id=2        System.out.print(request.getParameter("id"));        return "Detail3";    }    
                如果想把后台的数据显示到前台页面,我们可以使用Model ,和Map集合,就可以把后台值显示到前台页面              

[java] view plain copy
print?
  1. @RequestMapping(value=“Detail2”)  
  2.     public String Detail2(int id,Model model)  
  3.     {  
  4.         // 页面URL格式如下,从URL中获取id, http://localhost:8092/springmvcfirst/Product/Detail2?id=2  
  5.         System.out.print(id);  
  6.         model.addAttribute(”code”“001”);  //在后台通过Model属性赋值  
  7.         model.addAttribute(”name”“可口可乐”);  
  8.         return “Detail2”;  
  9.     }  
  10.       
  11.       
  12.       
  13.       
  14.     @RequestMapping(value=“Detail4”)  
  15.     public String Detail4(int id,Map<String,String> map)  
  16.     {  
  17.         // 页面URL格式如下,从URL中获取id, http://localhost:8092/springmvcfirst/Product/Detail4?id=2  
  18.         map.put(”code”“123”);  
  19.         map.put(”name”“可口可乐”);  
  20.         return “Detail4”;  
  21.     }  
@RequestMapping(value="Detail2")    public String Detail2(int id,Model model)    {        // 页面URL格式如下,从URL中获取id, http://localhost:8092/springmvcfirst/Product/Detail2?id=2        System.out.print(id);        model.addAttribute("code", "001");  //在后台通过Model属性赋值        model.addAttribute("name", "可口可乐");        return "Detail2";    }    @RequestMapping(value="Detail4")    public String Detail4(int id,Map<String,String> map)    {        // 页面URL格式如下,从URL中获取id, http://localhost:8092/springmvcfirst/Product/Detail4?id=2        map.put("code", "123");        map.put("name", "可口可乐");        return "Detail4";    }
                  前台页面获取         

[html] view plain copy
print?
  1. <%@ page language=“java” contentType=“text/html; charset=UTF-8”  
  2.     pageEncoding=“UTF-8”%>  
  3. <!DOCTYPE html PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>  
  4. <html>  
  5. <head>  
  6. <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10. 编码   {code}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;编码&nbsp;&nbsp;&nbsp;{name}  
  11. </body>  
  12. </html>  
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>编码   ${code} 编码   ${name}</body></html>
               以上代码中的接受参数为id,必须保证url中传值的参数名称为id,类型为整型,否则会报异常。如果想省略,怎么办,我们使用RequestParam进行控制

           2.@RequestParam

             使用RequestParam我们可以对url参数进行别名控制,属性设置,默认值设置等,还是上面的例子,代码我们改成如下

            

[java] view plain copy
print?
  1. @RequestMapping(value=“Detail”)  
  2.     public String Detail(@RequestParam(“skuid”int id)  
  3.     {  
  4.         //  这样页面中的参数可以改为 skuid, 页面URL格式如下,从URL中获取id, http://localhost:8092/springmvcfirst/Product/Detail?skuid=2  
  5.         System.out.print(id);  
  6.         return “Detail”;  
  7.     }  
  8.       
  9.     @RequestMapping(value=“Detail2”)  
  10.     public String Detail2(@RequestParam(value =“skuid”,required =false,defaultValue=“1”int id,Model model)  
  11.     {  
  12.          //可以设置是URL中是否必须带参数,默认值,别名等  
  13.         // 页面URL格式如下,从URL中获取id, http://localhost:8092/springmvcfirst/Product/Detail2?skuid=2  
  14.         System.out.print(id);  
  15.         model.addAttribute(”code”“001”);  //在后台通过Model属性赋值  
  16.         model.addAttribute(”name”“可口可乐”);  
  17.         return “Detail2”;  
  18.     }  
@RequestMapping(value="Detail")    public String Detail(@RequestParam("skuid") int id)    {        //  这样页面中的参数可以改为 skuid, 页面URL格式如下,从URL中获取id, http://localhost:8092/springmvcfirst/Product/Detail?skuid=2        System.out.print(id);        return "Detail";    }    @RequestMapping(value="Detail2")    public String Detail2(@RequestParam(value ="skuid",required =false,defaultValue="1") int id,Model model)    {         //可以设置是URL中是否必须带参数,默认值,别名等        // 页面URL格式如下,从URL中获取id, http://localhost:8092/springmvcfirst/Product/Detail2?skuid=2        System.out.print(id);        model.addAttribute("code", "001");  //在后台通过Model属性赋值        model.addAttribute("name", "可口可乐");        return "Detail2";    }

           3.@RequestMapping

                @RequestMapping也可以直接设置参数,包括参数名称,值,请求方式等

                 

[java] view plain copy
print?
  1. @RequestMapping(value=“ProductIndex”,params={“id=1”,“code”,“!name”},method={RequestMethod.GET})  
  2.     public String ProductIndex()  
  3.     {  
  4.         //页面url http://localhost:8092/springmvcfirst/Product/ProductIndex?id=1&code=001  
  5.         return “ProductIndex”;  
  6.     }  
  7.       
@RequestMapping(value="ProductIndex",params={"id=1","code","!name"},method={RequestMethod.GET})    public String ProductIndex()    {        //页面url http://localhost:8092/springmvcfirst/Product/ProductIndex?id=1&code=001        return "ProductIndex";    }    

            上面的action通过RequestMapping指定了请求方式必须为get,,,     参数id必须存在而且值为1,参数code必须存在,参数name必须不存在

              RequestMapping的重载方法很多,可以去查资料,比如我们可以通过headers属性来控制请求头信息等

          4.@PathVariable

             通过@PathVariable我们可以动态设置URL请求模板的参数格式,对URL的格式做一些限制等。比如我们可以通过正则和通配符最URL做更深一步的控制。

[java] view plain copy
print?
  1. @Controller  
  2. @RequestMapping(“/Product/{variable1}”)  
  3. public class ProductController {  
  4.   
  5.       
  6.     @RequestMapping(value=“ViewDetailOne/{variable2}”)  
  7.     public String ViewDetailOne(@PathVariable String variable1, @PathVariable ( “variable2” ) int variable2)  
  8.     {  
  9.         //url格式  http://localhost:8092/springmvcfirst/Product/aa/ViewDetailOne/3  
  10.         System.out.print(variable1);  
  11.         System.out.print(variable2);  
  12.         return “ViewDetailOne”;  
  13.     }  
  14.      }  
@Controller@RequestMapping("/Product/{variable1}")public class ProductController {    @RequestMapping(value="ViewDetailOne/{variable2}")    public String ViewDetailOne(@PathVariable String variable1, @PathVariable ( "variable2" ) int variable2)    {        //url格式  http://localhost:8092/springmvcfirst/Product/aa/ViewDetailOne/3        System.out.print(variable1);        System.out.print(variable2);        return "ViewDetailOne";    }     }

原创粉丝点击