zoukankan      html  css  js  c++  java
  • springMvc参数传递的方法

    1. package cn.edu.hj.controller;  
    2. import java.util.Map;  
    3. import javax.servlet.http.HttpServletRequest;  
    4. import org.springframework.stereotype.Controller;  
    5. import org.springframework.ui.Model;  
    6. import org.springframework.web.bind.annotation.RequestMapping;  
    7. import org.springframework.web.bind.annotation.RequestParam;  
    8.   
    9. @Controller  
    10. //@RequestMapping(value = "/hello")//表示要访问这个action的时候都要加上这个/hello路径  
    11. public class HelloController {  
    12.       
    13. /* 接收参数getParameter()的时候: 
    14.  * 如果地址栏/springmvc/hello.htm上面没有传递参数,那么当id为int型的时候会报错,当id为Integer的时候值为null 
    15.  * 当地址栏为/springmvc/hello.htm?id=10的时候,action中有三种接收方式 
    16.  * 1、String hello(@RequestParam(value = "userid") int id),这样会把地址栏参数名为userid的值赋给参数id,如果用地址栏上的参数名为id,则接收不到 
    17.  * 2、String hello(@RequestParam int id),这种情况下默认会把id作为参数名来进行接收赋值 
    18.  * 3、String hello(int id),这种情况下也会默认把id作为参数名来进行接收赋值 
    19.  * 注:如果参数前面加上@RequestParam注解,如果地址栏上面没有加上该注解的参数,例如:id,那么会报404错误,找不到该路径 
    20.  */  
    21.     @RequestMapping(value = "/hello.htm")  
    22.     public String hello(int id){//getParameter()的方式  
    23.         System.out.println("hello action:"+id);  
    24. //      return "hello";  
    25.         return "redirect:/index.jsp";//不能重定向web-info里面的文件,而且需要写上绝对路径  
    26.     }  
    27.       
    28.     //返回页面参数的第一种方式,在形参中放入一个map  
    29.     @RequestMapping(value = "/hello1.htm")  
    30.     public String hello(int id,Map<String,Object> map){  
    31.         System.out.println("hello1 action:"+id);  
    32.         map.put("name", "huangjie");  
    33.         return "hello";  
    34.     }  
    35.       
    36.     //返回页面参数的第二种方式,在形参中放入一个Model  
    37.     @RequestMapping(value = "/hello2.htm")  
    38.     public String hello2(int id,Model model){  
    39.         System.out.println("hello2 action:"+id);  
    40.         model.addAttribute("name", "huangjie");  
    41.         //这个只有值没有键的情况下,使用Object的类型作为key,String-->string  
    42.         model.addAttribute("ok");  
    43.         return "hello";  
    44.     }  
    45.   
    46.     //得到request,response,session等,只要在方法形参中声明参数即可  
    47.     @RequestMapping(value = "/hello3.htm")  
    48.     public String hello3(HttpServletRequest request){  
    49.         String id = request.getParameter("id");  
    50.         System.out.println("hello3 action:"+id);  
    51.         return "hello";  
    52.     }  
  • 相关阅读:
    Scala中隐式转换(implicit conversion)的优先顺序
    protege4.0使用中的理论
    国外程序员整理的 C++ 资源大全
    什么是本体论?
    深入分析C++引用
    在基于对话框的MFC程序中,使程序在任务栏中不显示图标
    PhoneGap搭建运行环境(3.2版本)
    [JS代码]如何判断ipad或者iphone是否为横屏或者竖屏
    windwos iis 7.5 使用html 报405错误
    NodeJs 开源
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/5785607.html
Copyright © 2011-2022 走看看