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}或${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表單把username和password的值傳入:

 

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

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

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

        <input type="submit">

</form>

 在success.jsp 頁面里用${requestScope.ok}或${ok}即可取出。

 

(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>

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM