【问题标题】:Spring Boot shows the String instead of the HTML file from an Angular2 Single Page appSpring Boot 显示字符串而不是 Angular2 单页应用程序中的 HTML 文件
【发布时间】:2018-05-31 09:38:13
【问题描述】:

我正在尝试将所有 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";
    }

}

我的pom.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>

【问题讨论】:

  • 你用的是jsps还是Thymeleaf?
  • html 的角度

标签: java html angularjs spring-boot url-mapping


【解决方案1】:

在你的控制器中,改变

@RestController

到

@Contoller

这将呈现 thymeleaf 页面,而不是呈现字符串作为输出。

【讨论】:

    【解决方案2】:

    使用@RestController 注解会生成ResponseBody。在这种情况下,字符串将生成为字符串,而不是您的视图映射。

    在您的情况下,您将不得不使用 @Controller 并且您必须使用重定向或创建正确的 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"
         }
    }
    

    【讨论】:

    • 你能举个例子吗?
    【解决方案3】:

    如果您在 SpringBoot 项目中使用 Thymeleaf 作为模板解析器。

    例如:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    

    然后将 index.html 放入 src/main/resources/templates 中。这是 Thymeleaf 寻找静态资源的地方!

    你的 RestController 应该有以下内容

    @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";
    }
    
    }
    

    您可能需要正确设置安全配置才能访问它。

    【讨论】:

      【解决方案4】:
      1. 如果你的控制器要返回 html,那么控制器应该有 @Controller 注解而不是 @RestController
      2. 此外,您应该检查您的 html 所在的位置,如果您使用的是 thymeleaf 模板引擎,您的 html 应该位于 src/main/resources/templates 文件夹中。 做以上两件事应该可以解决您的问题。

      【讨论】:

        猜你喜欢
        • 2019-05-10
        • 2021-08-14
        • 1970-01-01
        • 1970-01-01
        • 2018-02-19
        • 2020-09-24
        • 1970-01-01
        • 2018-06-22
        • 2019-12-06
        相关资源
        最近更新 更多