Spring MVC 環境搭建(二) Spring MVC 環境搭建(一)

Spring MVC 環境搭建(一)中咱們知道 spring 的配置是經過 urlmapping 映射到控制器,而後經過實現Controller接口的handlerequest方法轉向頁面。html

但這存在一個問題,當咱們項目的頁面不少時,這樣一個映射對應一個控制器,配置文件將會很臃腫!web

其實在新版本的spring中,urlMapping 已經基本不用,而是採用註解機制。spring

spirng 註解

註解實際上至關於一種標記,它容許你在運行時動態地對擁有該標記的成員進行操做。app

實現註解須要三個條件:註解聲明、使用註解的元素、操做使用註解元素的代碼。
 

1、註解配置

一、首先在 spring-servlet.xml 配置文件中,修改 context 命名空間的申明。
 
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">
 
二、在 spring-servlet.xml 註釋掉 urlMapping 中添加:
 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

 

2、相關注解說明

一、@Controller

當咱們使用註解後,控制器能夠再也不去實現 Controller 接口,只需在類的頭部加上 @Controller,告訴 spring 該類就是控制器,spring 則會幫你自動裝配。

二、@RequestMapping

Spring 經過 RequestMapping 來指定訪問控制器方法來轉向頁面。 在老版本的 Spring 中,若是不一樣的 url 訪問同一個控制器,要使用多動做控制器(MultiActionController )。
使用:在控制器中的方法前加上 RequestMapping 

3、註解使用

一、假如咱們訪問 http://localhost:8080/MyWeb/news 就顯示新聞頁面

 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>

二、假如咱們訪問 http://localhost:8080/MyWeb/news?id=1 就顯示新聞 id=1 的列表頁面

 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 }

三、假如咱們訪問 http://localhost:8080/MyWeb/news/1/123456 就顯示管理新聞 id=1 的列表頁面

 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 }
相關文章
相關標籤/搜索