Spring Boot返回字符串而不是.html文件

时间:2018-12-09 17:36:19

标签: java html spring spring-mvc spring-boot

我尝试运行spring boot应用程序,该应用程序将在静态文件夹中返回HTML静态文件, 问题是:每次加载页面时:127.0.0.1我得到的是字符串“ bakara”,而不是HTML文件bakara.html。 当我加载127.0.0.1/bakara.html时,我得到了bakara.html文件

pom.xml:

library( data.table )
#create two data.tables
dt1 <- as.data.table( DF1 )
dt2 <- as.data.table( DF2 )
#add suffixes to columns, to identify them after the join
names( dt1 ) <- paste0( names( dt1 ), ".dt1" )
names( dt2 ) <- paste0( names( dt2 ), ".dt2" )
#save column order for later
colorder <- c( names( dt1 ), names( dt2 ) )
#set date-time as poosix-stamps
dt1[, `:=`( start.join = as.POSIXct( paste0( Date.dt1, TrackTime.dt1 ), format = "%d/%m/%Y%H:%M:%S" ),
            end.join = as.POSIXct( paste0( Date.dt1, TrackTime.dt1 ), format = "%d/%m/%Y%H:%M:%S" ) )]
dt2[, `:=`( start.join = as.POSIXct( paste0( DATE_SIGHT.dt2, TIME_START.dt2 ), format = "%d-%b-%y%H:%M:%S" ),
            end.join = as.POSIXct( paste0( DATE_SIGHT.dt2, TIME_END.dt2 ), format = "%d-%b-%y%H:%M:%S" ) )]
#set key on dt2
setkey( dt2, start.join, end.join )
#perform the overlap join
result <- foverlaps( dt1, dt2, type = "within", nomatch = 0L )
#drop the join columns
result[, grep( ".join$", names( result ) ) := NULL]
#set the order of columns right
setcolorder( result, colorder )

#       X.dt1    Y.dt1 Date.dt1 Time.dt1 TrackTime.dt1 DATE_SIGHT.dt2 GROUP_SIGHT.dt2 TIME_START.dt2 TIME_END.dt2 GRP_SIZE.dt2
# 1: 113.8578 22.19537 7/1/2016 02:38:04      10:38:04       7-Jan-16               1       10:38:00     10:51:00            1
# 2: 113.8577 22.19537 7/1/2016 02:38:14      10:38:14       7-Jan-16               1       10:38:00     10:51:00            1

application.properties:

<?xml version="1.0" encoding="UTF-8"?>
<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>il.mda.ks</groupId>
<artifactId>mdaForm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mdaForm</name>
<description>Demo project for Spring Boot</description>


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.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>
    <!-- This is a web application -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Tomcat embedded container -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- JSTL for JSP -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <!-- Need this to compile JSP -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Need this to compile JSP, tomcat-embed-jasper version is not working, 
        no idea why -->
    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.6.1</version>
        <scope>provided</scope>
    </dependency>

    <!-- Optional, test for static content, bootstrap CSS -->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.7</version>
    </dependency>

</dependencies>

HomeController.java:

#spring.mvc.view.prefix=/static/
#spring.mvc.view.suffix=.html

spring.mvc.view.prefix=/static
spring.mvc.view.suffix=.html

项目结构:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class HomeController {

    @RequestMapping("/")
    @ResponseBody
    public String welcome() {
        return "bakara";
    }
}

4 个答案:

答案 0 :(得分:2)

默认情况下,Spring Boot看起来适合templates文件夹static文件夹中的html模板,适用于css and js等其他文件。尝试将src/main/resources/templates文件夹中的html文件移动到从控制器方法中删除@ResponseBody,并从应用程序属性spring.mvc.view.prefix=/static中删除它。我希望它能起作用。

答案 1 :(得分:2)

我有同样的问题。仅通过返回带有html文件名的String便无法返回html。对我来说,它只能使用ModelAndView来工作,在您的情况下,它看起来像这样:

@RequestMapping("/")
public ModelAndView welcome() {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("bakara");
    return modelAndView;
}

有人说这种问题是因为jdk版本而发生的。我使用的是jdk14。

答案 2 :(得分:1)

使用注解@ResponseBody时,实际上是告诉spring不要尝试使用返回的名称查找视图。如果需要html,只需从控制器方法中删除注释。

答案 3 :(得分:1)

Sub DeleteSAC() Dim count As Integer Dim i As Integer Dim j As Integer Sheets(1).Select lastRow = ActiveSheet.Cells(Rows.count, "B").End(xlUp).Row 'have to keep data in a table for this to actually work as it ctrls+left to the table, which will end where the very last text of any row is lastColumn = ActiveSheet.Cells(1, Columns.count).End(xlToLeft).Column count = Sheet2.Cells(1, 7).Value i = 2 j = lastColumn For i = 2 To lastRow For j = lastColumn To 1 If Sheet1.Cells(i, j) = "SAC" Then count = count - 1 GoTo NextIteration End If Next j NextIteration: Next i Sheet2.Cells(1, 7) = count End Sub 与VS @Controller

  • @RestController用于将类标记为Spring MVC Controller。
  • @Controller是一个便捷注释,它仅添加了@RestController@Controller注释。

因此,在您的情况下,只需从HomeController.java中的@ResponseBody方法中删除@ResponseBody注释,就足以获得所需的输出。

还可以查看此春季指南,了解如何Serve Web Content with Spring MVC