Skip to content

Instantly share code, notes, and snippets.

@mloza
Last active March 21, 2020 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloza/0c02e59ef24757fd473fe339c51c8638 to your computer and use it in GitHub Desktop.
Save mloza/0c02e59ef24757fd473fe339c51c8638 to your computer and use it in GitHub Desktop.
Kod źródłowy do wpisu Spring Boot Widoki - https://blog.mloza.pl/spring-boot-widoki
@ComponentScan(basePackageClasses = {MainController.class, WebAppConfiguration.class})
<dependency>
<groupId>com.lyncode</groupId>
<artifactId>jtwig-spring</artifactId>
<version>2.1.7</version>
</dependency>
@EnableAutoConfiguration
@ComponentScan
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class);
}
}
@Controller
public class MainController {
@RequestMapping("/")
public ModelAndView index() {
return new ModelAndView("index");
}
}
@Controller
public class MainController {
@RequestMapping("/")
public ModelAndView index() {
ModelAndView modelAndView = new ModelAndView("index");
modelAndView.addObject("greetings", "Hello world from variable!");
modelAndView.addObject("greetingsArray", new String[] {"Hello", "World", "!"});
return modelAndView;
}
}
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<h1>Hello World from view!</h1>
<h3>{{ greetings }}</h3>
<ul>
{% for i in greetingsArray %}
<li>{{ i }}</li>
{% endfor %}
</ul>
<ul>
{% for i in greetingsArray %}
<li>{{ loop.index+1}}. {{ i }}</li>
{% endfor %}
</ul>
@Configuration
public class WebAppConfiguration {
@Bean
public ViewResolver viewResolver() {
JtwigViewResolver jtwigViewResolver = new JtwigViewResolver();
jtwigViewResolver.setPrefix("classpath:views/");
jtwigViewResolver.setSuffix(".twig");
return jtwigViewResolver;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment