Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在特定URL上返回一些HTML文件-Spring启动_Java_Spring_Routes_Spring Boot - Fatal编程技术网

Java 如何在特定URL上返回一些HTML文件-Spring启动

Java 如何在特定URL上返回一些HTML文件-Spring启动,java,spring,routes,spring-boot,Java,Spring,Routes,Spring Boot,我不熟悉弹簧靴。当用户给我特定的URL时,我尝试返回一些页面 我有两页: \src\main\resources\static\index.html \src\main\resources\static\admin.html 现在我有以下几对: GET / - return index.html GET /admin.html - return admin.html GET / - return index.html GET /admin - return admin.html 我想要以下

我不熟悉弹簧靴。当用户给我特定的URL时,我尝试返回一些页面

我有两页:

\src\main\resources\static\index.html
\src\main\resources\static\admin.html
现在我有以下几对:

GET / - return index.html
GET /admin.html - return admin.html
GET / - return index.html
GET /admin - return admin.html
我想要以下几双:

GET / - return index.html
GET /admin.html - return admin.html
GET / - return index.html
GET /admin - return admin.html

我知道,我可以创建一些
控制器
,然后我可以使用annotation
@RequestMapping(“/admin”)
并返回我的管理页面。但这需要很多行动。如果我有更多的页面,那该怎么办呢。

用@RequestMapping(“/admin”)注释控制器方法而不是返回“admin”,并将admin.html放在模板目录中。

用@RequestMapping(“/admin”)注释控制器方法而不是返回“admin”并将admin.html放在templates目录中。

除了创建一个
@Controller
使用
@RequestMapping
定义所有方法之外,还有另一种方法更方便,并且在添加或删除html文件时不需要更改

选项1-如果您不介意人们看到.html后缀

将文件保留在静态文件夹中,并向项目中添加
webmvcconfiguer
,如下所示:

@Configuration
public class StaticWithoutHtmlMappingConfigurer extends WebMvcConfigurerAdapter implements WebMvcConfigurer {

    private static final String STATIC_FILE_PATH = "src/main/resources/static";

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        try {
            Files.walk(Paths.get(STATIC_FILE_PATH), new FileVisitOption[0])
                .filter(Files::isRegularFile)
                .map(f -> f.toString())
                .map(s -> s.substring(STATIC_FILE_PATH.length()))
                .map(s -> s.replaceAll("\\.html", ""))
                .forEach(p -> registry.addRedirectViewController(p, p + ".html"));

        } catch (IOException e) {
            e.printStackTrace();
        }

        // add the special case for "index.html" to "/" mapping
        registry.addRedirectViewController("/", "index.html");
    }

}
选项2–如果您希望在没有html的情况下提供服务并通过模板引擎进行解析

将html移动到templates文件夹,启用例如thymeleaf模板,并将配置更改为:

@Configuration
public class StaticWithoutHtmlMappingConfigurer extends WebMvcConfigurerAdapter implements WebMvcConfigurer {

    private static final String STATIC_FILE_PATH = "src/main/resources/static";

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        try {
            Files.walk(Paths.get(STATIC_FILE_PATH), new FileVisitOption[0])
                .filter(Files::isRegularFile)
                .map(f -> f.toString())
                .map(s -> s.substring(STATIC_FILE_PATH.length()))
                .map(s -> s.replaceAll("\\.html", ""))
                .forEach(p -> registry.addViewController(p).setViewName(p));

        } catch (IOException e) {
            e.printStackTrace();
        }

        // add the special case for "index.html" to "/" mapping
        registry.addViewController("/").setViewName("index");
    }

}

除了创建一个
@Controller
@RequestMapping
定义所有方法之外,还有另一种方法更方便,并且在添加或删除html文件时不需要更改

选项1-如果您不介意人们看到.html后缀

将文件保留在静态文件夹中,并向项目中添加
webmvcconfiguer
,如下所示:

@Configuration
public class StaticWithoutHtmlMappingConfigurer extends WebMvcConfigurerAdapter implements WebMvcConfigurer {

    private static final String STATIC_FILE_PATH = "src/main/resources/static";

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        try {
            Files.walk(Paths.get(STATIC_FILE_PATH), new FileVisitOption[0])
                .filter(Files::isRegularFile)
                .map(f -> f.toString())
                .map(s -> s.substring(STATIC_FILE_PATH.length()))
                .map(s -> s.replaceAll("\\.html", ""))
                .forEach(p -> registry.addRedirectViewController(p, p + ".html"));

        } catch (IOException e) {
            e.printStackTrace();
        }

        // add the special case for "index.html" to "/" mapping
        registry.addRedirectViewController("/", "index.html");
    }

}
选项2–如果您希望在没有html的情况下提供服务并通过模板引擎进行解析

将html移动到templates文件夹,启用例如thymeleaf模板,并将配置更改为:

@Configuration
public class StaticWithoutHtmlMappingConfigurer extends WebMvcConfigurerAdapter implements WebMvcConfigurer {

    private static final String STATIC_FILE_PATH = "src/main/resources/static";

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        try {
            Files.walk(Paths.get(STATIC_FILE_PATH), new FileVisitOption[0])
                .filter(Files::isRegularFile)
                .map(f -> f.toString())
                .map(s -> s.substring(STATIC_FILE_PATH.length()))
                .map(s -> s.replaceAll("\\.html", ""))
                .forEach(p -> registry.addViewController(p).setViewName(p));

        } catch (IOException e) {
            e.printStackTrace();
        }

        // add the special case for "index.html" to "/" mapping
        registry.addViewController("/").setViewName("index");
    }

}

不明白你想要完成什么。如果您只需要静态html,那么ApacheHTTPWeb服务器就可以实现这一点。如果您正在寻找一个动态站点,那么带控制器的SpringMVC就是您想要的。@ccit spence,我有RESTSpring引导服务。当然,我还需要返回一些HTML/js文件。我需要URL喜欢,并获得HTML文件admin.HTML,blog.HTML等。我会看看使用控制器个人。这将帮助你更好地管理事情。如果不这样做,您将在html中产生大量代码重复。否定控制器感知到的额外代码。不理解您试图完成的任务。如果您只需要静态html,那么ApacheHTTPWeb服务器就可以实现这一点。如果您正在寻找一个动态站点,那么带控制器的SpringMVC就是您想要的。@ccit spence,我有RESTSpring引导服务。当然,我还需要返回一些HTML/js文件。我需要URL喜欢,并获得HTML文件admin.HTML,blog.HTML等。我会看看使用控制器个人。这将帮助你更好地管理事情。如果不这样做,您将在html中产生大量代码重复。用控制器消除感知到的额外代码。是的,我知道这种方法。但是如果我有很多html页面呢。我应该创建多个样板控制器吗?你可以使用一个控制器和多个方法,每个方法都用不同的@RequestMapping注释。但是如果我有很多html页面呢。我应该创建多个样板控制器吗?你可以使用一个控制器和多个方法,每个方法都用不同的@RequestMapping注释