基於註解的spring3.0.x MVC學習筆記

來源:互聯網
上載者:User

這次開始主要對spring的每個註解用法進行詳細的介紹,
@RequestMapping的參數如下

/**

 * @see RequestMapping 參數

 *

 * @param value

 *            需要跳轉的地址

 * @param mehtod

 *            基於RestFul的跳轉參數,有RequestMethod.get post,put 等

 * @param params

 *            符合某個參數的時候才調用該方法

 * @param headers

 *            符合頭資訊的時候才調用

 * */

SpringMVC中大部分請求都是由RequestMapping提交的,而且提交的類型有很多種,以3.0來講一般的請求方式有以下幾種

第一種:以無參的形式返回:

/**

  * 無參數返回的是根據 prefix首碼+@RequestMapping value +suffix尾碼組成

   *

   * */

  @RequestMapping("/novoid")

  public void novoid() {

      logger.info(this.getClass().getSimpleName() + "novoid方法被調用");

  }

返回的地址是http://訪問地址/項目名稱/spring設定檔bean 為viewResolver 的prefix的值+requestMapping返回的值+suffix的值

第二種:返回一個String類型:

/**

     * 根據String字串返回對應的地址 prefix首碼+傳回值+suffix尾碼組成

     * */

    @RequestMapping("/string")

    public String string() {

        logger.info("String 方法調用");

        return "WEB-INF/jsp/success";

    }

第三種:返回一個ModelAndView對象

/**

 * spring2.5的方法,返回一個ModelAndView 對象,通過setViewName方法跳轉到指定的頁面 調用addObject

 * 相當於調用request.setAttribute方法

 * */

@RequestMapping("/modelview")

public ModelAndView view(Model model) {

    logger.info("view 方法調用");

    ModelAndView andView = new ModelAndView();

    andView.setViewName("WEB-INF/jsp/success");

    return andView;

}

第四種返回一個Map集合

/**

     * @see 使用map作為傳回值的時候 是以prefix首碼+requestMapping的value+suffix尾碼組成 返回一個map

     *      ,map的put方法調用相當於request.setAttribute方法

     * */

    @RequestMapping("/mapa")

    public Map<String, Object> mapa(ModelMap map1) {

 

        Map<String, Object> map = new HashMap<String, Object>();

        UserBean bean = new UserBean();

        bean.setId(1);

        bean.setUsername("Edward Lau");

        bean.setPassword("edward");

        map.put("hello", "world key");

        map.put("user", bean);

        return map;

    }

使用第四種方法,可以在頁面中通過調用JSTL進行取值,如下面jsp代碼

<%@ 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>Mapa</title>

</head>

<body>

Map a

姓名:${user.username }

密碼:${user.password }

hello:${hello }

</body>

</html>

第五種返回一個ModelMap類型:

/**
     * @see 返回一個ModelMap類型,返回地址根據以prefix首碼+requestMapping的value+suffix尾碼組成
     *      ModelMap 本身也擁有hashmap的方法,也可以使用addAllAttributes對一個map添加到attribute裡面
     * */
    @RequestMapping("/map")
    public ModelMap map() {
        ModelMap map = new ModelMap();
        map.addAttribute("aa", "bb");
        map.addAllAttributes(temp());
        return map;
    }

    /**
     *@see 臨時類
     *@return 返回一個map類型
     * */
    public Map<String, UserBean> temp() {
        Map<String, UserBean> map1 = new HashMap<String, UserBean>();
        UserBean bean = new UserBean();
        bean.setId(1);
        bean.setUsername("Edward Lau");
        bean.setPassword("edward");
        map1.put("user", bean);
        // map1.put("hello", "world key");
        UserBean bean1 = new UserBean();
        bean1.setId(2);
        bean1.setUsername("Edward Lau2");
        bean1.setPassword("edward");
        map1.put("user1", bean1);
        System.out.println(map1);
        return map1;
    }

使用ModelMap可以把一個多個集合存到一個屬性中,可以直接在頁面調用EL 語言進行讀取,jsp代碼如下:

<%@ 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>Mapa</title>

</head>

<body>

Map a

姓名:${user.username }

密碼:${user.password }

hello:${hello }

aa:${aa }

</body>

</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.