配置使用Mvc

  1. 云栖社区>
  2. 博客>
  3. 正文

配置使用Mvc

死瘦宅 2019-10-13 14:09:46 浏览143
展开阅读全文

web.xml配置此配置在Web-INF下 意思是此文件拒接外部访问


<web-app
        version="3.0"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!-- 设置编码参是UTF8 -->
        <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>
    <!--上面解决post请求中文乱码问题-->

    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:Application.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

2、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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:annotation-config/> <!--开启注解模式-->
    <context:component-scan base-package="com.sun"/> <!--管理对象路径-->
    <mvc:annotation-driven/> <!--开启MVC注解模式-->
    <mvc:default-servlet-handler/>


</beans>

ssm配置信息

    <context:component-scan base-package="com.sun.sunbao"/> <!--管理对象路径-->
    <mvc:annotation-driven/> <!--开启MVC注解模式-->
    <!--寻找静态资源、不加的话就会当Avtion拦截掉-->
    <!--优先级低 所有的类都没找见他再去找-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>

    <!--返回json配置-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--上传文件配置信息-->
    <!--multipartResolver id一定要写这个 不能写错因为他要找这个-->
    <bean id="multipartResolver"
          p:defaultEncoding="UTF-8"
          p:maxUploadSize="10485760"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
    ></bean>
    <!--视图解析器配置前后缀-->
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--静态资源配置-->
    <!--<mvc:resources mapping="/css/**" location="/WEB-INF/css/" />-->
    <!--事务管理员-->
    <tx:annotation-driven  transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--数据库链接-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/school?useUnicode=true&amp;
                characterEncoding=UTF-8&amp;useSSL=false"/>
        <property name="user" value="root"/>
        <property name="password" value="Sb1996350."/>
        <property name="maxPoolSize" value="3"/>
        <property name="minPoolSize" value="1"/>
        <property name="initialPoolSize" value="1"/>
    </bean>

    <!--创建mybatis对象工厂sqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--数据源对象 将dataSource对象放入、属性dataSource中-->
        <!--相应的配置找里面的属性进行配置即可-->
        <!--mybatis配置的xml路径-->
        <property name="mapperLocations" value="classpath:com/sun/sunbao/Dao/*.xml"></property>
    </bean>

    <!--扫描接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--扫描mapper映射接口-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">
            <!--value通过名字来查找sqlSessionFactory对象-->
        </property>
        <property name="basePackage" value="com.sun.sunbao.Dao"/>
    </bean>

网友评论

登录后评论
0/500
评论
死瘦宅
+ 关注