我非常接近这个工作,但我错过了一些简单的事情。
当我的RestController返回一个字符串" index"它应该转到index.html,但我得到一个空白页面,说"索引"在上面。它必须是我的配置关闭但我不知道如何。
我将index.html放入WEB-INF \ templates中,它只包含一个段落,上面写着" Hello World"。我想过使用Spring Boot,但我想弄清楚这一点,所以我现在就跳过它了。另外,我认为我已经非常接近于这项工作了。
什么代表我的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;
}
}
我的春天@Services在哪里:
@Configuration
@ComponentScan({"com.nox.poc.service"})
public class SpringRootConfig {
}
什么代表我的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[] { "/"};
}
}
我的控制器:
@RestController
public class PocRestController {
@RequestMapping(value = "/")
public String getMainPage(ModelMap map) {
return "index";
}
}
我的pom.xml(我使用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>
当我在Eclipse中启动tomcat 8时,一切都很好,外部日志文件中没有任何内容。但是当我点击localhost:8080 \ pocApp \
时我得到一个空白页面,上面写着&#34; index&#34;。很明显,我错过了一些配置,但我还没有找到答案。它必须是小事......
答案 0 :(得分:0)
所以我错过了一些基本的东西。从更多谷歌搜索找到答案。碰到了这个:
how to use spring4 @RestController to return a jsp page?
将我的注释从@RestController更改为@Controller工作。现在一切都有道理了!