【发布时间】:2016-11-01 15:31:05
【问题描述】:
您好,
我正在使用 Spring、Hibernate、ThymeLeaf 等做简单的 Web 应用程序。我对 Spring 4 和 Hibernate 5 有一些经验,但我第一次使用 ThymeLeaf。我需要将输入的值输入到表单对象中并保存在 POJO(会话、数据库)中并在下一页上显示输入的值。希望你能帮助我。
我正在尝试修复它几天。因此,让我向您展示我的来源,而不是一个错误。
WebConfig.class
/**
* Holds resolver's prefix.
*/
private static final String PREFIX = "/WEB-INF/templates/";
/**
* Holds resolver's postfix.
*/
private static final String SUFFIX = ".html";
/**
* Static resources location
*/
private static final String RESOURCE_LOCATION = "/resources/styles/";
/**
* Static resources location
*/
private static final String RESOURCE_HANDLER_STYLE = "/resources/styles/**";
/**
* Holds resolver's encoding.
*/
private static final String UTF8 = "UTF-8";
/**
* Application context
*/
private ApplicationContext applicationContext;
/**
* Sets the applicationContext.
*
* @param applicationContext the application context.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
/**
* Gets viewResolver
*
* @return ViewResolver
*/
@Bean(name = "viewResolver")
public ViewResolver getViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(getTemplateEngine());
resolver.setCharacterEncoding(UTF8);
return resolver;
}
/**
* Gets TemplateEngine
*
* @return the engine.
*/
private TemplateEngine getTemplateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(getTemplateResolver());
return engine;
}
/**
* Gets resolver.
*
* @return resolver.
*/
private ITemplateResolver getTemplateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix(PREFIX);
resolver.setSuffix(SUFFIX);
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCacheable(true);
return resolver;
}
/**
* Sets the path to css,image and js files.
*
* @param registry the registry to set handlers of resource folder.
*/
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry
.addResourceHandler(RESOURCE_HANDLER_STYLE).addResourceLocations(RESOURCE_LOCATION)
.setCachePeriod(86400);
}
ClientForm.class
@NotNull
private int clientId;
@NotNull
@Size(min = 2, max = 45)
private String clientName;
@NotNull
private boolean clientAgreement;
public String toString() {
return "ClientForm [ id = " + this.clientId + ", name = " + this.clientName
+ ", agreement = " + this.clientAgreement + " ]";
}
控制器
private static final String LOGIN_PAGE = "login";
@Autowired
ClientService clientService;
@GetMapping(value = {"/", "/login", "/home", "/index"})
public String getLoginForm(Model model) {
logger.info("GET request to " + LOGIN_PAGE);
model.addAttribute("clientForm", "BLABLABLA");
return LOGIN_PAGE;
}
@PostMapping(value = "/")
public String checkClientInfo(@ModelAttribute(value = "clientForm") @Valid ClientForm clientForm, BindingResult bindingResult) {
logger.info("POST request to " + LOGIN_PAGE);
if (bindingResult.hasErrors()) {
logger.info("POST request to " + LOGIN_PAGE + " has some errors. Check trace.");
return LOGIN_PAGE;
}
logger.info("POST request to " + LOGIN_PAGE + " is done successfully. You are redirecting to /order page.");
return "redirect:/order";
}
login.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Online Shop</title>
<link rel="stylesheet" type="text/css" th:href="@{/resources/styles/style.css}">
</head>
<body onload="document.clientForm.clientName.focus();">
<form th:action="@{/}" th:object="${clientForm}" name="clientForm" action="#" method="POST">
<div>
<h1>Welcome to Online Shop</h1>
<input type="text" th:name="*{clientName}" name="clientName" value='' placeholder="Enter your name"/>
<h1 data-th-text="${clientForm}"></h1>
<p><input type="checkbox" name="checkBoxOption" value="isChecked"/>I agree with the terms of service</p>
<input type="submit" value="Enter">
</div>
</form>
</body>
</html>
还有我的错误(我必须承认 th: 运行良好,css 的 url 有效): error
还有我的 gradle.build 脚本: build
希望有人有什么好主意。 祝你有美好的一天!
【问题讨论】:
标签: java spring hibernate spring-mvc thymeleaf