日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

【Spring MVC学习】spring mvc入门示例

發布時間:2024/4/14 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Spring MVC学习】spring mvc入门示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

之前進公司就直接使用spring mvc做項目了,并沒有系統學習過,后來再回頭去總結的時候發現我只是在項目中會使用,但是有關spring mvc的配置,還有注解什么的想起來會感覺很混亂,趁著這幾天不忙,好好地系統學習總結一下spring mvc。本文只是描述了如何創建一個簡單spring mvc工程,有關spring mvc注解的內容請參看本人的另外一篇文章:

http://blog.csdn.net/lmb55/article/details/50879776

下面就以一個spring mvc小例子開始這次學習。

1、導入spring mvc相關的jar包;

2、在web.xml中加入如下有關spring mvc的配置:

<servlet><servlet-name>appServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>appServlet</servlet-name><url-pattern>/restservice/*</url-pattern></servlet-mapping>

注意:

(1)、DispatcherServlet是前端控制器,每一個請求最先訪問的都是DispatcherServlet,配置在web.xml中,攔截匹配的請求,DispatcherServlet攔截匹配規則需要自己定義,把攔截下來的請求依據相應的規則分發給目標controller去處理。是配置spring mvc的第一步。

(2)、load-on-startup:表示啟動容器時初始化該Servlet;

(3)、url-pattern:表示哪些請求交給Spring MVC處理, 本例配置中resetservice下的所有請求均交給spring mvc來處理。

自此請求已交給Spring MVC框架處理,因此我們需要配置Spring的配置文件,默認DispatcherServlet會加載WEB- INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件,因本配置給出了初始化參數init-param,所以會加載WEB-INF/spring/appServlet/servlet-context.xml。

3、配置spring的配置文件(在WEB-INF/spring/appServlet下配置名為servlet-context.xml的配置文件)

<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:beans="http://www.springframework.org/schema/beans"xmlns:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 該配置的功能為:啟動包掃描功能;下面會有詳細介紹 --><context:component-scan base-package="com.hollycrm.hollyuniproxy.server.http,com.hollycrm.**.mvc" /><!-- if you use annotation you must configure this setting--><mvc:annotation-driven /><!-- 攔截有關靜態資源的訪問,在訪問靜態資源的時候就可以不經過controller。所有的頁面引用到的"/resources/**"資源都到/resources/目錄下面去找 --><mvc:resources mapping="/resources/**" location="/resources/" /><mvc:resources mapping="/**" location="/" /><!-- 如果當前視圖是"/",則交給相應的視圖解析器直接解析為視圖 --><mvc:view-controller path="/" view-name="login"/><mvc:view-controller path="/welcome" view-name="home"/><!-- 重定向:如果當前視圖 是"/",則重定向到/admin/index--><mvc:view-controller path="/" view-name="redirect:/admin/index"/><!-- 視圖名稱解析器 --><beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><beans:property name="prefix" value="/" /><beans:property name="suffix" value=".jsp" /></beans:bean>

注意:

1、命名空間<context:component-scan />

首先,如果要使注解工作,則必須配置component-scan 。該配置的功能為:啟動包掃描功能,以便注冊帶有@Controller、@Service、@repository、@Component等注解的類成為spring的bean。例:

<context:component-scanbase-package="com.tgb.web"/>

base-package 屬性指定了需要掃描的類包,類包及其遞歸子包中所有的類都會被處理。還允許定義過濾器將基包下的某些類納入或排除。
  
Spring支持以下4種類型的過濾方式:
    
    1) 注解org.example.SomeAnnotation 將所有使用SomeAnnotation注解的類過濾出來;
    
    2) 類名指定org.example.SomeClass 過濾指定的類;
    
    3) 正則表達式com.kedacom.spring.annotation.web..* 通過正則表達式過濾一些類;
    
    4) AspectJ 表達式 org.example..*Service+ 通過AspectJ 表達式過濾一些類;
    
示例如下:

<context:component-scan base-package="com.hollycrm.hollyuniproxy.server.http,com.hollycrm.**.mvc"><!-- 正則表達式的過濾方式 --><context:exclude-filter type="regex" expression="com.hollycrm.hollyuniproxy.server.http"/><!-- 注解的過濾方式 --><context:exclude-filter type="annotation" expression="com.hollycrm.Service"/><context:exclude-filter type="annotation" expression="com.hollycrm.Controller"/><context:exclude-filter type="annotation" expression="com.hollycrm.Repository"/></context:component-scan>

2、InternalResourceViewResolver:視圖名稱解析器,用于支持Servlet、JSP視圖解析;prefix和suffix:查找視圖頁面的前綴和后綴(前綴[邏輯視圖名]后綴),比如傳進來的邏輯視圖名為hello,則該該jsp視圖頁面應該存放在“WEB-INF/jsp/hello.jsp”;

今天先寫到這兒,稍后新建一個spring mvc的工程傳到資源頁,再來補~

總結

以上是生活随笔為你收集整理的【Spring MVC学习】spring mvc入门示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。