1. 程式人生 > >第一個註解式的SpringMVC專案

第一個註解式的SpringMVC專案

1.匯入jar
1)匯入Spring的四個核心jar。 spring-beans.jar ,spring-core.jar, spring-context.jar, spring-expression.jar
2)日誌的jar 。 commons-logging.jar
3)SpringMVC中jar , spring-context-support:工具類,UI,快取類
spring-webmvc.jar : SpringMVC框架的實現包。
兩個jar都位於Spring的核心資料夾/libs/
4)註解式開發需要spring-aop.jar 。 位於Spring核心資料夾/libs/spring-aop.jar

2.定義SpringMVC的配置檔案,配置檔案和Spring的是一樣
註冊元件掃描器<context>標籤。 引入spring-context.xsd約束檔案

<?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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 註冊元件掃描器 --> <context:component-scan base-package="com.sinosoft.controllers"/> <!-- 註冊檢視解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>

3.在web.xml註冊中央排程器(DispatcherServlet)。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>01</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 註冊中央排程器 -->
  <servlet>
      <servlet-name>springmvc01</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>
      
      <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>springmvc01</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  
</web-app>

4.定義處理器,接收使用者的請求。對請求作出處理。
@Controller:表示處理器類
@RequestMapping:在方法上定義處理器方法處理的請求URI,在類上定義名稱空間

package com.sinosoft.controllers;

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

@Controller
@RequestMapping(value="/user")
public class MyController {
    
    @RequestMapping(value={"/some.do","/two.do"})
    public ModelAndView doSome(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","doSome annotion SpringMVC");
        mv.setViewName("show");
        return mv;
    }
    
    @RequestMapping(value="/other.do")
    public ModelAndView doOther(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","doOther annotion SpringMVC");
        mv.setViewName("show");
        return mv;
    }
    
}

5)定義顯示處理結果的頁面(檢視)