Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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 如何使用注释配置SpringMVC_Java_Spring Mvc_Thymeleaf - Fatal编程技术网

Java 如何使用注释配置SpringMVC

Java 如何使用注释配置SpringMVC,java,spring-mvc,thymeleaf,Java,Spring Mvc,Thymeleaf,我一直在努力创建一个使用Thymeleaf的springmvcweb应用程序。我不确定使用注释来配置它的确切方式。以下是我的两个相关课程: webPageController.java package webservice; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframew

我一直在努力创建一个使用Thymeleaf的springmvcweb应用程序。我不确定使用注释来配置它的确切方式。以下是我的两个相关课程:

webPageController.java

package webservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import webservice.Config.WebPageControllerConfig;

@Controller
public class webPageController {

    @Autowired
    WebPageControllerConfig webPageControllerConfig;

    @RequestMapping("/home")
    public String home( Model model){
        model.addAttribute("9", webPageControllerConfig.getstartHour());
        return "home";
    }

    @RequestMapping("/help")
    public String help(String name, Model model){
        return "help";
    }

    @RequestMapping("/Navbar")
    public String navbar(String name, Model model) {return "Navbar";}
}
WebPageControllerConfig.java

package webservice.Config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.io.File;

@Configuration
public class WebPageControllerConfig {
    @Value("${WebController.startHour}")
    private String startHour;

    @Value("${WebController.endHour}")
    private String endHour;

    @Value("${WebController.numOfSkus}")
    private int numOfSkus;

    @Value("${WebController.inputFile}")
    private File skusToQuery;

    public String getStartHour(){return startHour;}
}
我尝试了许多不同的方法来获得正确的配置。这是我最近的一次尝试。我想使用
@AutoConfiguration
注释,但它不能正常工作。我有一个带有
@springbootplication
的应用程序类。我能够运行程序并加载与/home和/help对应的页面。然而,一旦我补充说 到home.html页面,我发现一个错误:

java.langlIllegalStateException: Neither BindingResult nor plain target object for bean 'startHour' available as request attribute
因此,我不确定我在配置类中做错了什么。此外,我不知道如何使用注释和配置类实现与此xml相同的功能。如果您有任何提示/帮助,我将不胜感激,因为即使在看了多个教程之后,我也无法确定如何配置它

编辑:这是我的application.yaml,它位于resources目录下

WebController:
    startHour: 9:00 AM
    endHour: 12:00 PM
    numOfSkus: 100
    inputFile: null
这是我的home.html,在参考资料/模板下

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Practice</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="all" href="../static/css/main.css" th:href="@{/css/main.css}" />
</head>
<body>
    <!-- Need to change in order to fit my project -->
    <!-- <form action="#" th:action="@{/home}" th:object="${homePageController}" method="post">-->
        <h1><u>OIC GIV Comparator</u></h1>
        <strong>Start hour:</strong>
        <input type="text" value="8:00 AM" th:field="${startHour}"/>
        <strong>End hour:</strong>
        <input type="text" value="1:00 PM" th:field="${endHour}"/>
        <p><strong>Number of Skus to query (should change to file for input)</strong></p>
        <input type="number" th:field="${inputFile}"/>
        <p><strong>--or--</strong></p>
        <p><strong>Enter file of skus</strong></p>
        <input type="file" th:field="${inputFile}"/>
        <p> <button class="btn btn-default">Start</button> </p>
</body>
</html>

实践
OIC GIV比较器
开始时间:
结束时间:
要查询的SKU数量(应更改为文件以供输入)

--或--

输入SKU文件

开始

乍一看,可能存在两个问题

  • @Value(${WebController.startHour}”)
    对我来说似乎不正确。只有当您的
    应用程序.yml
    文件中有条目
    WebController.startHour:
  • 删除
    @Bean
    注释
  • 在控制器中,而不是
    @Autowired private String startHour
    ,do
    @autowiredWebPageControllerConfig WebPageControllerConfig
  • 使用webPageControllerConfig.getStartHour()
  • model.addAttribute(“9”,webPageControllerConfig.getstartHour())对我来说没有多大意义。。
    将其更改为
    model.addAttribute(“startHour”,webPageControllerConfig.getStartHour())
    model.addAttribute(“endHour”,webPageControllerConfig.getEndHour())
    model.addAttribute(“inputFile”,1)

  • 另外,我不确定为什么@Value(${WebController.starthour})没有抛出错误。我的yaml文件是application.yamlwhat是你的应用程序的内容。yamlI现在觉得很愚蠢。我意识到WebController部分引用的是我的yaml,这就是为什么它没有抛出任何错误。我把我的yaml贴在上面,这是我们大家学习的方式。所以,我用你的建议更新了我的问题。然而,我仍然收到相同的错误。是的,我试图用默认值9将变量添加到模型中。所以,我注释掉了model.addAttribute(“9”,webPageControllerConfig.getStartHour());您仍然看到相同的错误或错误消息更改?是的,是相同的错误。这可能是由于没有配置属性spring-teleaf前缀和/或后缀造成的吗?请尝试添加model.addAttribute(“startHour”,webPageControllerConfig.getStartHour());和model.addAttribute(“endHour”,webPageControllerConfig.getEndHour());和model.addAttribute(“inputFile”,1);
    @Bean
    String getStartHour(){return startHour;}