SpringMVC02

news2024/9/20 12:40:36

1.拦截器

1.1基本概念

SpringMVC 中的Interceptor拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理。比如通过它来进行权限验证,或者是来判断用户是否登陆等操作。对于SpringMVC拦截器的定义方式
有两种:

实现接口:org.springframework.web.servlet.Handlerlnterceptor

继承适配器:org.springframework.web.servlet.handler.HandlerlnterceptorAdapter

1.2 拦截器实现

(1)实现HandleInterceptor接口

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*拦截器的实现
* 实现HandlerInterceptor接口
* */
public class MyInterceptor01 implements HandlerInterceptor{
   /*
   *
   * 在目标Handler(方法)执行前 执行
   * 返回true 执行Handler方法
   * 返回false 阻止Handler方法的执行
   * @param request
   * @param response
   * @param Exception
   *
   * */


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("目标Handler执行前MyInterceptor01 --》preHandle方法");
   return true;
    }

    /*
     *
     * 在目标Handler(方法)视图生成前 执行
     * 返回true 执行Handler方法
     * 返回false 阻止Handler方法的执行
     * @param request
     * @param response
     * @param Exception
     *
     * */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("目标Handler视图生成前MyInterceptor01 --》postHandle方法");
    }
    /*
     *
     * 在目标Handler(方法)视图生成后 执行
     * 返回true 执行Handler方法
     * 返回false 阻止Handler方法的执行
     * @param request
     * @param response
     * @param Exception
     *
     * */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("目标Handler视图生成后MyInterceptor01 --》 afterCompletion方法");
    }
}

在servlet-context.xml中配置拦截器

方法一:

<!-- 配置拦截器 方式一 -->
<mvc:interceptors>
    <!--使用bean标签定义一个Interceptor
    直接定义在mvc:interceptro软标签中,表示会拦截所有的请求。
    -->
    <bean class="com.xxxx.springmvc.intercreptor.MyInterceptor01"/>
</mvc:interceptors>

方法2:

<!--拦截器配置 方法2 推荐使用-->
<mvc:interceptors>
    <mvc:interceptor>
        <!--通过mvc:mapping 配置需要被拦截的资源 支持通配符 支持可配置多个-->
        <!-- “/**”表示拦截所有的请求-->
        <mvc:mapping path="/**"/>
        <!--通过mvc:exclude-mapping 配置不需要拦截的资源 支持通配符 支持可配置多个-->
        <mvc:exclude-mapping path="/url/*"/><!-- "/url/*"表示url路径下的所有资源-->
        <bean class="com.xxxx.springmvc.intercreptor.MyInterceptor01"/>
    </mvc:interceptor>
</mvc:interceptors>

运行结果:

a88f8cf460b3466ba3e4483c5874ae56.png

(2)继承HandleInterceptorAdapter

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

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

/*
* 拦截器实现
* 继承HandleInterceptorAdapter适配器
* */
public class MyInterceptor02 extends HandlerInterceptorAdapter {
    /*
     *
     * 在目标Handler(方法)执行前 执行
     * 返回true 执行Handler方法
     * 返回false 阻止Handler方法的执行
     * @param request
     * @param response
     * @param Exception
     *
     * */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("目标Handler执行前MyInterceptor01 --》preHandle方法");
        return true;
    }

}

在servlet-context.xml中配置拦截器

<mvc:interceptors>

    <mvc:interceptor>
        <!--拦截的信息-->
        <mvc:mapping path="/**"/>
        <!--放行的信息-->
        <mvc:exclude-mapping path="/url/test01"/>
        <mvc:exclude-mapping path="/url/test02"/>
        <bean class="com.xxxx.springmvc.intercreptor.MyInterceptor02"/>

    </mvc:interceptor>

</mvc:interceptors>

(3)多个拦截器实现

<mvc:interceptors>
    <mvc:interceptor>
        <!--拦截器链 也叫多个拦截器-->
        <!--如果有多个拦截器满足拦截处理的要求 则会根据配置的先后顺序执行
        先配置的拦截器的preHandle方法先执行
        先配置的拦截器的postHandle afterCompletion 方法后执行
        -->
        <mvc:mapping path="/**"/>
        <bean class="com.xxxx.springmvc.intercreptor.MyInterceptor01"/>
    </mvc:interceptor>
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <bean class="com.xxxx.springmvc.intercreptor.MyInterceptor02"/>
    </mvc:interceptor>
</mvc:interceptors>

1.3 拦截器应用

非法请求拦截

(1)用户控制器

UserInfoController定义

*
* 用户模块(不需要拦截)
*
* 用户添加(需要拦截)
* 用户更新(需要拦截)
* 用户删除(需要拦截)
* */
@Controller
@RequestMapping("/userInfo")
public class UserinfoController {
/*
* 用户登录
* */
    @RequestMapping("/login")
    public ModelAndView userLogin(HttpSession session){
    System.out.println("用户登录。。。");
    ModelAndView modelAndView=new ModelAndView();
   //设置视图
    modelAndView.setViewName("success");

    //如果用户登录,则设置用户对象到session作用域中。
        User user=new User();
        user.setId(1);
        user.setUserName("admin");
        user.setUserPassword("123334");
     session.setAttribute("user", user);
    return modelAndView;
}
    /*
     * 用户添加
     * */
    @RequestMapping("/add")
    public ModelAndView userAdd(){
        System.out.println("用户添加。。。");
        ModelAndView modelAndView=new ModelAndView();
        //设置视图
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /*
     * 用户更新
     * */
    @RequestMapping("/update")
    public ModelAndView userUpdate(){
        System.out.println("用户更新。。。");
        ModelAndView modelAndView=new ModelAndView();
        //设置视图
        modelAndView.setViewName("success");
        return modelAndView;
    }
    /*
     * 用户删除
     * */
    @RequestMapping("/delete")
    public ModelAndView userDelete(){
        System.out.println("用户删除。。。");
        ModelAndView modelAndView=new ModelAndView();
        //设置视图
        modelAndView.setViewName("success");
        return modelAndView;
    }
}

(2)在servlet-context.xml配置拦截器(这里只配置了登录的)

import com.xxxx.springmvc.po.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

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

public class Loginintercreptor extends HandlerInterceptorAdapter {
    /*
    * 在目标方法执行前 执行
    * @param request
    * @param response
    * @param
    * @return
    * @throws Exception
    *
    * */

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

        //获取session作用域中的user
        User user=(User)request.getSession().getAttribute("user");
        //判断对应的session作用域中的session是否为空
        if(user==null){//如果为空,表示用户未登录
            //拦截请求并拦截转到登录界面
            response.sendRedirect(request.getContextPath()+"/login.jsp");
           //不执行目标方法
           return false;
        }
        return true;
    }
}

1.4 文件上传

1.4.1 环境配置

(1)在pom.xml中添加依赖

<!-- 添加 commons-fileupload依赖  -->

 <dependency>
     <groupId>commons-fileupload</groupId>
     <artifactId>commons-fileupload</artifactId>
     <version>1.3.2</version>
 </dependency>

(2)在servlet-context.xml中修改(添加)

 <!--文件上传-->
    </bean>
    <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
   <!--  允许文件上传的最大尺寸 -->
  <property name="maxUploadSize">
   <value>104857600</value>
    </property>
<!--
设置文件放入临时文件夹的最小大小限制。
此值是阈值,低于此值,则保存在内存中,如高于此值,则生成硬盘上的临时文件。-->

   <property name="maxInMemorySize">
     <value>4096</value>
      </property>

</bean>

1.4.2  代码实现

(1)单文件上传

页面表单
1.表单的method属性设置为POST
2.表单的enctype属性设置为multipart/form-data
3.input的type属性设置为file,且设置对应的name属性。
<form method="post" action="uploadFile" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <button>上传</button>
</form>

代码实现

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

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 文件上传控制器
 */
@Controller
public class FileController {

    /**
     * 单文件上传
     * @param request HttpServletRequest
     * @param file 上传的文件
     * @return 结果视图名
     */
    @RequestMapping("/uploadFile")
    public String uploadFile(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
        // 判断文件是否为空,如果不为空,则进行对应的文件上传操作
        if (!file.isEmpty()) {
            try {
                // 获取项目所在路径(绝对路径)
                String path = request.getServletContext().getRealPath("/");
                // 设置上传文件存放的目录
                File uploadFile = new File(path + "/upload");
                // 判断目录是否存在,如果不存在则创建目录
                if (!uploadFile.exists()) {
                    uploadFile.mkdir();
                }
                // 获取上传文件的文件名
                String originalFilename = file.getOriginalFilename();
                // 获取上传文件的后缀名
                String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
                // 通过当前系统时间毫秒数生成随机的文件名
                String filename = System.currentTimeMillis() + suffix;
                // 转存文件到指定目录
                file.transferTo(new File(path, filename));
                // 如果上传成功,设置作用域
                request.setAttribute("msg", "文件上传成功");
            } catch (IOException e) {
                e.printStackTrace();
                request.setAttribute("msg", "文件上传失败");
            }
        } else {
            request.setAttribute("msg", "文件不存在");
        }
        return "result";
    }
}

aa0b25d352414e50a9ea93a778c30634.png

b0ea121efb4648cd8f6574fac6aca4b4.png

514b2ecae42044b3b381eb3aee7edbd5.png

(2)多文件上传

页面表单

<form method="post" action="uploadFiles" enctype="multipart/form-data">
    <input type="file" name="files"/>
    <input type="file" name="files"/>
    <button>上传</button>
</form>

代码实现

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

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 文件上传控制器
 */
@Controller
public class FileController {

    /**
     * 多文件上传
     * @param request HttpServletRequest
     * @param file 上传的文件
     * @return 结果视图名
     */
   
    @RequestMapping("uploadFiles")
    public String uploadFiles(HttpServletRequest request, @RequestParam("files") List<MultipartFile> files) {
        if (files != null && files.size() > 0) {
            for (MultipartFile file : files) {
                saveFile(file, request);
            }
        }
        return "result";
    }
    public void saveFile(MultipartFile file,HttpServletRequest request){
        // 判断文件是否为空,如果不为空,则进行对应的文件上传操作
        if (!file.isEmpty()) {
            try {
                // 获取项目所在路径(绝对路径)
                String path = request.getServletContext().getRealPath("/");
                // 设置上传文件存放的目录
                File uploadFile = new File(path + "/upload");
                // 判断目录是否存在,如果不存在则创建目录
                if (!uploadFile.exists()) {
                    uploadFile.mkdir();
                }
                // 获取上传文件的文件名
                String originalFilename = file.getOriginalFilename();
                // 获取上传文件的后缀名
                String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
                // 通过当前系统时间毫秒数生成随机的文件名
                String filename = System.currentTimeMillis() + suffix;
                // 转存文件到指定目录
                file.transferTo(new File(uploadFile, filename));
                // 如果上传成功,设置作用域
                request.setAttribute("msg", "文件上传成功");
            } catch (IOException e) {
                e.printStackTrace();
                request.setAttribute("msg", "文件上传失败");
            }
        } else {
            request.setAttribute("msg", "文件不存在");
        }

    }
    }
在servlet-context.xml中修改(在拦截器里添加多文件上传的请求允许)
<mvc:exclude-mapping path="/uploadFiles"/>

1.5 SSM集成与测试

1.5.1环境配置

(1)IDEA下创建项目

创建Mavenapp项目

a976d2717c674e46ac98b1aae97c52c9.png

(2) 配置pom.xml

<?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>com.xxxx</groupId>
  <artifactId>SSM</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>SSM Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
<!--添加坐标依赖-->
    <!--junit 测试 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <!-- spring核心jar -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.4.RELEASE</version>
    </dependency>

    <!--spring测试jar -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.2.4.RELEASE</version>
    </dependency>
    <!--spring jdbc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.4.RELEASE</version>
    </dependency>

    <!-- spring事物-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.2.4.RELEASE</version>
    </dependency>

    <!-- aspectj切面编程的jar-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.5</version>
    </dependency>
    <!--c3p0 连接池 -->
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>

    <!--mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.3</version>
    </dependency>

    <!-- 添加mybatis与Spring整合的核心包 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.3</version>
    </dependency>

    <!-- mysql 驱动包 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.19</version>
    </dependency>
    <!-- 日志打印相关的jar-->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.2</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.2</version>
    </dependency>

    <!-- 分页插件 -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.10</version>
    </dependency>

    <!-- spring web -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.4.RELEASE</version>
    </dependency>
    <!-- spring mvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.4.RELEASE</version>
    </dependency>

    <!-- web servlet -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
    </dependency>
    <!--    添加json 依赖jar包(注意版本问题)-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.10.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.10.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.10.0</version>
    </dependency>


    <!--commons-fileupload -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.2</version>


    </dependency>
  </dependencies>
  <!--设置资源目录和插件-->
  <build>
    <finalName>ssm</finalName>
    <!-- Maven 项目:如果源代码(src/main/java)存在xml、properties、tld 等文件
     Maven 默认不会自动编译该文件到输出目录,如果要编译源代码中xml properties tld 等文件
     需要显示配置 resources 标签-->
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
          <include>**/*.tld</include>
        </includes>
        <filtering>false</filtering>
      </resource>

    </resources>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>

        </configuration>
      </plugin>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.4.27.v20200227</version>
        <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>

          <!--设置窗口-->
          <httpConnector>
            <port>8080</port>

          </httpConnector>
          <!--设置项目路径-->
          <webAppConfig>
            <contextPath>/ssm</contextPath>
          </webAppConfig>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.1</version>
        <configuration>
          <warSourceDirectory>src/main/webapp</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>

  </build>
</project>





(3)配置web.xml

位于webapp/WEB-INF/下

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app id="webApp_ID" 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">

  <!-- 启动spring容器 -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring.xml</param-value>
  </context-param>
   <!--设置监听器-->
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--编码过滤UTF-8-->

<filter>
<description>char encoding filter</description>
<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>

 <!--servlet请求分发器-->
<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:servlet-context.xml</param-value>
</init-param>
  <!--表示启动容器时初始化该Servlet-->
  <load-on-startup>1</load-on-startup>
</servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <!--这是拦截请求,"/"表示拦截所有请求,"*.do"拦截所有.do请求-->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

(4)配置servlet-context.xml文件

在src/main/resources下创建此.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
    <!-- 开启扫描器-->
<context:component-scan base-package="com.xxxx.ssm.controller"/>

 <!--mvc 注解驱动 并添加json 支持-->
<mvc:annotation-driven>
<mvc:message-converters>
 <!--返回信息为字符串时 处理 -->
<bean
class="org.springframework.http.converter.StringHttpMessageConverter"/>
<!--将对象转换为json 对象 -->
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
    <!--使用默认的Servlet响应静态文件-->
    <mvc:default-servlet-handler/>
    <!--配置视图解析器-->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀:在WEB-INF目录下的jsp目录下 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
 <!--后缀:以.jsp结尾的资源-->
<property name="suffix" value=".jsp"/>
</bean>
    <!--文件上传-->

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <!--允许文件上传的最大尺寸 -->
<property name="maxUploadSize">
<value>104857600</value>
</property>

 <!--设置文件放入临时文件夹的最大大小限制。
 此值是阈值,低于此值,则保存在内存中,如高于此值,则生成硬盘上的临时文件。
-->

 <property name="maxInMemorySize">
 <value>4096</value>
 </property>
</bean>
</beans>

(5)配置spring.xml文件

在src/main/resources下创建此.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
    <!-- 开启扫描器-->
<context:component-scan base-package="com.xxxx.ssm.controller"/>

 <!--mvc 注解驱动 并添加json 支持-->
<mvc:annotation-driven>
<mvc:message-converters>
 <!--返回信息为字符串时 处理 -->
<bean
class="org.springframework.http.converter.StringHttpMessageConverter"/>
<!--将对象转换为json 对象 -->
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
    <!--使用默认的Servlet响应静态文件-->
    <mvc:default-servlet-handler/>
    <!--配置视图解析器-->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀:在WEB-INF目录下的jsp目录下 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
 <!--后缀:以.jsp结尾的资源-->
<property name="suffix" value=".jsp"/>
</bean>
    <!--文件上传-->

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <!--允许文件上传的最大尺寸 -->
<property name="maxUploadSize">
<value>104857600</value>
</property>

 <!--设置文件放入临时文件夹的最大大小限制。
 此值是阈值,低于此值,则保存在内存中,如高于此值,则生成硬盘上的临时文件。
-->

 <property name="maxInMemorySize">
 <value>4096</value>
 </property>
</bean>
</beans>

(6)配置mybatis.xml

在src/main/resources下创建此.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.xxxx.ssm.vo"/>
</typeAliases>
<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
</configuration>

(7)配置db.properties

3d5b9756c83d430fa00ad8fc8594b6b7.png

在resources目录下创建对应文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost: 3306/ssm?
useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
jdbc.username=root
jdbc.password=root

(8)配置log4j.properties

同上,在resources目录下创建对应文件

log4j.rootLogger=DEBUG, Console
# Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org. apache. log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

1.5.2 添加源代码

(1)添加包

在项目的src/main/java下创建对应的包结构

com.xxxx.ssm.controller

com.xxxx.ssm.service

com.xxxx.ssm.mapper

com.xxxx.ssm.dao

com.xxxx.ssm.po

06e5ca0c7cc842f3b2f01414519ad8c7.png

(2)添加User.java

在com.xxxx.ssm.po包下创建javaBean文件User.java(数据库字段如下)

1f0a82d4b3984ed5b880f325aceab6c2.png

import java.util.Date;

public class User {
    private Integer userId;
    private String userName;
    private String userPwd;
    private String userEmail;
    private Date createDate;
    private Date updateDate;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }
}

(3)添加UserDao.java接口

在com.xxxx.ssm.dao包下创建UserDao.java文件,提供对应的用户详情查询功能。

package com.xxxx.ssm.dao;

import com.xxxx.ssm.po.User;

public interface UserDao {
    User queryUserByUserId(Integer userId);
}

(4)添加UserMapper.xml映射文件

com.xxxx.ssm.mapper包下创建UserMapping.sml文件,提供select查询标签配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxxx.ssm.dao.UserDao">
<select id="queryUserByUserId" parameterType="int" resultType="com.xxxx.ssm.po.User">
select user_id as userId,user_name as userName,user_pwd as userPwd
from tb_user
where user_id = #{userId}
</select>
        </mapper>

(5)添加UserService.java

在com.xxxx.ssm.service包下创建UserService.java文件

import com.xxxx.ssm.dao.UserDao;
import com.xxxx.ssm.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;
    public User queryUserByUserId(Integer userId){
        return userDao.queryUserByUserId( userId);
    }
}

(6)添加UserController.java

在com.xxxx.ssm.controller包下创建UserController.java文件

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {
    //注入userService
    @Autowired
    private UserService userService;
    @RequestMapping("/hello")
    public String hello(Model model){
        ModelAndView modelAndView=new ModelAndView();
        //调用UserService层查询方法
        User user=userService.queryUserByUserId(1);
        model.addAttribute("user", user);
      return "hello";

    }
}

结果:

9e94abba5e864aa2b26bea0cb4468609.png

b856fe61715d46618aef2381ee95a987.png

1.6 Restful URL

1.6.1 基本概念

模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计思想。

Restful风格的API是一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件。它主
要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

在Restful风格中,用户请求的url使用同一个url,用请求方式:get,post,delete,put ... 等方式对请求的
处理方法进行区分,这样可以在前后台分离式的开发中使得前端开发人员不会对请求的资源地址产生混淆和大量的
检查方法名的麻烦,形成一个统一的接口。

在Restful风格中,现有规定如下:

· GET(SELECT):从服务器查询,可以在服务器通过请求的参数区分查询的方式。

· POST(CREATE):在服务器端新建一个资源,调用insert操作。

·PUT(UPDATE):在服务器端更新资源,调用update操作。
· PATCH(UPDATE):在服务器端更新资源(客户端提供改变的属性)。(目前jdk7未实现,tomcat7不支
持)。
· DELETE(DELETE):从服务器端删除资源,调用delete语句。

1.6.2 SpringMVC支持RestFul Url风格设计

案例:如何使用Java构造没有扩展名的RESTful url,如/forms/1?

SpringMVC是通过@RequestMapping 及@PathVariable注解提供的。

通过如@RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE),即可处理/blog/1的delete 请求。

1.6.3 RestFul Url映射地址配置实现

1 准备环境

(1)添加Account

在src/resources/java对应的com.xxxx.ssm.po目录下创建Account.java实体类

public class Account {
    private Integer id;
    private String aname;
    private String type;
    private Double money;
    private Integer userId;
    private Date createTime;
    private Date updateTime;
    private String remark;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAname() {
        return aname;
    }

    public void setAname(String aname) {
        this.aname = aname;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}

(2)添加AccountDao

在src/resources/java对应的com.xxxx.ssm.dao目录下新建AccountDao.java接口类

public interface AccountDao {
    public Account selectById(Integer id);
    public int save(Account account);

    public int update(Account account);


    public int delete(Integer id);
}

(3)添加AccountMapper

在src/resources/java对应的com.xxxx.ssm.mapper目录下新建AccountMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxxx.ssm.dao.AccountDao">
    <resultMap id="BaseResultMap" type="com.xxxx.ssm.po.Account">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="aname" property="aname" jdbcType="VARCHAR"/>
        <result column="type" property="type" jdbcType="VARCHAR"/>
        <result column="money" property="money" jdbcType="DOUBLE"/>
        <result column="user_id" property="userId" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="DATE"/>
        <result column="update_time" property="updateTime" jdbcType="DATE"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, aname, type, money, user_id, create_time, update_time, remark
    </sql>

    <!-- 查询操作 -->
    <select id="selectById" resultMap="BaseResultMap" parameterType="java.lang.Integer">
        select
        <include refid="Base_Column_List"/>
        from tb_account
        where id = #{id,jdbcType=INTEGER}
    </select>

    <!-- 删除操作 -->
    <delete id="delete" parameterType="java.lang.Integer">
        delete from tb_account
        where id = #{id,jdbcType=INTEGER}
    </delete>

    <!-- 添加操作 -->
    <insert id="save" parameterType="com.xxxx.ssm.po.Account">
        insert into tb_account
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="aname != null">aname,</if>
            <if test="type != null">type,</if>
            <if test="money != null">money,</if>
            <if test="userId != null">user_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values(" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id,jdbcType=INTEGER},</if>
            <if test="aname != null">#{aname,jdbcType=VARCHAR},</if>
            <if test="type != null">#{type,jdbcType=VARCHAR},</if>
            <if test="money != null">#{money,jdbcType=DOUBLE},</if>
            <if test="userId != null">#{userId,jdbcType=INTEGER},</if>
            <if test="createTime != null">#{createTime,jdbcType=DATE},</if>
            <if test="updateTime != null">#{updateTime,jdbcType=DATE},</if>
            <if test="remark != null">#{remark,jdbcType=VARCHAR},</if>
        </trim>
    </insert>

    <!-- 更新操作 -->
    <update id="update" parameterType="com.xxxx.ssm.po.Account">
        update tb_account
        <set>
            <if test="aname != null">aname = #{aname,jdbcType=VARCHAR},</if>
            <if test="type != null">type = #{type,jdbcType=VARCHAR},</if>
            <if test="money != null">money = #{money,jdbcType=DOUBLE},</if>
            <if test="userId != null">user_id = #{userId,jdbcType=INTEGER},</if>
            <if test="createTime != null">create_time = #{createTime,jdbcType=DATE},</if>
            <if test="updateTime != null">update_time = #{updateTime,jdbcType=DATE},</if>
            <if test="remark != null">remark = #{remark,jdbcType=VARCHAR},</if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
</mapper>

(4)添加AccountService

在src/resources/java对应的com.xxxx.ssm.service目录下创建AccountService.java实体类

package com.xxxx.ssm.service;

import com.xxxx.ssm.dao.AccountDao;
import com.xxxx.ssm.po.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountService {
    @Autowired
private AccountDao accountDao;
    public Account selectById(Integer id) {
        return accountDao.selectById(id);
    }


        public int saveAccount(Account account) {
            return accountDao.save(account);
        }
            public int updateAccount(Account account) {
                return accountDao.update(account);
            }
                public int delAccount(Integer id){
                    return accountDao.delete(id);
                }
            }

2 URL映射地址配置

(1)GET请求配置

 /*
 * 传统的URL访问:
 * http://Localhost:8080/ssm/account/queryAccountById?id=1
 *
 * RestFul URL访问:
 * @GetMapping("/account/id")
 * http://localhost:8080/ssm/account/1
 * @PathVariable Integer id
 * 将形参设置为参数路径 声明在形参前面
 * @param
 * @return
 * */
/* @RequestMapping("/account/queryAccountById")*/
 @GetMapping("/account/{id}")
 @ResponseBody
 public Account queryAccountById(@PathVariable Integer id) {
     return accountService.selectById(id);


 }

(2)DELETE请求配置

/*删除操作
*
*
*
*@param
 *@return
 * */
@DeleteMapping("/account/{id}")
@ResponseBody
public Map<String,String> deleteAccountById(@PathVariable Integer id){
    //调用service层的删除方法,返回受影响的行数
    int row=accountService.delAccount(id);
    //判断受影响的行数是否大于0
    Map<String ,String> map=new HashMap<>();
    if(row>0){
        //删除成功
        map.put("code", "200");
        map.put("msg", "删除成功");
    }else{
        //删除失败
        map.put("code", "500");
        map.put("msg", "删除失败");
    }
    return map;
}

(3)POST请求配置

/*
* 添加操作
* */
@PostMapping("/account")
@ResponseBody
public Map<String,String> addAccount(@RequestBody Account account){
    Map<String ,String> map=new HashMap<>();
    // //调用service层的添加方法,返回受影响的行数
    int row=accountService.saveAccount(account);
    //判断受影响的行数是否大于0

    if(row>0){
        //删除成功
        map.put("code", "200");
        map.put("msg", "添加成功");
    }else{
        //删除失败
        map.put("code", "500");
        map.put("msg", "添加失败");
    }
    return map;
}

(4)PUT请求配置

/*更新操作
 *
 *
 *
 *@param
 *@return
 * */
@PutMapping("/account")
@ResponseBody
public Map<String,String> updateAccountById(@RequestBody Account account){
    //调用service层的删除方法,返回受影响的行数
    int row=accountService.updateAccount(account);
    //判断受影响的行数是否大于0
    Map<String ,String> map=new HashMap<>();
    if(row>0){
        //删除成功
        map.put("code", "200");
        map.put("msg", "更新成功");
    }else{
        //删除失败
        map.put("code", "500");
        map.put("msg", "更新失败");
    }
    return map;
}

1.7 全局异常统一处理

1.7.1 概念

在JavaEE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的、不可预知的异常需要处理。每个过程都单独处理异常,系统的代码耦合度高,工作量大且不好统一,维护的工作量也很大。

SpringMVC对于异常处理这块提供了支持,通过SpringMVC提供的全局异常处理机制,能够将所有类型的异常处理从各处理过程解耦出来,既保证了相关处理过程的功能较单一,也实现了异常信息的统一处理和维护。
全局异常实现方式Spring MVC处理异常有3种方式:
1.使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver
2.实现Spring的异常处理接口HandlerExceptionResolver 自定义自己的异常处理器
3.使用@ExceptionHandler 注解实现异常处理

1.7.2 异常处理实现

(1)全局异常处理方式一

1. 配置简单异常处理器

配置SimpleMappingExceptionResolver对象

<!--配置简单异常处理器-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <!--页面在转发时出现异常 设置默认的页面 -->
    <property name="defaultErrorView" value="error"/>
    <!--异常发生时 设置异常的变量名-->
    <property name="exceptionAttribute" value="ex"/>
    

可以在处理异常的页面获取异常信息

在WEBINF/jsp目录下创建一个error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>处理异常页面</h3>
<h4>${ex}</h4>
</body>
</html>

2.使用自定义异常

参数异常

/*
* 自定义异常:参数异常*/
public class ParamsException extends RuntimeException{
    private Integer code=300;
    private String msg="参数异常";
    public ParamsException(){
        super("参数异常!");
    }

    public ParamsException(String msg) {
        super(msg);
        this.msg = msg;
    }
    public ParamsException(Integer code) {
        super("参数异常!");
        this.code= code;
    }
    public ParamsException(Integer code,String msg) {
        super(msg);
        this.code= code;
    this.msg=msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

业务异常

public class BusinessException extends RuntimeException{
private Integer code=400;
private String msg="业务异常";
    public BusinessException(){
        super("业务异常!");
    }

    public BusinessException(String msg) {
        super(msg);
        this.msg = msg;
    }
    public BusinessException(Integer code) {
        super("业务异常!");
        this.code= code;
    }
    public BusinessException(Integer code,String msg) {
        super(msg);
        this.code= code;
        this.msg=msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

3.设置自定义异常和页面的映射

<!--设置自定义异常和页面的映射-->
<property name="exceptionMappings">
    <props >
        <!--key代表的是自定义异常的路径:标签中设置的是具体的页面-->
        <prop key="com.xxxx.ssm.exception.ParamsException">params-error</prop>
        <prop key="com.xxxx.ssm.exception.BusinessException">business-error</prop>
    </props>
</property>

(2)全局异常处理方式二(推荐)

1. 实现HandlerExceptionResolver接口

@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView=new ModelAndView("error");
        modelAndView.addObject("ex", "默认的错误信息");

       
        return modelAndView;
    }
}

2.自定义异常处理

@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView=new ModelAndView("error");
        modelAndView.addObject("ex", "默认的错误信息");
//自定义异常
        if(ex instanceof ParamsException){
            //设置残花异常页面
            modelAndView.setViewName("params-error");
            ParamsException e=(ParamsException)ex;
            modelAndView.addObject("ex", e.getMsg());

        }
        if(ex instanceof BusinessException){
            modelAndView.setViewName("business-error");
            BusinessException e=(BusinessException)ex;
            modelAndView.addObject("ex", e.getMsg());

        }
        //处理json格式
        /*
        * 判断handler返回的类型 视图/json
        * */
        /*response.getWriter().write("json");
        return null;
        */
        return modelAndView;
    }
}

使用实现 HandlerExceptionResolver接口的异常处理器进行异常处理,具有集成简单、有良好的扩展性、对已有代码没有入侵性等优点,同时,I在异常处理时能获取导致出现异常的对象,有利于提供更详细的异常处理信息。

(3)全局异常处理方法三

页面继承BaseController

/*
* 父类:页面处理器虚继承父类(父类中异常处理的方法需添加注解)
* */
public class BaseController {
    @ExceptionHandler
    public String exc(HttpServletRequest request,HttpServletResponse response,Exception ex){
        request.setAttribute("ex", ex);
        if(ex instanceof ParamsException){
            return "params-error";
        }
        if(ex instanceof BusinessException){
            return "business-error";
        }

return "error";
    }
}

使用@ExceptionHandler 注解实现异常处理,具有集成简单、有扩展性好(只需要将要异常处理的
Controller 类继承于BaseController即可)、不需要附加Spring配置等优点,但该方法对已有代码存在入侵性(需要修改已有代码,使相关类继承于BaseController),在异常处理时不能获取除异常以外的数据。

1.7.3 未捕获异常的处理

对于 Unchecked Exception而言,由于代码不强制捕获,往往被忽略,如果运行期产生了Unchecked
Exception,而代码中又没有进行相应的捕获和处理,则我们可能不得不面对尴尬的404、500 ...... 等服务器内部错误提示页面。|

此时需要一个全面而有效的异常处理机制。目前大多数服务器也都支持在web.xml中通过<error-page>(Websphere/Weblogic)或者<error-code>(Tomcat)节点配置特定异常情况的显示页面。修改web.xml文件,增加以下内容:

 <!-- 出错页面定义-->
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/500.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>

</error-page>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1973018.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

CAD-文字、图块、多行文字,沿多段线对齐到多段线的顶点,沿直线进行均分,都可以操作

图块和文字对齐直线-均布直线-对齐多段线顶点-旋转平行 (defun c:duiqi () ;将图块与直线对齐&#xff0c;并均分。;先创建的图块排最右;先等分的坐标排最右;刚好对应了(defun MoveToPosition (Blockname p_list / ent refPoint dx dy) ;移动对象到指定坐标(prompt "\nSel…

【Git-驯化】一文学会git中对代码进行存储操作:git stash技巧

【Git-驯化】一文学会git中对代码进行存储操作&#xff1a;git stash技巧 本次修炼方法请往下查看 &#x1f308; 欢迎莅临我的个人主页 &#x1f448;这里是我工作、学习、实践 IT领域、真诚分享 踩坑集合&#xff0c;智慧小天地&#xff01; &#x1f387; 免费获取相关内…

@change事件传参

change事件传参 change"(value)>handleChange(value, item,index)" 这样可以接收index参数区分是哪一个组件事件&#xff0c;又可以接收子组件传的value值 <div class"boxItem" v-for"(item, index) in checkPeopleList" :key"inde…

VUE实现TAB切换不同页面

VUE实现TAB切换不同页面 实现效果 资源准备 ReceiveOrderList, TodoListMulti, SignList 这三个页面就是需要切换的页面 首页代码 <template><div><el-tabs v-model"activeTab" type"card" tab-click"handleTabClick"><…

用于相位解包的卷积和空间四向 LSTM 联合网络

原文&#xff1a;A Joint Convolutional and Spatial Quad-Directional LSTM Network for Phase Unwrapping 作者&#xff1a;Malsha V. Perera 和 Ashwin De Silva 摘要&#xff1a; 相位展开是一个经典的病态问题&#xff0c;其目标是从包裹相位中恢复真实的相位。本文&…

鸿蒙(API 12 Beta2版)NDK开发【使用Node-API扩展能力接口】

简介 [扩展能力接口]进一步扩展了Node-API的功能&#xff0c;提供了一些额外的接口&#xff0c;用于在Node-API模块中与ArkTS进行更灵活的交互和定制&#xff0c;这些接口可以用于创建自定义ArkTS对象等场景。 Node-API接口开发流程参考[使用Node-API实现跨语言交互开发流程]…

非负数、0和正整数 限制最大值且保留两位小数在elementpuls表单中正则验证

一、结构 <el-form-item label"单价&#xff1a;" prop"price"><el-inputv-model.trim"formData.price"placeholder"请输入"blur"formMethod.fixTwo"><template #append>(元)</template></el-i…

基础算法:离散化(C++实现)

文章目录 1. 离散化的定义2. 离散化例题2.1 离散化二分2.2 离散化哈希表 1. 离散化的定义 离散化是一种在程序设计和算法优化中常用的技术&#xff0c;其核心思想是将无限空间中有限的个体映射到有限的空间中去&#xff0c;以此提高算法的时空效率。具体来说&#xff0c;离散化…

Docker 安装 GitLab教程

本章教程,主要介绍如何在Docker 中安装GitLab。 GitLab 是一个开源的 DevOps 平台,提供了一整套工具,用于软件开发生命周期的各个阶段,从代码管理到 CI/CD(持续集成和持续交付/部署),再到监控和安全分析。 一、拉取镜像 docker pull gitlab/gitlab-ce:latest二、创建 G…

【React】探讨className的正确使用方式

文章目录 一、className的正确用法二、常见错误解析三、实例解析四、错误分析与解决五、注意事项六、总结 在React开发中&#xff0c;正确使用className属性对组件进行样式设置至关重要。然而&#xff0c;由于JavaScript和JSX的特殊性&#xff0c;开发者常常会犯一些小错误&…

ShardingSphere实战(2)- 水平分表

上篇博客&#xff0c;我们讲了 ShardingSphere实战&#xff08;1&#xff09;- 分库分表基础知识&#xff0c;这篇博客&#xff0c;正式开始实操。 项目环境&#xff1a; JDK11 MySQL 8.0.30 Springboot 2.7.4 Mybatis ShardingSphere HikariCP 连接池 一、Maven 依赖 <…

filebeat发送日志

filebeat: 1.可以在本机收集日志 2.也可以远程收集日志 3.轻量级的日志收集系统&#xff0c;可以在非Java环境运行 logstash是在jvm环境中运行&#xff0c;资源消耗很高&#xff0c;启动一个logstash需要消耗500M左右的内存 filebeat只消耗10M左右的内存 test3是装有logstash的…

C语言的内存布局

根据 C 语言的内存布局规律&#xff0c;通常局部变量和全局变量哪一个的地址更小&#xff1f; 答&#xff1a;如图所示。 下面代码中&#xff0c;为何两个不同的变量可以存放在同一个地址上&#xff1f; #include <stdio.h> void func1(void); void func2(void); voi…

安装 qcloud-python-sts 失败 提示 gbk codecs decode byte 应该如何解决

安装 qcloud-python-sts 失败 提示 gbk codecs decode byte 应该如何解决 解决方案&#xff1a; 将windows 修改为utf-8编码格式 解决步骤如下&#xff1a; 1. 进入控制台 2. 点击区域 4. 点击管理 4.勾选UTF-8 5.重启系统即可

Java零基础之多线程篇:线程间如何通信?

哈喽&#xff0c;各位小伙伴们&#xff0c;你们好呀&#xff0c;我是喵手。运营社区&#xff1a;C站/掘金/腾讯云&#xff1b;欢迎大家常来逛逛 今天我要给大家分享一些自己日常学习到的一些知识点&#xff0c;并以文字的形式跟大家一起交流&#xff0c;互相学习&#xff0c;一…

计算机技术基础 (bat 批处理)Note6

计算机技术基础 &#xff08;bat 批处理&#xff09;Note6 本节主要讲解FOR命令语句&#xff08;循环&#xff09;在 bat 批处理中的使用 (part 2) 变量延迟 命令语句 在没有开启变量延迟的情况下&#xff0c;批处理命令行中的变量改变&#xff0c;必须到下一条命令才能体现…

C++ STL在算法题中的常用语法

Vector 1.将vector<int>中的元素全部置换为0 fill(vec.begin(), vec.end(), 0); 2.vector容器是可以直接用比较是否值等的&#xff01; Unordered_set 1. unordered_set的删除&#xff08;count的值也会减少&#xff09; 2.unordered_map中的int默认值是0&#xff0c;…

Prometheus-v2.45.0+Grafana+邮件告警

目录 普罗米修斯监控架构介绍 Prometheus 监控架构 1. 数据抓取&#xff08;Scraping&#xff09; 2. 时序数据库&#xff08;TSDB&#xff09; 3. 数据模型 4. PromQL 查询语言 5. 告警&#xff08;Alerting&#xff09; 6. Alertmanager 7. 可视化&#xff08;Visu…

从0开始搭建vue + flask 旅游景点数据分析系统( 六):搭建后端flask框架

这一期开始开发header部分&#xff0c;预期实现两个目标&#xff1a; 创建 Flask 项目导入旅游数据后端实现旅游数据的查询 1 python 环境 & 开发环境 python 安装和pycharm安装需要去网上找包&#xff0c;建议python使用3.8 或者3.9版本 2 新建项目 我们新建一个文件…

Kafka详解以及常见kafka基本操作

1、 kafka 是什么,有什么作用 Kafka是一个开源的高吞吐量的分布式消息中间件&#xff0c;对比于缓冲和削峰&#xff1a;上游数据时有突发流量&#xff0c;下游可能扛不住&#xff0c;或者下游没有足够多的机器来保证冗余&#xff0c;kafka在中间可以起到一个缓冲的作用&#x…