gpt4 book ai didi

java - 我的 Thymeleaf/HTML 不喜欢 : prefix,,并且当页面处于表单中时不会呈现页面

转载 作者:行者123 更新时间:2023-12-02 10:46:55 25 4
gpt4 key购买 nike

我有一个表单(使用 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 配置文件:

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>

我的表单 Controller :

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";
}

最佳答案

您忘记将其插入到您的 html 页面中:

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

如果没有此声明,您的页面将无法处理 thymelaf。

关于java - 我的 Thymeleaf/HTML 不喜欢 : prefix,,并且当页面处于表单中时不会呈现页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52472846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com