zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    android 2.3 bug android 4.x bug
    设计高性能CSS3动画的几个要素
    开启硬件加速 解决页面闪白 保证动画流畅
    取消input在ios下,输入的时候英文首字母的默认大写 android 上去掉语音输入按钮
    JS中的类型检测
    CSS的一点知识
    HTML Canvas
    HTML DOM Table 对象
    只是有所了解的语言
    骑士巡游问题
  • 原文地址:https://www.cnblogs.com/steveshao/p/13492333.html
Copyright © 2011-2022 走看看