实训第四天之ssh之web.xml

来源:互联网 发布:java 射击游戏 编辑:程序博客网 时间:2024/05/16 06:51

首先上一个web.xml文件源码:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>OKAir</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>  <filter>    <filter-name>characterEncodingFilter</filter-name>    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    <init-param>      <param-name>encoding</param-name>      <param-value>UTF-8</param-value>    </init-param>    <init-param>      <param-name>forceEncoding</param-name>      <param-value>true</param-value>    </init-param>  </filter>  <filter-mapping>    <filter-name>characterEncodingFilter</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <listener>    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>  </listener>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext-*.xml</param-value>  </context-param>  <servlet>    <servlet-name>springmvc-servlet</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:springmvc-servlet.xml</param-value>    </init-param>  </servlet>  <servlet-mapping>    <servlet-name>springmvc-servlet</servlet-name>    <url-pattern>*.action</url-pattern>  </servlet-mapping></web-app>

对web文件中每个模块的单独理解
1、设置默认界面
< welcome-file-list>和< welcome-file>
设置了初始界面,当输入根路径的时候,会自动匹配列表中的文件显示

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

2、配置编码过滤器:
springmvc有内置的编码过滤器:org.springframework.web.filter.CharacterEncodingFilter;
通过它可以实现对编码集的自动设置
使用< init-param>可以设置类中的属性,在本过滤器中使用
< param-name>encoding
< param-value>UTF-8< /param-value>
设置了编码为utf-8
使用
< init-param>
< param-name>forceEncoding< /param-name>
< param-value>true< /param-value>
< /init-param>
配置了无视原来编码,转换为utf-8

 <filter>    <filter-name>characterEncodingFilter</filter-name>    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    <init-param>      <param-name>encoding</param-name>      <param-value>UTF-8</param-value>    </init-param>    <init-param>      <param-name>forceEncoding</param-name>      <param-value>true</param-value>    </init-param>  </filter>  <filter-mapping>    <filter-name>characterEncodingFilter</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>

3、使用:
< filter-mapping>
< filter-name>characterEncodingFilter< /filter-name>
< url-pattern>/*< /url-pattern>
< /filter-mapping>
配置了使用编码过滤器的路径。此处填的为/* 意思为所有路径
进行了此配置之后会对所有的路径调用编码过滤器 改变其编码格式

    <filter>        <filter-name>characterEncodingFilter</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>UTF-8</param-value>        </init-param>        <init-param>            <param-name>forceEncoding</param-name>            <param-value>true</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>characterEncodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>

4、配置使用监听器:
Spring提供ServletContentListener的一个实现类ContextLoaderListener监听器,该类可以作为Listener使用,在启动Tomcat容器的时候,该类的作用就是自动装载ApplicationContext的配置信息,如果没有设置contextConfigLocation的初始参数则会使用默认参数WEB-INF路径下的application.xml文件。如果需要自定义读取多个配置文件或者修改默认路径,则可以在web.xml进行配置

< param-value>classpath:applicationContext-*.xml< /param-value>
加载在classpath下的所有以classpath:applicationContext-开头的.xml文件

    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationContext-*.xml</param-value>    </context-param>

5、配置spring
声明了一个为springmvc-servlet的servlet 指向springmvc的核心类并且在servlet-mapping中进行使用,配置;当访问为.action的后缀的路径时,会交给DispatcherServlet进行处理。
< init-param>标签 配置了spring的配置文件DispatcherServlet

    <servlet>        <servlet-name>springmvc-servlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:springmvc-servlet.xml</param-value>        </init-param>    </servlet>    <servlet-mapping>        <servlet-name>springmvc-servlet</servlet-name>        <url-pattern>*.action</url-pattern>    </servlet-mapping>

注意:
1、WEB-INF中的文件不能通过路径文件直接访问,需要通过响应才能访问。
2、注意自己配置的 < url-pattern>*.action< /url-pattern>的链接的地址
3、在访问用@RequestMapping(“”)修饰的方法时候,注意要在方法路径前加上类的@RequestMapping(“”)的路径,两者是嵌套关系。
4、导入项目后出现tomcat错误。:
Description Resource Path Location Type Target runtime Apache Tomcat v8.0 is not defined. OKAir Unknown Faceted Project Problem
解决方案:
打开项目.setting/org.eclipse.wst.common.project.facet.core
将其< runtime name=”Apache Tomcat v8.5”/>改为与当前相匹配的版本即可

原创粉丝点击