SpringMVC

1.介绍

MVC思想:专事专干
SpringMVC:spring对MVC思想的实现(请求:Controller;视图:视图引擎;数据:Model)

2.前端控制器

统一的入口,并将请求进行分发

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
         version="3.0">
    <!-- Spring MVC 前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定 Spring 容器启动加载的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mvc.xml</param-value>
        </init-param>
        <!-- Tomcat 启动初始化 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--    字符编码-->
    <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>


mvc.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"
       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.xsd">

    <!--    IoC,DI注解解析器   只有Controller版式便签才能够创建对象-->
    <context:component-scan base-package="cn.wolfcode.web"/>

    <!--    JSON注解-->
    <mvc:annotation-driven/>

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

    <!--    引入配置文件-->
    <import resource="classpath:applicationContext.xml"/>

</beans>

3.前端控制器路径

/*.do,不符合RESTful风格

/

/*

4.处理响应

4.1.HTML

@RequestMapping("/list")
    public String list(Model model, QueryObject queryObject){
        PageResult<Department> pageResult = departmentService.query(queryObject);
        model.addAttribute("pageResult",pageResult);
        return "department/list";
        //List<Department> departments=departmentService.listAll();
        //model.addAttribute("departments",departments);
        //return "department/list";
    }

mvc.xml配置便利寻找视图

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
</bean>

4.2.转发和重定向

地址栏 WEB-INF 共享请求路径 表单重复提交
转发 不变 可以访问 共享
定向 不可以访问 不共享

注意

4.3.处理时间参数

/** 预约时间*/
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date appointmentTime;

4.4.文件上传

web.xml限制大小

<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:mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <multipart-config>
            <max-file-size>52428800</max-file-size>
            <max-request-size>52428800</max-request-size>
        </multipart-config>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--    字符编码-->
    <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

mvc.xml

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--最大上传文件大小 10M -->
        <property name="maxUploadSize" value="#{1024*1024*10}"/>
    </bean>

4.5.拦截器

需要实现HandlerInterceptor

public class CheckLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //if(request.getSession().getAttribute("EMPLOYEE_IN_SESSION")!=null){
        //    return true;
        //}

        if(UserContext.getEmployee()!=null){
            return true;
        }

        response.sendRedirect("/static/login.html");
        return false;
    }
}

mvc.xml

<mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/static/**"/>
            <mvc:exclude-mapping path="/login**"/>
            <mvc:exclude-mapping path="/index/**"/>
            <mvc:exclude-mapping path="/list"/>
            <mvc:exclude-mapping path="/save"/>
            <mvc:exclude-mapping path="/listItem**"/>
            <mvc:exclude-mapping path="/systemDictionary/queryById"/>
            <bean class="cn.wolfcode.web.interceptor.CheckLoginInterceptor"></bean>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/static/**"/>
            <mvc:exclude-mapping path="/login**"/>
            <mvc:exclude-mapping path="/index/**"/>
            <mvc:exclude-mapping path="/list"/>
            <mvc:exclude-mapping path="/save"/>
            <mvc:exclude-mapping path="/listItem**"/>
            <mvc:exclude-mapping path="/systemDictionary/queryById"/>
            <bean class="cn.wolfcode.web.interceptor.CheckPermissionInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>


返回课程体系

posted @ 2021-05-05 11:04  LinkYup  阅读(53)  评论(0编辑  收藏  举报