Skip to content

Instantly share code, notes, and snippets.

@mloza

mloza/ComponentScan.java

Last active Mar 21, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.