SSM教程 (三) spring

完整项目见  https://gitee.com/anti-murphy/demo_ssm

第一步:

 在spring文件夹下新建如图三个文件

<?xml version="1.0" encoding="UTF-8"?>
<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"
       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-3.0.xsd">



    <!--扫描dao-->
    <context:component-scan base-package="cn.xyz.dao"/>


</beans>
View Code
<?xml version="1.0" encoding="UTF-8"?>
<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"
       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-3.0.xsd">




    <!--扫描controller-->
    <context:component-scan base-package="cn.xyz.controller"/>

    <mvc:interceptors>

        <mvc:interceptor>
            <mvc:mapping path="/admin/**"/>
            <bean class="cn.xyz.interceptor.AdminInterceptor"></bean>
        </mvc:interceptor>

        <mvc:interceptor>

            <mvc:mapping path="/user/**"/>
            <bean class="cn.xyz.interceptor.UserInterceptor"></bean>
        </mvc:interceptor>

    </mvc:interceptors>




    <!-- 视图解析器 -->
    <bean id="templateResolver"
          class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML" />
        <property name="cacheable" value="true" />
        <property name="characterEncoding" value="UTF-8" />
    </bean>

    <bean id="templateEngine"
          class="org.thymeleaf.spring5.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
        <property name="enableSpringELCompiler" value="true" />
    </bean>

    <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine"/>
        <property name="characterEncoding" value="UTF-8" />
    </bean>
    



    <!--配置静态资源必须加这句-->
    <mvc:annotation-driven/>





    <!--静态资源配置-->
    <mvc:resources location="/assets/" mapping="/assets/**"/>
    <mvc:resources location="/sass/" mapping="/sass/**"/>
    <mvc:resources location="/templates/" mapping="/templates/**"/>

</beans>
View Code
<?xml version="1.0" encoding="UTF-8"?>
<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"
       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-3.0.xsd">



    <!--扫描dao-->
    <context:component-scan base-package="cn.xyz.service"/>



</beans>
View Code

第二步:在 controller 类名前加@Controller

    在 service 前加@Service

    entity前加 @Component

猜你喜欢

转载自www.cnblogs.com/Lorentz-Z/p/12274390.html