Spring Boot中使用addViewController实现实现无业务逻辑跳转

当项目中涉及大量的页面跳转,我们可以使用addViewControllers方法实现无业务逻辑跳转,从而减少控制器代码的编写。

addViewControllers方法可以实现将一个请求直接映射为视图,不需要编写控制器来实现,从而简化了页面跳转。

简单示例

Spring Boot项目添加以下依赖

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

假如要访问template路径下的hello.html

添加一个配置类,实现WebMvcConfigurer接口,重写addViewControllers方法。添加@Configuration注解。

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/hello").setViewName("hello");
  }
}

效果相当于以下方法:

  @GetMapping("hello")
  public String hello(){
    return "hello";
  }

因为thymeleaf默认提供的视图解析器,映射到template目录下,如果要访问页面自定义的目录,就需要在application.properties中配置了。

假设要访问page下的hello2.html:

首先application.properties中添加以下配置:

spring.thymeleaf.prefix=classpath:/page/
spring.thymeleaf.suffix=.html

然后在配置类中

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/hello2").setViewName("hello2");
  }

}

启动项目,在浏览器中访问hello2
在这里插入图片描述

  • 15
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是详细的步骤: 1. 首先,我们需要在pom.xml添加Quartz的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> ``` 2. 创建一个Job类,继承QuartzJobBean,并实现executeInternal方法。该方法就是我们要执行的任务逻辑。 ``` public class MyJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { // 任务逻辑 } } ``` 3. 创建一个实现SchedulingConfigurer接口的定时任务配置类。该类可以动态添加定时任务,也可以从数据库、配置文件等读取定时任务。 ``` @Configuration public class QuartzConfig implements SchedulingConfigurer { @Autowired private ApplicationContext applicationContext; @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { // 从数据库或配置文件读取定时任务 // ... // 动态添加定时任务 taskRegistrar.addTriggerTask( () -> { MyJob myJob = applicationContext.getBean(MyJob.class); myJob.executeInternal(null); }, triggerContext -> { // 定时任务表达式 CronTrigger cronTrigger = new CronTrigger("0/5 * * * * ?"); return cronTrigger.nextExecutionTime(triggerContext); } ); } } ``` 4. 在Spring Boot主类上加上@EnableScheduling注解,启用定时任务。 ``` @SpringBootApplication @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这样,我们就可以使用QuartzJobBean+SchedulingConfigurer实现动态添加定时任务了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值