Spring实战 - 整合Spring MVC

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

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.2.RELEASE</version>
    </dependency>

2、配置Web.xml

  • CharacterEncodingFilter 配置字符集过滤器,用于解决中文编码问题
<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>
  <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
  • DispatherServlet 配置Spring的Servlet分发器处理所有HTTP的请求和响应。
<servlet>
    <servlet-name>springServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

3、配置Spring MVC

<?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">

  <description>Spring MVC Configuration</description>
  <!--  加载配置文件  -->
  <context:property-placeholder ignore-unresolvable="true" location="classpath:cats.properties"/>

  <!--  使用Annotation自动注册Bean,只扫描@Controller -->
  <context:component-scan base-package="com.codeonline.cats" use-default-filters="false" >
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>
  <!--  支持注解映射 -->
  <mvc:annotation-driven />

  <!--  定义视图文件解析  -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="${web.view.prefix}"></property>
    <property name="suffix" value="${web.view.suffix}"></property>
  </bean>
  <!--  静态资源文件扫描  -->
  <mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
</beans>

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

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

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

  <!-- 扫描指定包中使用注解的类 排除对注释@Controller的扫描-->
  <context:component-scan base-package="com.codeonline.cats">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>

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" %>
<html>
<head>
    <title>码出高薪</title>
</head>
<body>
登录成功
</body>
</html>

8、查看整合Spring MVC 效果
整合Spring MVC 效果.png

猜你喜欢

转载自www.cnblogs.com/9coding/p/12192470.html