Spring实战 - 整合Spring MVC

  • 环境: MacOS + IntelliJ IDEA 2019.3.1 (Ultimate Edition)

基于Spring实战 - 实现自动登录的功能,整合Spring MVC

1、在pom.xml中增加spring-webmvc依赖

    
    
      org.springframework
      spring-webmvc
      5.2.2.RELEASE
    

2、配置Web.xml

  • CharacterEncodingFilter 配置字符集过滤器,用于解决中文编码问题

  encodingFilter
  org.springframework.web.filter.CharacterEncodingFilter
  
    encoding
    UTF-8
  
  
    forceEncoding
    true
  


  encodingFilter
  /*

  • DispatherServlet 配置Spring的Servlet分发器处理所有HTTP的请求和响应。

    springServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:spring-mvc
    
    1
  
  
    springServlet
    /
  

3、配置Spring MVC




  Spring MVC Configuration
  
  

  
  
    
  
  
  

  
  
    
    
  
  
  

4、在resources 目录下创建cats.properties

web.view.prefix=/WEB-INF/views
web.view.suffix=.jsp

5、在spring-context中去掉对@Controller 的扫描

  
  
    
  

6、在Controller 文件夹下创建MyController

package com.codeonline.cats.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author 码出高薪
 * @Desc.  登录控制
 * @date 2020/1/12 04:57
 */

@Controller
public class LoginController {

  private Logger logger = LoggerFactory.getLogger(LoginController.class);

  @RequestMapping(value ={" ","login"}, method = RequestMethod.GET)
  public String login(){
    return "/login";
  }
}

7、在WEB-INF/views目录下创建login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    码出高薪


登录成功


8、查看整合Spring MVC 效果


整合Spring MVC 效果.png

你可能感兴趣的:(Spring实战 - 整合Spring MVC)