1. 程式人生 > 實用技巧 >SpringMVC專案基礎架構及配置概述

SpringMVC專案基礎架構及配置概述

一、SpringMVC基礎目錄配置:

 目錄說明:

  src/main/java後臺程式碼

  resources:配置檔案

  webapp

    static:靜態資原始檔

    WEB-INF:

      view:存放jsp檔案

      web.xml:配置Servlet以及Filter

    index.jsp: 預設主頁

二、SpringMVC配置檔案概述

注1:web.xml檔案是web專案中總的配置檔案,在web.xml中會說明springMVC的配置檔案位置和spring配置檔案的位置

最基礎的配置檔案:web.xml

<?xml version="1.0" encoding="UTF-8"
?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--定義站臺名稱-->
<display-name>Archetype Created Web Application</
display-name>
<!--welcome pages-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!---->
<!-- Spring MVC配置 -->
<!-- ====================================== -->
<!--配置springmvc DispatcherServlet
-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--配置springMVC-servlet.xml作為mvc的配置檔案(自定義servlet.xml配置檔案的位置和名稱)-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springMVC-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Spring配置 -->
<!-- ====================================== -->
<!--把spring-mybatis.xml加入到配置檔案中(指定Spring Bean的配置檔案所在目錄)-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mybatis.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

注2:SpringMVC是一個基於DispatcherServlet的MVC框架,每一個請求最先訪問的都是DispatcherServlet,DispatcherServlet負責轉發每一個Request請求給相應的Handler,Handler處理以後再返回相應的檢視(View)和模型(Model),返回的檢視和模型都可以不指定,即可以只返回Model或只返回View或都不返回。

DispatcherServlet是繼承自HttpServlet的,既然SpringMVC是基於DispatcherServlet的,那麼我們先來配置一下DispatcherServlet,好讓它能夠管理我們希望它管理的內容。HttpServlet是在web.xml檔案中宣告的。

說明1:

 servlet

  servlet-class:配置DispatcherServlet,

  init-param

    param-value:這是springMVC配置檔案的位置

  load-on-startup:表示servlet被載入的先後順序,值越小,優先順序越高。若是負整數或者沒設定,那就是容器會當Servlet被請求時再載入;若是正整數或0,容器在應用啟動時就載入並初始化這個servlet。

  async-supported:servlet 3.0後推出的新特性,作用是支援非同步處理

說明2:

 context-param:

  param-name: contextConfigLocation(表示這是spring的配置檔案)

  param-value: spring配置檔案的位置

  listenner: 監聽器

三、web專案啟動過程

1、啟動一個WEB專案的時候,WEB容器會去讀取它的配置檔案web.xml,讀取<listener>和<context-param>兩個結點。

2、緊急著,容建立一個ServletContext(servlet上下文),這個web專案的所有部分都將共享這個上下文。

3、容器將<context-param>轉換為鍵值對,並交給servletContext。

4、容器建立<listener>中的類例項,建立監聽器。

(2)Load-on-startup指定servlet被載入的順序

(3)載入順序:ServletContext->context-param ->listener -> filter -> servlet

四、web.xml詳解

1、web應用圖示:指出IDE和GUI工具用來表示Web應用的大圖示和小圖示

<icon>
<small-icon>/images/rocket.png</small-icon>
<large-icon>/images/space.png</large-icon>
</icon>

2、web應用名稱:提供GUI工具可能會用來標記這個特定的web應用的一個名稱

<display-name>spaceRocket</display-name>    

3、web應用描述:給出與此相關的說明性文字

<disciption>spaceRocket servlets and JSP pages.</disciption>    

4、上下文引數:宣告應用範圍內的初始化引數

<context-param>
     <param-name>ContextParameter</para-name>
<param-value>test</param-value>
<description>It is a test parameter.</description>
</context-param>

在servlet裡面可以通過getServletContext().getInitParameter("context/param")得到

5、過濾器配置:將一個名字與一個實現javaxs.servlet.Filter的類相關聯

    <filter>
<filter-name>setCharacterEncoding</filter-name>
<filter-class>com.blueice.setCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

6、監聽器配置

<listener>
<listerner-class>listener.SessionListener</listener-class>
</listener>

7、servlet配置

基本配置

<servlet>
<servlet-name>snoop</servlet-name>
<servlet-class>SnoopServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>snoop</servlet-name>
<url-pattern>/snoop</url-pattern>
</servlet-mapping>

高階配置

<servlet>
<servlet-name>snoop</servlet-name>
<servlet-class>SnoopServlet</servlet-class>
<init-param>
<param-name>foo</param-name>
<param-value>bar</param-value>
</init-param>
<run-as>
<description>Security role for anonymous access</description>
<role-name>tomcat</role-name>
</run-as>
</servlet>
<servlet-mapping>
<servlet-name>snoop</servlet-name>
<url-pattern>/snoop</url-pattern>
</servlet-mapping>

元素說明

 <servlet></servlet> 用來宣告一個servlet的資料,主要有以下子元素:          

  <servlet-name></servlet-name> 指定servlet的名稱          

  <servlet-class></servlet-class> 指定servlet的類名稱

  <jsp-file></jsp-file> 指定web站臺中的某個JSP網頁的完整路徑

  <init-param></init-param> 用來定義引數,可有多個init-param。在servlet類中通過getInitParamenter(String name)方法訪問初始化引數          

  <load-on-startup></load-on-startup>指定當Web應用啟動時,裝載Servlet的次序。                                      

    當值為正數或零時:Servlet容器先載入數值小的servlet,再依次載入其他數值大的servlet.                                      

    當值為負或未定義:Servlet容器將在Web客戶首次訪問這個servlet時載入它 
<servlet-mapping></servlet-mapping> 用來定義servlet所對應的URL,包含兩個子元素   <servlet-name></servlet-name> 指定servlet的名稱   <url-pattern></url-pattern> 指定servlet所對應的URL

8、會話超時配置

<session-config>
<session-timeout>120</session-timeout>
</session-config>

9、MIME型別配置

<mime-mapping>
<extension>htm</extension>
<mime-type>text/html</mime-type>
</mime-mapping>

10、指定歡迎頁配置

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>

11、配置錯誤頁面

通過錯誤碼來配置error-page

<error-page>
<error-code>404</error-code>
<location>/NotFound.jsp</location>
</error-page>

通過異常的型別來配置error-page

<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/error.jsp</location>
</error-page>

12、TLD配置

<taglib>
<taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
<taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>
</taglib>
如果MyEclipse一直在報錯,應該把<taglib> 放到 <jsp-config>
<jsp-config>
<taglib>
<taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
<taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>
</taglib>
</jsp-config>

13、資源管理物件配置

<resource-env-ref>
<resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
</resource-env-ref>

14、資源工廠配置

<resource-ref>
<res-ref-name>mail/Session</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>

配置資料庫連線池就可在此配置

<resource-ref>
<description>JNDI JDBC DataSource of shop</description>
<res-ref-name>jdbc/sample_db</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

15、安全限制配置

<security-constraint>
<display-name>Example Security Constraint</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/jsp/security/protected/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>tomcat</role-name>
<role-name>role1</role-name>
</auth-constraint>
</security-constraint>

16、登入驗證配置

<login-config>
<auth-method>FORM</auth-method>
<realm-name>Example-Based Authentiation Area</realm-name>
<form-login-config>
<form-login-page>/jsp/security/protected/login.jsp</form-login-page>
<form-error-page>/jsp/security/protected/error.jsp</form-error-page>
</form-login-config>
</login-config>

17、安全形色

security-role元素給出安全形色的一個列表,這些角色將出現在servlet元素內的security-role-ref元素的role-name子元素中。

分別的宣告角色可是高階IDE處理安全資訊更為容易。

<security-role>
<role-name>tomcat</role-name>
</security-role>

18、web環境引數

env-entry元素宣告web應用的環境項

<env-entry>
<env-entry-name>minExemptions</env-entry-name>
<env-entry-value>1</env-entry-value>
<env-entry-type>java.lang.Integer</env-entry-type>
</env-entry>

19、EJB宣告

<ejb-ref>
<description>Example EJB reference</decription>
<ejb-ref-name>ejb/Account</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>com.mycompany.mypackage.AccountHome</home>
<remote>com.mycompany.mypackage.Account</remote>
</ejb-ref>

20、本地EJB宣告

<ejb-local-ref>
<description>Example Loacal EJB reference</decription>
<ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>
<local>com.mycompany.mypackage.ProcessOrder</local>
</ejb-local-ref>

21、配置DWR

<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

22、配置Struts

   <display-name>Struts Blank Application</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>application</param-name>
<param-value>ApplicationResources</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>struts-bean</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-html</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-nested</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-logic</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
</taglib>

23、配置Spring(基本上都是在Struts中配置的)

   <!-- 指定spring配置檔案位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!--載入多個spring配置檔案 -->
/WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml
</param-value>
</context-param>
<!-- 定義SPRING監聽器,載入spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>

參考文件1:https://blog.csdn.net/yangshuaionline/article/details/90476048

參考文件2:https://www.cnblogs.com/superjt/p/3309255.html

參考文件3:https://www.cnblogs.com/shamo89/p/9942291.html

參考文件4:https://developer.ibm.com/zh/articles/j-lo-servlet30/