gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:54:54 25 4
gpt4 key购买 nike

索引 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com