我有一个表单(使用Java,Springboot和Thymeleaf),并且不喜欢在输入区域前添加th:前缀。如果th:在html中,则整个页面将不会呈现。如果我将其取出,则页面将呈现,但随后我没有得到实际的客户输入,因为它不能以适当的形式工作。有想法该怎么解决这个吗?我是否缺少依赖项或幕后的东西?
POM文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.Oli</groupId>
<artifactId>ResourceWebsite</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>OliAffiliatePortal</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Thymeleaf Congfig文件:
package com.Oli.ResourceWebsite.configuration;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.spring4.SpringTemplateEngine;
@Configuration
public class ThymeleafConfiguration {
@Bean public ClassLoaderTemplateResolver emailTemplateResolver() {
ClassLoaderTemplateResolver emailTemplateResolver=new ClassLoaderTemplateResolver();
emailTemplateResolver.setPrefix("templates/");
emailTemplateResolver.setTemplateMode("HTML5");
emailTemplateResolver.setSuffix(".html");
emailTemplateResolver.setTemplateMode("XHTML");
emailTemplateResolver.setCharacterEncoding("UTF-8");
emailTemplateResolver.setOrder(1);
return emailTemplateResolver;
}
}
我的HTML /表单
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml" xmlns:modelAttributeth="http://www.w3.org/1999/xhtml" xmlns:form="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8" />
<title>DirectBind</title>
<link th:replace="fragments/header :: head" />
</head>
<body>
<div th:if="${param.sentMessageFail}">
<div class="ui warning message">
<i class="close icon"></i>
<div class="header">
Oops, your message failed to send!
</div>
Please try again, or send us an email at <a href="mailto:InternetBusinessDivision@Heffins.com">InternetBusinessDivision@Heffins.com</a>
</div>
</div>
<div th:if="${param.sentMessage}">
<div class="ui positive message">
<i class="close icon"></i>
<div class="header">
Thank you for your message!
</div>
<p>We'll get back to you as soon as we can.</p>
</div>
</div>
<form:form enctype="multipart/form-data" id="form" class="ui form" th:object="${directBind}" method="POST" th:action="@{/send}">
<div class="field">
<label>Contact Name</label>
<input type="text" th:field="*{contactName}" />
</div>
<div class="field">
<label>Address</label>
<textarea th:field="*{formAddress}"></textarea>
</div>
<div class="two fields">
<div class="field">
<label>Phone Number</label>
<input type="text" th:field="*{phoneNumber}" />
</div>
</div>
<div class="ui fluid buttons">
<a class="ui button" onclick="clearForm()">Clear</a>
<div class="or"></div>
<button class="ui blue button">Send</button>
</div>
</form:form>
</body>
</html>
我的表单控制器:
package com.Oli.ResourceWebsite.controllers;
import com.Oli.ResourceWebsite.models.OliDb.Document;
import com.Oli.ResourceWebsite.models.ResourceWebsite.DirectBind;
import com.Oli.ResourceWebsite.models.ResourceWebsite.Mail;
import com.Oli.ResourceWebsite.services.EmailService;
import com.Oli.ResourceWebsite.services.StorageService;
import com.sun.media.jfxmedia.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.mail.MailException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;
@Controller
public class DirectBindController {
@Autowired private EmailService emailService;
@GetMapping("/directBind")
public String getDirectBind(Model model){
DirectBind directBind = new DirectBind();
return "directBind";
}
@PostMapping(value="/send")
public String send(Model model, @ModelAttribute(value="directBind") DirectBind directBind) {
Mail mail = new Mail();
mail.setFrom("no-reply@hgitservices.com");
mail.setTo(new String[]{"stacief@hgitservices.com"});
mail.setSubject("IME Self Support - User Message");
Map<String, Object> mailModel = new HashMap<String, Object>();
mail.setModel(mailModel);
try {
emailService.sendSimpleMessage(mail, directBind);
} catch (Exception e) {
e.printStackTrace();
return ("redirect:/?sentMessageFail");
} return ("redirect:/?sentMessage");
}
@RequestMapping(value="/email")
public String email() {
return "emailMessage";
}
答案 0 :(得分:1)
您忘记在HTML页面中插入此代码了
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
没有此声明,您的页面将无法处理百里香。