学习web.xml配置
web.xml用于初始化工程信息,其格式符合对应的schema文件,模板有sun公司定义。主要内容在<web-app>中,并有此元素配置格式版本信息。
各个标签如下所示:
1. Welcome-file-list 欢迎列表,项目启动后默认显示的页面。若第一个不存在则显示第二个,以此类推。
<welcome-file-list>web/index.html</welcome-file-list>
<welcome-file-list>web/login.html</welcome-file-list>
如上,file1不存在则显示file2
2. Error-page 错误页显示,可通过错误码或异常类指定错误页面。
<error-page>
<error-code>404</error-code>
<location>/WEB_INF/error.jsp</location>
</error-page>
<error-page>
<error-exception>java.lang.Exception</error-exception>
<location>/WEB_INF/error.jsp</location>
</error-page>
3. filter过滤器
Filter是一个过滤器标签,可用于过滤编码,示范及机构如下:
<filter>
<filter-name>encodingFilter</ filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</initparam>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-param>/*</url-param>
</filter-mapping>
每个filter对应一个mapping,mapping中的过滤器要和filter中的对应。
例如struts2就是一个过滤器
<filter>
<filter-name>struts2</ filter-name>
<filter-class>org.apache.struts2.dispatcher.FileDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-param>/*</url-param>
</filter-mapping>
4. Listener监听器
用于实例化Spring容器的就是一个监听器。<listener/>标签中只有一个标签,listener-class。要和<context-param/>一起使用。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*.xml</param-value>
</context-param>
另外,记录日志类也可加载在监听器中,结构类似。
5. Servlet
最早的struts1就是用servlet加载的,Spring-mvc也是用servlet加载的。其结构和fiter类似。
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/admin/*</url-pattern>
</servlet-mapping>