Java Spring Boot显示字符串,而不是Angular2单页应用程序中的HTML文件
我正在尝试将所有Java Spring Boot显示字符串,而不是Angular2单页应用程序中的HTML文件,java,html,angularjs,spring-boot,url-mapping,Java,Html,Angularjs,Spring Boot,Url Mapping,我正在尝试将所有GET请求映射到站点的未映射url,以显示index.html文件 因此,我创建了以下@RestController: @RequestMapping(method=RequestMethod.GET) public String redirectToIndex() { return "index.html"; } 我的index.html位于resources/static/index.html中。我还尝试了以下控制器: @RequestMapping(method=
GET
请求映射到站点的未映射url,以显示index.html
文件
因此,我创建了以下@RestController
:
@RequestMapping(method=RequestMethod.GET)
public String redirectToIndex() {
return "index.html";
}
我的index.html
位于resources/static/index.html
中。我还尝试了以下控制器:
@RequestMapping(method=RequestMethod.GET)
public String redirectToIndex() {
return "/static/index.html";
}
但是,在这两种情况下,浏览器都会直接显示字符串index.html
和/static/index.html
,而不是显示html文件。我在这里尝试了很多不同的方法来解决类似的问题,但无法解决。。。请帮忙
编辑:
我的填充@RequestController
类:
@RestController
public class FeedbackController {
@Autowired
private FeedbackService feedbackService;
@RequestMapping(method=RequestMethod.POST, value="/feedback")
public Feedback addFeedback(@RequestBody Feedback feedback) {
return feedbackService.addFeedback(feedback);
}
@RequestMapping(method=RequestMethod.GET)
public String redirectToIndex() {
return "index.html";
}
}
Mypom.xml
:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
org.springframework.boot
spring启动程序父级
1.5.8.1发布
UTF-8
UTF-8
1.8
org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
springbootmaven插件
将生成带有@RestController注释的响应库。在这种情况下,字符串将生成为字符串,而不是视图映射
在您的情况下,您必须使用@Controller,并且必须使用redirect或创建适当的index.jsp并返回“index”
下面是我能想到的代码
将这些添加到您的application.properties中
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
在/main/webapp/WEB-INF/view文件夹下添加index.jsp。为控制器添加以下代码
@Controller
public class MyController {
@RequestMapping("/index")
public String index() {
return "index"
}
}
如果您在SpringBoot项目中使用Thymeleaf作为模板解析器 e、 g: 您可能需要正确设置安全配置才能访问此文件
在控制器中,更改 @RestController 到 @控制器
这将呈现thymeleaf页面,而不是呈现字符串作为输出。您能给出一个例子吗?您是使用jsp还是thymeleaf作为html
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
@Controller
public class FeedbackController {
@Autowired
private FeedbackService feedbackService;
@RequestMapping(method=RequestMethod.POST, value="/feedback")
@ResponseBody
public Feedback addFeedback(@RequestBody Feedback feedback) {
return feedbackService.addFeedback(feedback);
}
@RequestMapping(method=RequestMethod.GET)
public String redirectToIndex() {
return "index";
}
}