Spring MVC 環境搭建(二)


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

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

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

spirng 注解

注解實際上相當於一種標記,它允許你在運行時動態地對擁有該標記的成員進行操作。

實現注解需要三個條件:注解聲明、使用注解的元素、操作使用注解元素的代碼。
 

一、注解配置

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">
 
2、在 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>  -->

如下圖所示:

 

二、相關注解說明

1、@Controller

當我們使用注解后,控制器可以不再去實現 Controller 接口,只需在類的頭部加上 @Controller,告訴 spring 該類就是控制器,spring 則會幫你自動裝配。

2、@RequestMapping

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

三、注解使用

1、假如我們訪問 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.2、修改 web.xml 的 url 攔截配置

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>

2、假如我們訪問 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 }

3、假如我們訪問 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM