我正在尝试使用 thymeleaf 创建一个包含一系列复选框的表单。我传递给 thymeleaf 模板的对象源包含一个字符串和一个列表。
package com.controller;
import java.util.List;
public class Source {
private String sourceName;
private List<String> testList;
public String getSourceName()
{
return sourceName;
}
public void setSourceName(String name)
{
this.sourceName = name;
}
public List<String> getTestList()
{
return testList;
}
public void setTestList(List<String> list)
{
this.testList = list;
}
}
Run Code Online (Sandbox Code Playgroud)
我使用此 MVC 控制器将源类型的对象传递到模板中。
package com.controller;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
import org.springframework.web.servlet.View;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import …
Run Code Online (Sandbox Code Playgroud) 那是:
<div th:if="${#fields.hasGlobalErrors()}">
和
<ul th:if="${#fields.hasErrors('global')}">
和
<div th:if="${#fields.hasGlobalErrors()}">
当我将它们添加到 HTML 中时,页面甚至不会呈现,更不用说提交表单了。所有示例的结果是:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#fields.hasErrors('global')"
我尝试使用 v2.1.4 和 v.2.1.3 进行此操作并得到相同的错误。Bug 还是我做错了什么?
是的,标签全部闭合且格式正确。是的,该代码位于表单内。是的,表单的所有其他方面都可以在没有全局错误检查的情况下工作。
以下是损坏的 HTML 的简短版本:
<form action="search.html" th:action="@{/auto/search}">
<p th:if="${#fields.hasErrors('global')}" th:errors="*{global}">
Incorrect date
</p>
<input type="text" th:field="${command.stockNumber}" />
<select th:field="*{command.startYear}">
<option value="" th:each="year : ${modelYears}" th:value="${year}"
th:text="${year}"></option>
</select>
</form>
Run Code Online (Sandbox Code Playgroud)
还有控制器..
@RequestMapping(value = "/auto/search", method = RequestMethod.POST)
public String search(@Validated
@ModelAttribute("command")
AutoSearchCommand autoSearchCommand …
Run Code Online (Sandbox Code Playgroud) 可以在Thymeleaf 中创建一个指向 URL 片段标识符的url :
<a th:href="@{/search#item-25(q=${query},all='true')}">
more...
</a>
Run Code Online (Sandbox Code Playgroud)
但是我想动态创建片段标识符(以便我可以链接到#item-n
),例如(不是工作代码):
<a th:href="@{/search#item-${n}(q=${query},all='true')}">
more...
</a>
Run Code Online (Sandbox Code Playgroud)
Thymeleaf 如何实现这一目标?
我有一个基于 Spring MCV 4 + HTML5 + Thymeleaf 的应用程序,我似乎无法在页面上使用 javascript。
网络应用程序的结构是:
webapp
assets
css
img
js
i18n
messages.properties
WEB-INF
html
fragments
common.html
default.html
footer.html
header.html
login.html // exclusively for ../login.html
sidebar.html
home
welcomeSignedIn.html
client
home.html
create.html
Run Code Online (Sandbox Code Playgroud)
在我的登录页面中,由于它是一个单页,背景中只有一张图片,所以我有一段可以完美运行的JS:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/login :: loginFragment">
<meta charset="UTF-8" />
<title><th:text="#{app.name}"></label></th:text>
</head>
<body>
<div class="login-container animated fadeInDown">
<div class="loginbox bg-white">
<form th:action="@{/login}" method="post">
<div class="loginbox-title">SIGN IN</div>
<div class="loginbox-textbox">
<input type="text" id="ssoId" name="ssoId" class="form-control" placeholder="Username" />
</div>
<div class="loginbox-textbox">
<input type="password" id="password" name="password" …
Run Code Online (Sandbox Code Playgroud) 我是百里香新手,我有一个小问题。我成功地从数据库获取数据并显示到表甲酸盐,这里我true/false
从数据库获取数据。表格式中显示的内容相同。
但我想在前端显示true
asyes
和fasle
as 。no
<tbody>
<tr th:each="trn,iterStat : ${trans}">
<td th:text="${trn.txn}">Yes</td>
</tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)
如何更改我的代码?
尝试制作一个按钮的时间比新手想象的要长得多。
我收到的错误消息是:
'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "${id}"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?提前致谢。
我的控制器:
@Controller
public class BuyerController {
private BuyerService buyerService;
@Autowired
public void setBuyerService(BuyerService buyerService){
this.buyerService = buyerService;
}
@RequestMapping("/add-buyer")
public String showBuyerPager(Model model){
List<Buyer> buyers = buyerService.findAllBuyers();
model.addAttribute("buyers", buyers);
model.addAttribute("buyer", new Buyer());
return "add-buyer";
}
@GetMapping("/showBuyerForm")
public String addBuyerForm(Model model){
model.addAttribute("buyer", new Buyer());
model.addAttribute("buyerId", new Buyer().getBuyerId());
return "add-buyer";
}
@PostMapping("/addBuyer")
public String postBuyerForm(@ModelAttribute("buyer") Buyer buyer, Model model){
buyerService.saveBuyer(buyer);
model.addAttribute("buyer", new Buyer());
return "redirect:/";
} …
Run Code Online (Sandbox Code Playgroud) 我有一个form
要修改的对象。该对象可以在两个页面中使用,并具有相同的值。
我想要两个按钮,并根据提交的按钮发送到一页或另一页。
现在我的代码是这样的:
<!-- Make my action dinamically depending on button submitted -->
<form action="#" th:action="@{/action}" th:object="${myObject}"
method="post">
<input type="submit" name="new" value="new"/>
<input type="submit" name="edit" value="edit"/>
</form>
Run Code Online (Sandbox Code Playgroud)
th:action
应该是动态的。
我在控制器中创建了方法,因此根据我可以处理它的操作:
@RequestMapping(value = {"/new", "/edit"}, method = RequestMethod.POST)
public String handlePost(@RequestParam String action, @ModelAttribute MyObject myObject) {
if (action.equals("new")) {
//Make some stuff
return "new";
} else if (action.equals("edit")) {
//Make some stuff
return "edit";
}
return "index";
}
Run Code Online (Sandbox Code Playgroud)
在 Thymeleaf 文档中我看到了类似的东西,但我无法处理它。
更新1
我的对象是一个在我的页面中呈现的 N 列矩阵。如果一切正常,我会编辑其值,但我可以使用新操作添加新列。
我想要两个按钮,根据它们中的哪一个进入新建或进入编辑。对象始终存在。 …
我尝试了 if object.qtdItem == 0
,然后将 TD 的颜色设置为红色:
<td th:style="${obj.qtdItem == 0 ? 'color: red;'}" th:text="${obj.text}"></td>
Run Code Online (Sandbox Code Playgroud)
但这发生了
error: Caused by:
org.springframework.expression.spel.SpelParseException: Expression
[perfil.qtdItem == 0 ? 'red'] @38: EL1044E: Unexpectedly ran out of
input
Run Code Online (Sandbox Code Playgroud) 我的 Thymeleaf 上下文中有一个变量,称为 r。r 有一个返回 URL 的 getUrl,例如 www.a.co 我想创建一个 HTML 锚点http://www.a.co在 Thymeleaf 中是否有更好的方法?我的解决方案如下,但我不太喜欢它。
<a th:href="@{http://{path}(path=${r.url})}">
<span th:text="${r.url}"/>
</a>
Run Code Online (Sandbox Code Playgroud) 我正在尝试从表单执行简单的提交操作。我在我的项目中使用带有百里香叶模板的 Spring Boot 框架。eclipse IDE中使用的语言是java。
我想做的就是从表单中获取 empname 和 empid (参考 Employee 类)并将其存储在 java 对象中。
当我运行应用程序时,应用程序将打开,当我导航到 edit.html 时,我在浏览器中收到此错误消息 -
Whitelabel 错误页面 此应用程序没有 /error 的显式映射,因此您将其视为后备。2018 年 6 月 18 日星期一 16:14:40 EDT 出现意外错误(类型=内部服务器错误,状态=500)。模板解析时发生错误(模板:“类路径资源[templates/edit.html]”)
我还在控制台上收到此错误消息 -
引起:org.springframework.beans.NotReadablePropertyException:bean 类 [com.cardinalcommerce.model.Employee] 的无效属性“empname”:Bean 属性“empname”不可读或具有无效的 getter 方法:是否返回类型getter 与 setter 的参数类型匹配吗?在 org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] 在 org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor. java:612)〜[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]在org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:104)〜[spring-context-5.0.6 .RELEASE.jar:5.0.6.RELEASE] 在 org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:228) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] 在 org .springframework.web.servlet.support.BindStatus.(BindStatus.java:129) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE] 在 org.springframework.web.servlet.support.RequestContext .getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE] 在 org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) 〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 位于 org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:305) 〜[thymeleaf-spring5-3.0.9.RELEASE .jar:3.0.9.RELEASE] 在 org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:252) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在 org .thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:226) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在 org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess( AbstractSpringFieldTagProcessor.java:174) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在 org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) …
thymeleaf ×10
spring ×5
html ×2
java ×2
spring-boot ×2
spring-mvc ×2
bind ×1
checkbox ×1
eclipse ×1
forms ×1
javascript ×1