【问题标题】:ThymeLeaf 3 + Spring 4 [ ERROR: Can not resolve object in the view ]ThymLeaf 3 + Spring 4 [错误:无法解析视图中的对象]
【发布时间】: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


    【解决方案1】:

    您在getLoginForm() 中的实现看起来有问题。正确的方法是将 form 类 (ClientForm.class) 的对象发送到视图。

    @GetMapping(value = {"/", "/login", "/home", "/index"})
    public String getLoginForm(Model model) {
        logger.info("GET request to " + LOGIN_PAGE);
        model.addAttribute("clientForm", new ClientForm());
        return LOGIN_PAGE;
    }
    

    详细参考请查看demo spring项目here

    【讨论】:

    • 和我一样。我在会话中保存属性。不是吗?
    • 请尝试在您的模型中发送新的 ClientForm()。将“BLABLABLA”更改为新的 ClientForm()
    • 哦,我发现了,我在没有任何改变之前就这样做了。无论如何谢谢你。当我部署我的应用程序时,它可以工作,但没有显示具有错误属性的元素。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 2017-12-08
    • 2014-08-27
    • 2017-07-08
    • 1970-01-01
    相关资源
    最近更新 更多