gpt4 book ai didi

java - 无法从 Thymeleaf 表单将值发布到 HTTP 请求

转载 作者:行者123 更新时间:2023-12-01 08:53:33 30 4
gpt4 key购买 nike

@RequestMapping(value = "/updatestatus", method = RequestMethod.POST)
String updateRequestStatus(Principal principal, @ModelAttribute(value = "requestModel") RequestModel requestModel) {
log.info("Updating request:");
log.info("-------------------------------");
Users manager = usersRepository.findOneByInitialName(principal.getName());
RequestDO request = requestRepository.findOne(requestModel.getRequestId());
Users employee = usersRepository.findOne(request.getUsers().getId());
requestRepository.updateRequestStatusByRequestId(requestModel.getRequestStatus(), requestModel.getRequestId());
// Instead of getting the same RequestDO object from DB, I just updated it's status for using in mail correctly.
request.setStatus(requestModel.getRequestStatus());
log.info("");
/***
Send Notification Mail to Employee
***/
/*mailUtil.sendNotificationEmailWithTemplating(employee, manager, request);*/
return "requests";
}

HTML 表单:

<form role="form" th:action="@{/request/updatestatus}"
th:object="${requestModel}" method="post">
<input type="text" th:value="${request.id}"
th:field="*{requestId}" style="display: none;"/>
<input type="text" th:value="APPROVED" th:field="*{requestStatus}"
style="display: none;"/>
<input type="hidden" th:name="${_csrf.parameterName}"
th:value="${_csrf.token}"/>
<input type="submit" value="Approve Request">
</form>

错误:

HTTP Status 500 - Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/requests.html]")

我尝试将 requestId 设置为当前 requestId,并将 requestStatus 设置为值 APPROVED,但收到 HTTP Status 500 错误。我做错了什么?

堆栈跟踪:完整堆栈跟踪

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/requests.html]")
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:976)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:856)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:841)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:100)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

从表单请求模型:

public class RequestModel {

private Long requestId;
private String selectedDate;
private RequestStatus requestStatus;

public String getSelectedDate() {
return selectedDate;
}

public void setSelectedDate(String selectedDate) {
this.selectedDate = selectedDate;
}

public Long getRequestId() {
return requestId;
}

public void setRequestId(Long requestId) {
this.requestId = requestId;
}

public RequestStatus getRequestStatus() {
return requestStatus;
}

public void setRequestStatus(RequestStatus requestStatus) {
this.requestStatus = requestStatus;
}
}

最佳答案

问题在于 requestStatus 属性,您应该使用 Controller 中相同类型的对象。示例:

Controller :

@RequestMapping(value = "/updatestatus", method = RequestMethod.GET)
public ModelAndView getRequests(){
ModelAndView mav = new ModelAndView("requests");
//your code
mav.addObject("STATUS", RequestStatus.APPROVED);
return mav;
}

在 HTML 中

<input type="text" th:value="${STATUS}" th:field="*{requestStatus}"
style="display: none;"/>

这应该可以正常工作

关于java - 无法从 Thymeleaf 表单将值发布到 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42207334/

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