Spring MVC 註解

URL路徑映射

  • 這三個等價
@RequestMapping("/hello")
@RequestMapping("/hello.html")
@RequestMapping("hello")
  • 可以配置多級路徑
@RequestMapping("/test/hello.html")
@RequestMapping("test/hello")
  • 同一個方法可以配置多個路徑
@RequestMapping(value={"/test/hello.html","wow.html"})
@RequestMapping(path={"/test/hello.html","haha"})
  • url映射可以使用通配* ,* 這裏可以填任意字符用來表示一級目錄,**匹配多級目錄,?匹配一個字符(示例中可填任意字符)
@RequestMapping("/*/hello.html")
@RequestMapping("/**/hello.html")
@RequestMapping("/**/*hello?")

窄化請求映射

@RequestMapping("/user")
@Controller
public class SecondController{}

//等價於
@RequestMapping("/user")
@Controller
public class SecondController{}

這倆的作用是在方法的url路徑之前增加/user

請求方法限定

@ RequestMapping(path = “/haha2”,method = RequestMethod.POST)
只處理POST請求

其他

  • params的使用
    @RequestMapping(path = "/haha2",method = RequestMethod.GET,params = {"id"})
// http://localhost:8080/haha2?id

    @RequestMapping(path = "/haha2",method = RequestMethod.GET,params = {"id!=123"})
//http://localhost:8080/haha2?id=1234

    @RequestMapping(path = "/haha2",method = RequestMethod.GET,params = {"id=123","age=1234"})
//http://localhost:8080/haha2?id=123&age=1234
  • 請求頭需要滿足的條件
    @RequestMapping(path = "/haha2",method = RequestMethod.GET,params = {"id=123","age=1234"},headers = "localhost")

Controller方法返回值

返回ModelAndView

  • 我們之前的就是返回的ModelAndView
    @RequestMapping(path = "/haha2",method = RequestMethod.GET,params = {"id=123","age=1234"},headers = "localhost")
    public ModelAndView handleRequest(HttpServletRequest request) throws Exception {
        //模型和視圖
        ModelAndView mv = new ModelAndView();
        User user = new User();
        user.setUsername("苟利國家生死以");
        user.setPassword("豈因禍福避趨之");
        //把對象放進去了
        mv.addObject("user",user);
        //把視圖放進去了
        mv.setViewName("/index.jsp");
        return mv;
    }

void 方式(最原始的方式)

@RequestMapping(path = "/haha3")
    public void test(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("test");
        User user = new User();
        user.setId(1);
        user.setUsername("haha");
        user.setPassword("12345");
        request.setAttribute("user",user);
        request.getRequestDispatcher("WEB-INF/index.jsp").forward(request,response);
    }

返回字符串

  • 表示以web根目錄作爲起點的路徑(絕對路徑)
    @RequestMapping(path = "/haha4")
    public String test2(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("test2");
        User user = new User();
        user.setId(1);
        user.setUsername("test2");
        user.setPassword("12345");
        request.setAttribute("user",user);
        return "/WEB-INF/index.jsp";
    }
  • 表示以當前目錄作爲起點的路徑(相對路徑)
    @RequestMapping(path = "/haha5")
    public String test3(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("test3");
        User user = new User();
        user.setId(1);
        user.setUsername("test3");
        user.setPassword("12345");
        request.setAttribute("user",user);
        return "index.jsp";
    }

可以用return返回WEB-INF下的資源

邏輯視圖

返回的字符串是物理視圖的一部分,完整的物理視圖這樣:

  • 物理視圖名
    mv.setViewName(“/WEB-INF/userlist.jsp”);
  • 頁面解析器注入兩個參數
<!--頁面解析器-->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
  • 之後返回的視圖名稱——邏輯視圖名:
    mv.setViewName(“userlist”);

返回字符串的時候model的返回

  • request.setAttribute(“user”,user);
  • model.addAttribute(“user”,user);

返回字符串的時候可以轉發(默認實現)

  • return “forword:/haha/index.jsp”;

返回字符串的時候可以重定向

  • return “redirect:/haha/index.jsp”;
發佈了85 篇原創文章 · 獲贊 18 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章