我一直在創建使用Thymeleaf一個Spring MVC Web應用程序配置掙扎Spring MVC的。我不確定使用註釋來配置它的確切方式。這裏是我的兩個相關類在這裏:如何使用註釋
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>
另外,我不確定爲什麼@Value(「$ {} WebController.starthour」)不會引發錯誤我YAML文件application.yaml –
什麼是我現在覺得自己很蠢,你application.yaml –
的內容。我意識到WebController部分引用回我的yaml,這就是爲什麼它不會拋出任何錯誤。我貼我的YAML上述 –