SpringMVC的簡單傳值,SpringMVC簡單傳值

來源:互聯網
上載者:User

SpringMVC的簡單傳值,SpringMVC簡單傳值

之前學習SpringMVC時感覺他的傳值很神奇:簡便,快捷,高效。

今天寫幾個簡單的傳值與大家分享,希望能對大家有協助。

一、

從後往前傳:

(1)

@Controller@RequestMapping(value={"/hello"})public class HelloController {     @RequestMapping(value={"sub"})    public ModelAndView submit(HttpServletRequest request) throws Exception {       // TODO Auto-generated method stub       ModelAndView m=new ModelAndView();       m.addObject("ok", "hello");       m.setViewName("success");    return m;    }}

 

 

把想要傳遞的東西放在addObject(String,Object)裡,值是Object類型,什麼都可以放。

setViewName() 是設定跳轉到哪個頁面 (success.jsp頁面)。

 

在success.jsp 頁面裡用${requestScope}或${ok}即可取出。是不是非常簡便快捷。

 

還可以以這種方式傳:

@Controller@RequestMapping(value={"/user"})public class UserController {    @RequestMapping(value={"/get"})    public ModelAndView user(User user) throws Exception {       ModelAndView mv=new ModelAndView();       
    mv.addObject("ok",user.getUsername()+"--"+user.getPassword()); mv.setViewName("success"); return mv;  }}

 

前端是一個簡單的form表單:

 

<form action="user/get" method="post">

        <input type="text" name="username" id="username">

        <input type="text" name="password" id="password">

        <input type="submit">

</form>

 

(2)傳回值也可以不是ModelAndView

@RequestMapping(value={"/map"})    public String  ok(Map map,Model model,ModelMap modelmap,User user) throws Exception {    map.put("ok1", user);       model.addAttribute("ok2",user);       modelmap.addAttribute("ok3", user);    return "show";}

 

二、

從前往後傳:

(1)

@RequestMapping(value={"ant/{username}/topic/{topic}"},method={RequestMethod.GET})    public ModelAndView ant(           @PathVariable(value="username") String username,           @PathVariable(value="topic") String topic           ) throws Exception {       // TODO Auto-generated method stub       ModelAndView m=new ModelAndView();       System.out.println(username);       System.out.println(topic);    return m;    }

 

前端是這個樣子:

    <a href="hello/ant/Tom/topic/Cat">ant</a>

與value={"ant/{username}/topic/{topic}"}一一對應。

 

還可以以這種形式:

 

@RequestMapping(value={"/regex/{number:\\d+}-{tel:\\d+}"})    public ModelAndView regex(           @PathVariable(value="number") int number,           @PathVariable(value="tel") String tel           ) throws Exception {       // TODO Auto-generated method stub       ModelAndView m=new ModelAndView();       System.out.println(number);       System.out.println(tel);       return m;    }

 

前端是這個樣子:

    <a href="hello/regex/100-111">regex(正則)</a>

(2)這是有鍵傳值:

 

@RequestMapping(value={"/ok1"})    public String  ok1(@RequestParam(value="username") String username) throws Exception {       System.out.println(username);       return "show";    }

 

前端是這個樣子:

    <a href="user/ok1?username=Tom">有鍵傳值</a>

 

這是無鍵傳值:

@RequestMapping(value={"/ok2"})    public String  ok2(@RequestParam String password,@RequestParam String username) throws Exception {             System.out.println(username);       System.out.println(password);             return "show";    }

 

前端是這個樣子:

    <a href="user/ok2?username=Tom&password=111">無鍵傳值</a>

 

有意思的是它可以準確的對應好兩個值。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.