0

I'm so close to having this working but I'm missing something simple.

When my RestController returns a string "index" it should go to index.html but instead I get a blank page that say "index" on it. It's got to be my config is off but I don't know how.

I put my index.html into WEB-INF\templates and it just contains a paragraph that says "Hello World". I thought about using Spring Boot but I want to figure this out so I'm skipping that for now. Plus I think I'm pretty close to getting this working.

What stands for my applicationContext.xml:

@Configuration
@ComponentScan({"com.spring.poc.web"})
@EnableWebMvc
public class SpringWebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }

    @Bean
      public ViewResolver viewResolver() {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setCacheable(false);
        templateResolver.setPrefix("/WEB-INF/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");

        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);

        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setCharacterEncoding("UTF-8");
        viewResolver.setOrder(1);
        viewResolver.setTemplateEngine(templateEngine);

        return viewResolver;
      }
}

Where my spring @Services are:

@Configuration
@ComponentScan({"com.nox.poc.service"})
public class SpringRootConfig {

}

What represents my web.xml

    public class WebXmlInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { SpringRootConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { SpringWebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/"};
    }

}

My Controller:

@RestController
public class PocRestController {



    @RequestMapping(value = "/")
    public String getMainPage(ModelMap map) {

        return "index";
    }   

}

My pom.xml (I'm using Eclipse Mars):

    project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.spring.poc</groupId>
  <artifactId>pocApp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

    <properties>
        <spring.version>4.2.2.RELEASE</spring.version>
        <jackson.version>2.6.3</jackson.version>
        <faces.version>2.2.12</faces.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- jackson allows spring RestController to bind whatever we send back as JSON -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.3</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

When I start tomcat 8 in Eclipse everything starts fine, nothing in the external log files. However when I hit localhost:8080\pocApp\

I get a blank page that says "index". So clearly I'm missing some configuration but I haven't found the answer. It has to be something small...

Justin
  • 859
  • 4
  • 15
  • 30
  • Possible duplicate of [how to use spring4 @RestController to return a jsp page?](http://stackoverflow.com/questions/26031254/how-to-use-spring4-restcontroller-to-return-a-jsp-page) – Sotirios Delimanolis Nov 17 '16 at 16:50

1 Answers1

0

So I was missing something fundamental. Found the answer from more googling. Came across this:

how to use spring4 @RestController to return a jsp page?

Changing my annotation from @RestController to @Controller worked. And now it all makes sense!

Community
  • 1
  • 1
Justin
  • 859
  • 4
  • 15
  • 30