[英]Include template for Springboot/Thymeleaf application
索引控制器:
@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中,当我们单击新链接时,要交换掉的模板部分提供了一个简单的include函数:
<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
您必须使用控制器将可以在表达式中使用的对象放入(Spring)模型中。 我的猜测是它应该起作用,如果您做这样的事情
@RequestMapping("/index")
String index(ModelMap model){
model.addAttribute("template", "my-template")
return "index";
}
它应该工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.