2017-02-07 12 views
0

Индекс контроллера:Включить шаблон для применения Springboot/Thymeleaf

@Controller 
public class IndexController { 

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

    @Autowired 
    UsersRepository usersRepository; 

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

MVC Config:

@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?

ответ

1

Посмотрите http://www.thymeleaf.org/doc/articles/layouts.html

Вы должны поместить объекты, которые вы можете использовать в выражениях в (Spring) модели с вашим контроллером. Я думаю, что он должен работать, Венна вы Somthing как

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

он должен работать

+0

Что означает параметр «» мой-шаблон»вы предоставили сделать? – santafebound

+0

Это значение должно быть вставлено в

+0

Я вижу, что вы имеете в виду, но это значение всегда должно отличаться в зависимости от того, какой URL-адрес пользователь нажал. Одна из моих ссылок выглядит так, например, '' Make a Request ''. Он должен загрузить index.html и заменить содержимое div на request.html. – santafebound