《SpringMVC從入門到放肆》8、SpringMVC註解式開發(基本配置)

上一篇咱們結束了配置式開發,配置式開發目前在企業中用的並非不少,大部分企業都在使用註解式開發,因此今天咱們就來學習註解式開發。所謂SpringMVC註解式開發是指,處理器是基於註解的類的開發方式。對於每個定義的處理器,無需在配置文件中逐個註冊,只需在代碼中經過對類與方法的註解,即可完成註冊。html

 

1、註冊組件掃描器java

這裏說的組件即處理器,須要指定處理器所在的基本包。web

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <!-- 註冊組件掃描器 -->
    <context:component-scan base-package="cn.wechatbao.controller"></context:component-scan>
    
</beans>

 

2、第一個註解式Demospring

1:Controllerspring-mvc

package cn.wechatbao.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {

    @RequestMapping("/my.do")
    public ModelAndView first(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "第一個註解式開發程序");
        mv.setViewName("/WEB-INF/jsp/welcome.jsp");
        return new ModelAndView("");
    }

}

 

2:JSP頁面(welcome.jsp)mvc

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>SpringMVC1</title>
  </head>
  
  <body>
    ${message }
  </body>
</html>

 

3:完整的項目結構app

 

 

3、命名空間的配置jsp

通常狀況下,咱們開發時,一個Controller類就是一個模塊,而裏面的全部處理器方法就是該模塊的不一樣業務功能。這個時候,咱們Controller與Controller之間就要用路徑來區分開來。以表示不一樣的業務模塊。這個時候,只須要在類上再加上@RequestMapping("/test")註解就OK了。學習

完整的類以下:spa

package cn.wechatbao.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/test")
public class MyController {

    @RequestMapping("/first.do")
    public ModelAndView first(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "第一個註解式開發程序方法一");
        mv.setViewName("/WEB-INF/jsp/welcome.jsp");
        return mv;
    }
    
    @RequestMapping("/second.do")
    public ModelAndView second(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "第一個註解式開發程序方法二");
        mv.setViewName("/WEB-INF/jsp/welcome.jsp");
        return mv;
    }

}

 

4、請求中通配符的使用

在實際開發的過程當中,咱們可能會遇到請求中的方法開頭或結尾是固定的,其它字符是可變的,好比:

http://localhost:8080/SpringMVC/usermanager/user-add.do

http://localhost:8080/SpringMVC/usermanager/user-edit.do

假設上面URL中usermanager是模塊名(也就是咱們說的命名空間),user-add.do和user-edit.do是具體的請求。可是添加和修改咱們徹底能夠用一個處理器方法來解決。這個時候用通配符就簡單多了。其實配置起來也特別簡單,只須要在處理器方法上面的註解里加*就能夠了。以下

@RequestMapping("/user-*.do")
public ModelAndView userAddOrUpdate(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    ModelAndView mv = new ModelAndView();
    mv.addObject("message", "用戶的添加或修改功能");
    mv.setViewName("/WEB-INF/jsp/welcome.jsp");
    return mv;
}

相關文章
相關標籤/搜索