SpringMVC

SpringMVC

MVC架构

Controller

控制层

  • 获取前端信息
  • 调用业务逻辑
  • 跳转页面

Model

模型层

  • 业务逻辑
  • 保存数据的状态

View

显示页面

依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>SpringMVC</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

</project>

优点

  • 轻量级 简单易学
  • 高效、基于请求响应的MVC框架
  • 兼容Spring
  • 约定优于配置
  • 功能强大:RESTful(用/而不是?传参)、数据验证、格式化、本地化、主题
  • 简介灵活

SpringMVC入门

配置

web.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:Spring的核心 请求分发器(前端控制器)-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--绑定Spring的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--启动级别:1-->
        <load-on-startup>1</load-on-startup>

    </servlet>

    <!-- / 只匹配所有请求,不去匹配jsp 一般用这个-->
    <!--/* 会匹配所有的请求,包括jsp-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

springmvc-servlet.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">


    <!--处理器映射器-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!--处理器适配器-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!--视图解析器 模板引擎 Thymeleaf Freemarker-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--前缀 注意 jsp后面那个/不可省略-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--配置url-->
    <bean id="/hello" class="com.rush.controller.HelloController"/>
</beans>

Controller

package com.rush.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author:zmc
 * @function:
 * @date: 2020/8/4 11:04
 */
public class HelloController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView mv = new ModelAndView();

        String result = "HelloSpringMVC";
        mv.addObject("msg",result);
        mv.setViewName("test");
        return mv;
    }
}

记得要导入所有要用的jar包

image-20200804113834626

注解开发

Controller

package com.rush.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author:zmc
 * @function:
 * @date: 2020/8/4 12:49
 */
@Controller
public class HelloController {
    //URL
    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("msg","HelloSpringMVC");
        //要跳转的页面
        return "hello";
    }
}

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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

        <!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理-->
        <context:component-scan base-package="com.rush.controller"/>
        <!--让Spring MVC不处理静态资源-->
        <mvc:default-servlet-handler/>
        <!--支持mvc注解驱动-->
        <mvc:annotation-driven/>

    <!--视图解析器 模板引擎 Thymeleaf Freemarker-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--前缀 注意 jsp后面那个/不可省略-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

RESTful风格

​ RESTful风格是一个资源定位系统及资源操作的风格。不是标准也不是协议,只是一种风格。可以使软件更加简洁、更有层次感,更易于实现缓存等机制。

  • 原本:localhost:8080/method?username=…&…
  • RESTful:localhost:8080/method/username=…/…

传参

package com.rush.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author:zmc
 * @function:
 * @date: 2020/8/4 12:49
 */
@Controller
public class HelloController {
    //传参 限制方法为get方法
    @RequestMapping(value = "/hello/{a}/{b}",method = RequestMethod.GET)
    //或者使用 @GetMapping(value = "/hello/{a}/{b}")
    public String hello(@PathVariable int a,@PathVariable int b, Model model){
        model.addAttribute("msg","结果为:"+(a+b));
        return "hello";
    }
}

使用RESTful风格,同一个url可以使用不同的方式提交,实现url的复用,相对来说安全,隐藏了参数名(但抓包依然可以获取)

重定向和转发

//有视图解析器的情况下
//重定向
return "redirect:/index.jsp";
//转发 不填的话默认也为转发
return "forward:/index";

获取前端的数据

如果前端参数名和后台方法名不一致

public void hello2(@RequestParam("usernmae") String name){

    }

实体类

url:?name=rush&id=1

后台

@RequestMapping("/user")
public String user(User user){
    System.out.println(user);
    return hello
}

只要需要前端接收的数据 都要用@RequestParam注解

乱码

在web.xml下配置Spring的filter

   <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

JSON

Jackson

依赖

   <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.0</version>
        </dependency>
//编码改为UTF-8
@RequestMapping(value = "/j1",produces = "application/json;charset=utf-8")
    //有了下面这个注解 就不会走视图解析器 会直接返回前端一个字符串
		//等同于类的RestController 
    @ResponseBody
    public String json1() {
        ObjectMapper mapper = new ObjectMapper();
        String str = "";
        User user = new User("mcmc", 3, "男");
        try {
             str = mapper.writeValueAsString(user);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

每一个Json都要修改编码模式很麻烦

我们可以在xml文件全局配置

 <!--JSON乱码配置-->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8"/>
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                        <property name="failOnEmptyBeans" value="false"/>
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

获取时间

 @RequestMapping(value = "/j1")
    //有了下面这个注解 就不会走视图解析器 会直接返回前端一个字符串
    @ResponseBody
    public String json1() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return mapper.writeValueAsString(sdf.format(date));
    }

FastJson

依赖

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.41</version>
        </dependency>
@RequestMapping("j4")
    @ResponseBody
    public String json3() throws JsonProcessingException{
        List<User> userList = new ArrayList<>();
        User user = new User("rush1", 3, "男");
        User user1 = new User("rush2", 3, "男");
        User user2 = new User("rush3", 3, "男");
        User user3 = new User("rush4", 3, "男");
        userList.add(user);
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        return JSON.toJSONString(userList);
    }

JSON字符串转Java对象

User user = JSON.parseObject(str,User.class);

Java对象转JSON对象

JSONObject jsonObject = (JSONObject)JSON.toJSON(user);

JSON对象转JAVA对象

JSON.toJavaObject(jsonObject,User.class);

SSM整合

application.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-mvc.xml"/>
    <import resource="spring-dao.xml"/>
</beans>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
   <typeAliases>
       <package name="com.rush.po"/>
   </typeAliases>

    <mappers>
        <mapper class="com.rush.dao.BookMapper"/>
    </mappers>
</configuration>

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

>
 <!--用Spring的数据源代替Mybatis的配置-->
    <context:property-placeholder location="classpath:db.properties"/>
     <!--用Spring的数据源代替Mybatis的配置-->
         <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                 <property name="driverClass" value="${jdbc.driver}"/>
                 <property name="jdbcUrl" value="${jdbc.url}"/>
                 <property name="user" value="${jdbc.username}"/>
                 <property name="password" value="${jdbc.password}"/>

                <property name="maxPoolSize" value="30"/>
                <property name="minPoolSize" value="10"/>
                <!--关闭连接后不开启自动commit-->
                <property name="autoCommitOnClose" value="false"/>
                <!--获取连接超时时间-->
                <property name="checkoutTimeout" value="10000"/>
                <!--连接失败的重试次数-->
                <property name="acquireRetryAttempts" value="2"/>

             </bean>
         <!--配置原来Mybatis的sqlSessionFactory-->
             <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
                 <property name="dataSource" ref="dataSource"/>
                 <!--绑定Mybatis配置文件-->
                 <property name="configLocation" value="classpath:mybatis-config.xml"/>
             </bean>
            <!--配置dao接口扫描包-->
            <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
                <property name="basePackage" value="com.rush.dao"/>
            </bean>

            <!--扫描service包-->
            <context:component-scan base-package="com.rush.service"/>
            <bean id="BookServiceImpl" class="com.rush.service.BookServiceImpl">
                <property name="bookMapper" ref="bookMapper"/>
            </bean>


         <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
             <property name="dataSource" ref="dataSource"/>
         </bean>
         <tx:advice id="txAdvice" transaction-manager="transactionManager">
             <tx:attributes>
                 <tx:method name="add" propagation="REQUIRED"/>
                 <tx:method name="delete" propagation="REQUIRED"/>
                 <tx:method name="update" propagation="REQUIRED"/>
                 <tx:method name="query" read-only="true"/>
                 <tx:method name="*" propagation="REQUIRED"/>
             </tx:attributes>
         </tx:advice>
</beans>

spring-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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

        <!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理-->
        <context:component-scan base-package="com.rush.controller"/>
        <!--让Spring MVC不处理静态资源-->
        <mvc:default-servlet-handler/>
        <!--支持mvc注解驱动-->
        <mvc:annotation-driven/>



    <!--视图解析器 模板引擎 Thymeleaf Freemarker-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--前缀 注意 jsp后面那个/不可省略-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

拦截器

​ 拦截器只会拦截访问的控制器方法,如果访问的是jsp/css/js/image 拦截器不会拦截

 <mvc:interceptors>
      <mvc:interceptor>
          <!--**表示拦截所有子目录-->
          <mvc:mapping path="/**"/>
          <bean class="com.rush.config.MyInterceptor"/>
      </mvc:interceptor>
  </mvc:interceptors>
package com.rush.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author:zmc
 * @function:
 * @date: 2020/8/5 19:48
 */
public class MyInterceptor implements HandlerInterceptor {
    //true 执行下一个拦截器 放行
    //false 拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("-----------------处理前----------------");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("-----------------处理后----------------");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("-----------------清理----------------");
    }
}

文件上传

 <!--文件上传配置-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/>
        <property name="maxUploadSize" value="10485760"/>
        <property name="maxInMemorySize" value="40960"/>
    </bean>
package com.rush.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

/**
 * @author:zmc
 * @function:
 * @date: 2020/8/6 10:41
 */
@Controller
public class FileUploader {
        //@RequestParam("file") 将name=file控件得到的文件封装为CommonsMultipartFile对象
        //批量上传使用CommonsMultipartFile数组即可
        @RequestMapping("/upload")
        public String fileUpload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
            String fileName = file.getOriginalFilename();

           if("".equals(fileName) || fileName == null){
               return "redirect:/index.jsp";
           }

           String path = request.getSession().getServletContext().getRealPath("/upload");
           File realPath = new File(path);
           if(!realPath.exists()){
               realPath.mkdir();
           }
            InputStream is = file.getInputStream();
            OutputStream os = new FileOutputStream(new File(realPath,fileName));

            int len = 0;
            byte[] buffer = new byte[1024];
            while( (len = is.read(buffer)) !=-1){
                os.write(buffer,0,len);
                os.flush();
            }
            os.close();
            is.close();
            return "redirect:/index.jsp";
        }
}

package com.rush.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

/**
 * @author:zmc
 * @function:
 * @date: 2020/8/6 10:41
 */
@Controller
public class FileUploader {
        //@RequestParam("file") 将name=file控件得到的文件封装为CommonsMultipartFile对象
        //批量上传使用CommonsMultipartFile数组即可
        @RequestMapping("/upload")
        public String fileUpload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
            String fileName = file.getOriginalFilename();

           if("".equals(fileName) || fileName == null){
               return "redirect:/index.jsp";
           }

           String path = request.getSession().getServletContext().getRealPath("/upload");
           File realPath = new File(path);
           if(!realPath.exists()){
               realPath.mkdir();
           }
            file.transferTo(new File(path+"/"+file.getOriginalFilename()));
            return "redirect:/index.jsp";
        }
}

form表单需要设置 enctype=“multipart/form-data” 将数据以二进制流方式传输

文件下载

package com.rush.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @author:zmc
 * @function:
 * @date: 2020/8/6 10:41
 */
@Controller
public class FileUploader {
        //@RequestParam("file") 将name=file控件得到的文件封装为CommonsMultipartFile对象
        //批量上传使用CommonsMultipartFile数组即可
        @RequestMapping("/download")
        public String download(HttpServletResponse response,HttpServletRequest request) throws IOException {
            //输入想下载的文件名
            String fileName = "1.png";

           String path = request.getSession().getServletContext().getRealPath("/upload");
           //设置页面不缓存,清空buffer
           response.reset();
           response.setCharacterEncoding("UTF-8");
           response.setContentType("multipart/form-data");
           response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(fileName,"UTF-8"));
           File file = new File(path,fileName);
           InputStream input = new FileInputStream(file);
            OutputStream out = response.getOutputStream();
            byte[] buff = new byte[1024];
            int index = 0;

            while((index = input.read(buff)) != -1){
                out.write(buff,0,index);
                out.flush();
            }
            out.close();
            input.close();
            return "redirect:/index.jsp";
        }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值