1引言:这里主要做三件事

  1.1resources文件夹下创建spring-mvc.xml并配置:开启注解驱动(mvc:annotation-driven),静态资源过滤(mvc:default-servlet-handler),视图解析器(InternalResourceViewResolver),扫描web相关的bean,既扫描controller包下的东西(context:component-scan base-package="xxx")

  1.2配置resources文件夹下的applicationContext.xml,这里将spring-dao.xml,spring-service.xml,spring-mvc.xml进行整合

  1.3配置web/WEB-INF文件夹下的web.xml文件:servlet(DispatchServlet)和对应的servlet-mapping,乱码过滤(CharacterEncodingFilter)

2步骤:

  2.1在resources文件夹新建spring-mvc.xml问价,代码如下:其中像注解驱动,静态资源过滤,视图解析器这些都是固定的,在视图解析器中prefix代表路径的前缀,suffix代表路径的后缀,假设controller层返回了一个字符串为“book”(注意:这个controller不是@RestController而是@Controller,如果是@RestController最后返回字符串的话界面仅仅会显示你返回的字符串,@Controller类型的controller层才会对返回的字符串通过视图解析器进行路径拼接)并且使用的是@Controller注解,那么通过下方代码中的视图解析器会将字符串拼接为/WEB-INF/jsp/book.jsp

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

    <!--注解驱动-->
    <mvc:annotation-driven/>

    <!--静态资源过滤-->
    <mvc:default-servlet-handler/>

    <!--扫描controller包-->
    <context:component-scan base-package="com.xiaoma1.controller"/>

    <!--视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

  2.2在创建mybatis层的时候我记得在resources文件夹下已经新建过applicationContext.xml了,当时是空的,啥都没有,现在代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="spring-dao.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="spring-mvc.xml"/>
</beans>

  2.3配置web.xml,这里要注意,servlet标签中的init-param标签里边的value一定要是applicationContext.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">

    <!--配置DispatchServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--配置乱码过滤-->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--session过期时间-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
</web-app>

至此底层配置结束,下面就编写controller和相应的jsp或者html界面就好了,下一节通过controller编写的接口实现具体的增删改查功能

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/XiaoMaGuai/p/16280032.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!