我一直在努力创建一个使用Thymeleaf的Spring MVC Web应用程序。我不确定使用注释配置它的确切方法。这是我的两个相关课程:
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
注释,但它无法正常工作。我有一个@SpringBootApplication
的应用程序类。我能够运行程序并加载与/ home和/ help对应的页面。但是,一旦我补充说
到home.html页面,我收到了一个错误:
java.langlIllegalStateException: Neither BindingResult nor plain target object for bean 'startHour' available as request attribute
所以,我不确定我在配置类中做错了什么。另外,我不知道如何使用注释和我的配置类实现与此xml here相同的功能。任何提示/帮助将不胜感激,因为即使在查看多个教程之后我也无法弄清楚如何配置它。
编辑:这是我的application.yaml,位于资源目录下。
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>
答案 0 :(得分:1)
@Bean String getStartHour(){return startHour;}
乍一看,可能存在一些问题
@Value("${WebController.startHour}")
对我来说似乎不正确。仅当您的application.yml
文件包含条目WebController.startHour: <someNumber>
@Bean
注释@Autowired private String startHour;
@Autowired WebPageControllerConfig webPageControllerConfig;
model.addAttribute("9", webPageControllerConfig.getstartHour());
对我来说没什么意义..
将其更改为model.addAttribute("startHour", webPageControllerConfig.getStartHour());
并添加
model.addAttribute("endHour", webPageControllerConfig.getEndHour());
model.addAttribute("inputFile", 1);