Java教程

Java-web配置

本文主要是介绍Java-web配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Java-web配置

  • 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_4_0.xsd"
             version="4.0">
        <!-- 注册DispatcherServlet -->
        <servlet>
            <servlet-name>app1</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>
            <!-- 启动级别 -->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>app1</servlet-name>
            <url-pattern>/</url-pattern><!-- / 不包括.jsp, /* 包括.jsp -->
        </servlet-mapping>
        
        <!-- 注册乱码过滤器 -->
        <filter>
            <filter-name>encoding</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>
        </filter>
        <filter-mapping>
            <filter-name>encoding</filter-name>
            <!-- 务必带“*”,否则过滤不了 -->
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    </web-app>
    
这篇关于Java-web配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!