java - 包含 Spring Boot/Thymeleaf 应用程序的模板

标签 java spring-boot thymeleaf templating

索引 Controller :

@Controller
public class IndexController {

    private static final Logger log = LoggerFactory.getLogger(TmtApplication.class);

    @Autowired
    UsersRepository usersRepository;

    @RequestMapping("/index")
    String index(){
        return "index";
    }
}

MVC 配置:

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/request").setViewName("index");
        registry.addViewController("/requests").setViewName("index");
        registry.addViewController("/team").setViewName("index");
    }

}

在 PHP 中,我们在单击新链接时想要替换的模板部分中有一个简单的包含函数:

<a href="index.php?action=notifications">notifications</a>

    if (!empty($_GET['action'])) { 
    $action = $_GET['action']; 
    $action = basename($action); 
    if (file_exists("templates/$action.htm") 
        $action = "index"; 
    include("templates/$action.htm"); 
} else { 
    include("templates/index.htm"); 
} 

在我的index.html上:

<body>

<div class="container" style="width: 100% !important;">

    <div th:replace="fragments/header :: header"></div>

    // Include dynamic content here depending on which menu item was clicked
    <div th:replace="@{'fragments/' + ${template}} :: ${template}"></div>

    <div th:replace="fragments/footer :: footer"></div>

</div>

</body>

Springboot/Thymeleaf 的等效项是什么?

最佳答案

看看http://www.thymeleaf.org/doc/articles/layouts.html

您必须使用 Controller 将可在表达式中使用的对象放入(Spring)模型中。我的猜测是它应该有效,你可以做类似的事情

@RequestMapping("/index")
String index(ModelMap model){
    model.addAttribute("template", "my-template")
    return "index";
}

应该可以

关于java - 包含 Spring Boot/Thymeleaf 应用程序的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42087618/

相关文章:

java - Azure 上带有命令行参数的 Spring Boot 应用程序

Java 声音文件不播放3

java - jcodec可以将音频添加到MP4文件中吗?

java - Spring 启动 : How to setup test data with liquibase in unit test

java - 在 Spring Boot Thymeleaf Controller 返回的 View 中显示字符串

java - 如何从 spring boot thymeleaf 获取输入值到java类?

javascript - 如何在 Jquery 中访问模型属性(列表)

java - 将 Java 代码 1.5 降级到 1.4(奖励积分 : J2ME, Blackberry!!!)

java - Eclipse:在文本编辑器中使用UTF-8编码使字符串无法正常工作,我该如何解决这个问题?

java - H2 - Oracle - liquibase - org.h2.jdbc.JdbcSQLException : Table "all_sequences" not found;