在Spring MVC 環境搭建(一)中咱們知道 spring 的配置是經過 urlmapping 映射到控制器,而後經過實現Controller接口的handlerequest方法轉向頁面。html
但這存在一個問題,當咱們項目的頁面不少時,這樣一個映射對應一個控制器,配置文件將會很臃腫!web
其實在新版本的spring中,urlMapping 已經基本不用,而是採用註解機制。spring
註解實際上至關於一種標記,它容許你在運行時動態地對擁有該標記的成員進行操做。app
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:context="http://www.springframework.org/schema/context" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.1.xsd">
1 <!-- 經過該語句能夠掃描com.myweb及com.myweb下子包中的類 --> 2 <context:component-scan base-package="com.myweb"></context:component-scan> 3 4 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 5 <property name="prefix" value="/WEB-INF/jsp/"></property> 6 <property name="suffix" value=".jsp"></property> 7 </bean> 8 <!--<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 9 <property name="mappings"> 10 <props> 11 <prop key="/index.html">IndexAction</prop> 12 </props> 13 </property> 14 </bean> -->
以下圖所示:jsp
1 package com.myweb; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.servlet.ModelAndView; 6 7 @Controller 8 public class NewsAction { 9 10 @RequestMapping("/news") 11 public ModelAndView ShowNews() { 12 13 ModelAndView mv = new ModelAndView("news"); // 默認爲 news 14 mv.addObject("content", "這是新聞頁面"); 15 return mv; 16 } 17 18 }
1.二、修改 web.xml 的 url 攔截配置post
1 <servlet-mapping> 2 <servlet-name>spring</servlet-name> 3 <url-pattern>/</url-pattern> 4 </servlet-mapping> 5 6 <servlet-mapping> 7 <servlet-name>spring</servlet-name> 8 <url-pattern>/</url-pattern> 9 </servlet-mapping>
1 package com.myweb; 2 3 import com.myweb.tool.BarFactory; 4 import com.myweb.tool.NavBar; 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RequestParam; 8 import org.springframework.web.servlet.ModelAndView; 9 10 @Controller 11 public class NewsAction { 12 13 @RequestMapping("/news") 14 public ModelAndView ShowNewsDetail(@RequestParam(value = "id", required = false) String id) { 15 16 // news?id=1 則觸發此方法 17 ModelAndView mv = new ModelAndView("news"); // 默認爲 news 18 19 NavBar topBar = BarFactory.createBar("top"); 20 NavBar bottomBar = BarFactory.createBar("tottom"); 21 mv.addObject("top_nav", topBar.getBarContent()); 22 mv.addObject("bottom_nav", bottomBar.getBarContent()); 23 24 if (id == null) { 25 mv.addObject("content", "這是新聞列表頁面, 沒有 ID 參數"); 26 } else { 27 mv.addObject("content", "這是新聞頁面, id 是: " + id); 28 } 29 30 return mv; 31 } 32 33 }
1 package com.myweb; 2 3 import com.myweb.tool.BarFactory; 4 import com.myweb.tool.NavBar; 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.PathVariable; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.servlet.ModelAndView; 9 10 @Controller 11 public class NewsAction { 12 13 @RequestMapping("/news/{id}/123456") 14 public ModelAndView ShowNewsDetail2(@PathVariable String id) { 15 16 // news/1/123456 則觸發此方法 17 ModelAndView mv = new ModelAndView("news"); // 默認爲 news 18 19 NavBar topBar = BarFactory.createBar("top"); 20 NavBar bottomBar = BarFactory.createBar("tottom"); 21 mv.addObject("top_nav", topBar.getBarContent()); 22 mv.addObject("bottom_nav", bottomBar.getBarContent()); 23 24 mv.addObject("content", "這是新聞列內容頁面(用PathVariable的方式), id 是:" + id); 25 26 return mv; 27 } 28 29 }