SpringBoot通过重写WebMvcConfigurer配置接口中的addViewControllers方法实现页面跳转

33 篇文章 21 订阅
23 篇文章 6 订阅

WebMvcConfigurer 配置接口其实是 Spring 内部的一种配置方式,采用 JavaBean 的形式来代替传统的 XML 配置文件形式进行针对框架个性化定制,可以自定义一些 Handler,Interceptor,ViewResolver,MessageConverter。基于 java-based 方式的 Spring MVC 配置,需要创建一个配置类并实现 WebMvcConfigurer 接口。

了解WebMvcConfigurer配置接口的详解:

请浏览本博客的文章:《SpringBoot中的WebMvcConfigurer配置接口的详解》

以前写 Spring MVC 的时候,如果需要访问一个页面,必须要写 Controller 类,然后再写一个方法跳转到页面,感觉好麻烦,其实重写 WebMvcConfigurer 中的 addViewControllers 方法即可达到效果了。

【示例】SpringBoot 中通过重写 WebMvcConfigurer 配置接口中的 addViewControllers 方法实现页面跳转。

(1)添加依赖

创建 Spring Boot 项目,使用 Maven 添加依赖文件,在 pom.xml 文件中,添加需要的依赖:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

(2)创建页面

在项目中的 resources/templates 目录下,创建 user 目录,并在该目录下创建 user-info.html 页面,页面代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户信息</title>
    <meta name="author" content="pan_junbiao的博客">
</head>
<body>
    <p>您好,欢迎访问 pan_junbiao的博客</p>
    <p>博客地址:https://blog.csdn.net/pan_junbiao</p>
</body>
</html>

(2)创建配置类

创建 com.pjb.config 包,并创建 WebMvcConfig 类,实现 WebMvcConfigurer 接口;重写  addViewControllers 方法;使用 @Configuration 注解,标注该类为配置类。

package com.pjb.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web MVC 配置类
 * @author pan_junbiao
 **/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer
{
    @Override
    public void addViewControllers(ViewControllerRegistry registry)
    {
        //设置页面注册
        registry.addViewController("/toUserInfo").setViewName("user/user-info");
        registry.addViewController("/toUserDetail").setViewName("user/user-detail");
        registry.addViewController("/").setViewName("/index");
    }
}

页面的跳转等同于使用 @Controller 注解的控制器类,代码如下:

/**
 * 用户控制器
 * @author pan_junbiao
 **/
@Controller
public class UserController
{
    @RequestMapping("/toUserInfo")
    public String toUserInfo()
    {
        return "/user/user-info";
    }
}

(3)执行测试

启动项目,在浏览器中输入地址:http://localhost:8080/toUserInfo,执行结果如下:

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Spring Boot 2.6.13,可以通过实现WebMvcConfigurer和WebSecurityConfigurerAdapter接口来同时生效WebMvcConfigurer和WebSecurityConfigurerAdapter的配置方法。 下面是一个示例代码,展示了如何实现这两个接口并使其同时生效: ```java @Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { // 实现WebMvcConfigurer接口方法 @Override public void addInterceptors(InterceptorRegistry registry) { // 添加拦截器 registry.addInterceptor(new MyInterceptor()); } } @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { // 实现WebSecurityConfigurerAdapter接口方法 @Override protected void configure(HttpSecurity http) throws Exception { // 配置安全规则 http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasRole("USER") .anyRequest().authenticated() .and() .formLogin().permitAll() .and() .logout().permitAll(); } } ``` 在上面的示例WebConfig类实现WebMvcConfigurer接口addInterceptors方法,用于添加拦截器。SecurityConfig类继承了WebSecurityConfigurerAdapter类,并重写configure方法,用于配置安全规则。 为了使这两个配置同时生效,需要在启动类上添加@EnableWebMvc和@EnableWebSecurity注解,示例如下: ```java @SpringBootApplication @EnableWebMvc @EnableWebSecurity public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这样,WebMvcConfigurer和WebSecurityConfigurerAdapter的配置方法就会同时生效。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pan_junbiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值