gpt4 book ai didi

java - 在 Spring boot 中的表单中使用另一个包中的模型 - Java

转载 作者:行者123 更新时间:2023-11-30 02:48:26 26 4
gpt4 key购买 nike

在我的 builds.html 页面中,我有以下形式:

<div id="deployment">
<form action="#" th:action="@{/builds}" th:object="${build}" method="post">
<textarea rows="20" cols="150" th:field="*{configuration}">
</textarea>
<br></br>
<button class="btn btn-primary btn-cons" type="submit">Deploy</button>
</form>
</div>

我的BuildsControler.java位于deployments包中:

import builds.Build;

@Controller
@ComponentScan("builds")
public class BuildsController {

// @Autowired
// private BuildsRepository buildsRepository;
private final static Logger logger = Logger.getLogger(BuildsController.class);

/*
* Add the builds url page
*/
@GetMapping("/builds")
public String index(Model model) {
model.addAttribute("builds", new Build());
return "builds";
}

/**
*
* @param object
* @return
*/
@PostMapping("/builds")
public Build deploy(@ModelAttribute Build build) {
logger.info(String.format("--------- Deploying build: %s --------", build));
return build;
}

}

Build模型位于builds包中时:

@Document(collection = "builds")
public class Build {

@Id
@JsonSerialize(using = ToStringSerializer.class)
private String id;
private String date;

@NotNull
private String configuration;

public Build() {
}

public Build(String configuration) {
this.id = new ObjectId().toHexString();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
this.date = dateFormat.format(new Date());
this.configuration = configuration;
}
}

所以我收到以下错误:

[THYMELEAF][http-nio-8080-exec-1] Exception processing template "builds": Exception evaluating SpringEL expression: "builds.build" (builds:514)
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "builds.build" (builds:514)] with root cause
org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 7): Property or field 'build' cannot be found on object of type 'builds.Build' - maybe not public?

由于 Build 在另一个包中,因此如何在表单中使用 build 模型?

最佳答案

您应该使用build不是builds ,在 GET 方法中添加属性时,因为您使用 build在这里th:object="${build}" ,所以 Spring 找不到它。

model.addAttribute("build", new Build());

此文档 example可能有帮助。

关于java - 在 Spring boot 中的表单中使用另一个包中的模型 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39439242/

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