JavaWeb配置详解(结合框架SpringMVC)

详解

先说一说常识性的东西,我们的JavaWeb程序运行一开始走的是web.xml文件,这是我们的核心文件,可以说没有web.xml文件我们就无法运行项目,这个文件长什么样子,读者自己新建一个web项目就行了。

web.xml

  • 项目名

  • 正常情况下我们会配置一些过滤器 filter,过滤器的意思就是在执行某件事件前先进行规范处理之后才能继续运行。我们常见的过滤器有编码过滤器、数据安全过滤器。下面贴一个公用的编码过滤器

    
    <filter>
        <filter-name>CharacterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
    filter>
    
    <filter-mapping>
        <filter-name>CharacterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
  • 下面就是设置一些浏览器的读取问题,我们用浏览器打开文件的时候会发现如果是doc文件, 会用记事本打开,出现乱码, 但是如果在web.xml里面配置了类型,浏览器就会根据配置的类型自动调用相应的程序打开
    在 WEB.XML 配置常用文件类型

    <mime-mapping>  
    <extension>docextension>  
    <mime-type>application/vnd.ms-wordmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>gifextension>  
    <mime-type>image/gifmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>htmextension>  
    <mime-type>text/htmlmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>htmlextension>  
    <mime-type>text/htmlmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>jnlpextension>  
    <mime-type>application/x-java-jnlp-filemime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>jpegextension>  
    <mime-type>image/jpegmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>jpgextension>  
    <mime-type>image/jpegmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>jsextension>  
    <mime-type>application/x-javascriptmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>pdfextension>  
    <mime-type>application/pdfmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>pngextension>  
    <mime-type>image/pngmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>rarextension>  
    <mime-type>application/x-rar-compressedmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>txtextension>  
    <mime-type>text/plainmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>xlsextension>  
    <mime-type>application/vnd.ms-excelmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>xmlextension>  
    <mime-type>text/xmlmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>xulextension>  
    <mime-type>application/vnd.mozilla.xul-xmlmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>zhtmlextension>  
    <mime-type>text/htmlmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>zipextension>  
    <mime-type>application/x-zipmime-type>  
    mime-mapping>  
    <mime-mapping>  
    <extension>zulextension>  
    <mime-type>text/htmlmime-type>  
    mime-mapping>
  • 这些事辅助配置,这些配置完了,下面我们就是配置框架的事情了,框架说白了就是servlet,所以我们就需要引用servlet来进行配置框架。 我们通过context-param 去加载spring配置文件




    contextConfigLocation
    classpath:applicationContext.xml



    org.springframework.web.context.ContextLoaderListener

  • 加载配置文件后我们就剩下去启动框架了

    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:config/springmvc.xmlparam-value>
        init-param>
        
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>*.actionurl-pattern>
    servlet-mapping>
  • 整个框架就配置好了,在web.xml文件中我们在配置一些错误界面,就会让整个系统变得更加的人性化。

    • 根据错误编码跳转
<error-page>  
       <error-code>403error-code>  
       <location>/change/error_go.actionlocation>  
error-page>
-  根据错误类型跳转      
  
        <error-page>   
            <exception-type>java.lang.Exceptionexception-type>   
            <location>/change/error_go.actionlocation>   
        error-page>
  • 最后就是系统的欢迎页
file-list>
        file>change/login_go.actionfile>
    file-list>

application.xml配置文件

  • 启动注解配置


    <context:component-scan base-package="tm.change">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    context:component-scan>
  • 配置数据源

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${c3p0.driverClass}">property>
        <property name="jdbcUrl" value="${c3p0.url}">property>
        <property name="user" value="${c3p0.user}">property>
        <property name="password" value="${c3p0.password}">property>

    bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource" />
        
        <property name="configLocation" value="classpath:config/mybatis-config.xml" />
    bean>

当然用之前需要引入数据库的配置


这个说白了就是properties文件,里面放置了数据连接的信息

  • 然后就要处理mybatis的mapper接口(可以看成是dao层的处理)

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="tm.change.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    bean>
  • 事务处理(可以看成是service层)

    • 注册一个事务
    
        <bean id="myTxManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        bean>
    • 配置事务的通知(指定哪些方法会有事务管理)
    
    <tx:advice id="txAdvice" transaction-manager="myTxManager">
        <tx:attributes>
            
            <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" />
            <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" />
            <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" />
            <tx:method name="*" propagation="SUPPORTS" read-only="true" />
        tx:attributes>
    tx:advice>
    • 切入点(在哪个里面具有事务)
    
    <aop:config>
        <aop:pointcut expression="execution(* tm.change.service..*(..))"
            id="txPointcut" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
    aop:config>
    <context:annotation-config />

上述的事务是在service层上insert*,update*,delete*等这些方法具有事务

  • controller层(springmvc.xml文件)

    • 配置视图解析器
    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        <property name="prefix" value="/WEB-INF/" />
        
        <property name="suffix" value=".jsp" />
    bean>
    • 设置上传文件的视图解析器
    
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="20971520" />
    bean>
    • 配置拦截器,可以直接定义拦截所有请求,也可以自定义拦截路径
    <mvc:interceptors>
    
    <bean class="com.wxisme.ssm.interceptor.IdentityInterceptor">bean>
        
    mvc:interceptors>
    • 配置一些绑定如日期邦迪
    
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
     <property name="converters">
         <list>
             
             <bean class="com.wxisme.ssm.controller.converter.DateConverter"/>
         list>
     property>
    bean>
    • 静态资源不加载问题的解决方法:

    在web.xml文件中如何启动spring是设置拦截的url是如下的,那么只有以.action的请求会被拦截处理,一些静态资源不会被拦截,这种情况是正常的,但是有的人会设置springmvc的拦截地址是拦截任何地址,那么这个时候的静态资源也就会被拦截。这个时候我们系统就会因加载不到静态资源而无法显示界面

    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>*.actionurl-pattern>
    servlet-mapping>

    遇到上述情况难道我们就没办法解决了吗,有的,看下代码

    
    
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/css/**" location="/css/" />  
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/imgdata/**" location="/imgdata/" />

转载于:https://www.cnblogs.com/zhangxinhua/p/8319238.html

你可能感兴趣的:(JavaWeb配置详解(结合框架SpringMVC))