Spring Boot中使用模板引擎参数化传参数

目录

 

 

理论

代码及演示


 

理论

在导航页里面,当点击某个栏目的时候,就得被激活,在Spring Boot中,一般把导航页做成片段的形式,这个片段是可以根据参数进行激活某一个栏目,如下的演示所示,通过三元运算,来给class设置属性,这种方式十分有效果!

 

代码及演示

程序运行截图如下:

Spring Boot中使用模板引擎参数化传参数_第1张图片

程序逻辑结构如下:

Spring Boot中使用模板引擎参数化传参数_第2张图片

源码如下:

MyMvcConfig.java

package navactivedemo.demo.config;

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

@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    @Bean
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){

        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {

                registry.addViewController("/").setViewName("index");
                registry.addViewController("/index.html").setViewName("index");
                registry.addViewController("/forum.html").setViewName("forum");
            }
        };

        return adapter;
    }
}

DemoApplication.java

package navactivedemo.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

bar.html




    
    Title
    








forum.html




    
    Title
    



Hello沃德---论坛

index.html




    
    Title
    



Hello沃德---主页

application.properties

spring.thymeleaf.cache=false
server.port=7777

porn.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.19.RELEASE
         
    
    com.loginWebDemo
    demo
    0.0.1-SNAPSHOT
    loginWeb
    Demo project for Spring Boot

    
        1.8
        3.0.9.RELEASE
        2.2.2
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

        
        
            org.webjars
            jquery
            3.3.1
        

        
        
            org.webjars
            bootstrap
            4.0.0
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


 

你可能感兴趣的:(Java,Spring,Boot)