Home > Enterprise >  Spring MVC Integration with Thymeleaf Existing JSP apache tiles
Spring MVC Integration with Thymeleaf Existing JSP apache tiles

Time:12-07

I am trying to configure Thymeleaf Html page with Spring MVC. I have controller method from which I am trying to return he thymeleaf template html page. Its existing project which uses spring mvc tiles.I need to integrate thymeleaf in to existing project. The template Engine is autowired which is coming from different Jar file. I have provided configuration below. I am not getting any exception but getting Page Not found when I try to load the page.

IS it possible to have one flow which resolves view with Tiles Jps and another flow with Thymeleaf template. how can I achieve it .

   @Controller
   @RequestMapping("/thymeleafConfiguration")
   public class ConfigController {

    @Autowired
    TemplateEngine templateEngine; // This class is coming from different jar and I have 
                                    //autowired. xml configuration is provided for reference
    
    
    @PostConstruct   // Changes needs to apply only to certain class so I am using 
                        //postconstruct method in controller 
                        // where I need to use thymeleaf template. 
    public void Init() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setCacheable(false);
        templateResolver.setPrefix("/templates/thymeleafPage/");
        templateResolver.setSuffix(".html");

        templateEngine.setTemplateResolver(templateResolver);

        
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setOrder(1);
        resolver.setTemplateEngine(templateEngine);
 
    }
    @RequestMapping(value = "/view") // controller method where I am redirecting thymeleaf 
                                         page
    public String  viewTemplate(){      
        return "thymeleaf";
    }
}

application-context.xml

<bean id="thymeleafProcessor" class="com.java.ThymeleafTemplateProcessor">
    <property name="templateEngine" ref="templateEngine"/>
</bean>

<bean id="htmlStringTemplateResolver" class="org.thymeleaf.templateresolver.StringTemplateResolver">
    <property name="templateMode" value="HTML" />
    <property name="cacheable" value="true" />
</bean>

<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
    <property name="enableSpringELCompiler" value="true" />
    <property name="templateResolvers">
        <set>
            <ref bean="htmlStringTemplateResolver" />
        </set>
    </property>
</bean>

Project Structure :

  myProject
  |
  |Src 
        -Java
        
        -templates
          -thymeleafPage        
            - thymeleaf.html
            
        -webContent

web.xml

<servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>*.*</url-pattern>
    </servlet-mapping>

Can you please guide me how can I load the Page. I have referred documentation of thymeleaf ThymeleafDocumetation Tutorial

I have followed some examples but couldnt find much difference. I appreciate your help. Jordan

CodePudding user response:

I have found the solution how to make it work for Jsp , HTML and Thymeleaf template together. Thank you

  • Related