我一直在努力创建一个使用 Thymeleaf 的 Spring MVC Web 应用程序。我不确定使用注释配置它的确切方法。这是我的两个相关类(class):
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>
最佳答案
@Bean String getStartHour(){return startHour;}
乍一看,可能存在一些问题
-
@Value("${WebController.startHour}")
对我来说似乎不正确。仅当您的application.yml
时它才会起作用文件有条目WebController.startHour: <someNumber>
- 删除
@Bean
注释 - 在您的 Controller 中,而不是
@Autowired private String startHour;
,做@Autowired WebPageControllerConfig webPageControllerConfig;
- 使用 webPageControllerConfig.getStartHour()
-
model.addAttribute("9", webPageControllerConfig.getstartHour());
对我来说没有多大意义.. 将其更改为model.addAttribute("startHour", webPageControllerConfig.getStartHour());
并添加model.addAttribute("endHour", webPageControllerConfig.getEndHour());
model.addAttribute("inputFile", 1);
关于java - 如何使用注解配置 Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46351456/