JavaWeb:web层依赖

web层我们使用的是springMVC框架,所以我们需要引入 springMVC 和 spring-web 相关依赖,还有servlet,jackson 等依赖;首先一样需要依赖 service 层
JavaWeb:web层依赖_第1张图片

  • pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>###项目名称###artifactId>
        <groupId>### groupId###groupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <artifactId>###web模块名称 ###artifactId>
    <packaging>warpackaging>

    <dependencies>

        
        <dependency>
            <groupId>### groudId###groupId>
            <artifactId>### 依赖的service层名称###artifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring.version}version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.9.7version>
        dependency>

    dependencies>

    <build>
        <resources>
            
            <resource>
                <directory>src/main/webapp/WEB-INF/configdirectory>
                <includes>
                    <include>**/*.xmlinclude>
                    <include>**/*.propertiesinclude>
                includes>
                <filtering>falsefiltering>
            resource>
        resources>
    build>
project>
  • spring-mvc文件配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"
>

    
    <context:component-scan base-package="cn.edu.zzti.cs.controller.*" />

    
    <mvc:annotation-driven/>

    
    <bean id="htmlViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/html/"/>
        <property name="suffix" value=".html"/>
        <property name="contentType" value="text/html;charset=utf-8"/>
    bean>

    
    <mvc:resources mapping="/html/**"
                   location="/WEB-INF/pages/html/" />
    <mvc:resources mapping="/js/**"
                   location="/WEB-INF/resources/js/" />
    <mvc:resources mapping="/css/**"
                   location="/WEB-INF/resources/css/" />
    <mvc:resources mapping="/images/**"
                   location="/WEB-INF/resources/images/" />
    <mvc:resources mapping="/json/**"
                   location="/WEB-INF/resources/json/" />
    <mvc:resources mapping="/lib/**"
                   location="/WEB-INF/resources/lib/" />

    
    
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/**/*.js"/>
            <mvc:exclude-mapping path="/**/*.css"/>
            <mvc:exclude-mapping path="/**/*.png"/>
            <mvc:exclude-mapping path="/html/login.html"/>
            <mvc:exclude-mapping path="/login/submit.action"/>
            
            <bean class="cn.edu.zzti.cs.interceptor.LoginInterceptor"/>
        mvc:interceptor>

    mvc:interceptors>


beans>

  • web.xml文件配置

<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_3_0.xsd"
         id="WebApp_ID" version="3.0">
    <display-name>ssm-demodisplay-name>
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath*:/spring/applicationContext-*.xmlparam-value>
    context-param>

    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <filter>
        <filter-name>encodingfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            
            <param-value>trueparam-value>
        init-param>
    filter>

    
    <filter-mapping>
        <filter-name>encodingfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    
    <servlet>
        <servlet-name>DispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:/spring/spring-mvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    
    <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        <url-pattern>*.actionurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        <url-pattern>*.htmlurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        <url-pattern>*.cssurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        <url-pattern>*.jsonurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        <url-pattern>*.jsonpurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        <url-pattern>*.jsurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        <url-pattern>*.pngurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        <url-pattern>*.jpgurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        <url-pattern>/resources/*url-pattern>
    servlet-mapping>

    <welcome-file-list>
        <welcome-file>/html/login.htmlwelcome-file>
    welcome-file-list>

    
    <filter>
        <filter-name>HiddenHttpMethodFilterfilter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
    filter>

    <filter-mapping>
        <filter-name>HiddenHttpMethodFilterfilter-name>
        <servlet-name>DispatcherServletservlet-name>
    filter-mapping>

    <session-config>
        
        <session-timeout>30session-timeout>
    session-config>

web-app>
  • spring应用上下文
    JavaWeb:web层依赖_第2张图片

你可能感兴趣的:(JavaEEstudy)