无法从Thymeleaf表单向HTTP请求发布值

时间:2017-02-13 15:13:07

标签: java html spring spring-boot thymeleaf

@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状态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;
    }
}

1 个答案:

答案 0 :(得分:1)

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

控制器:

@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;"/>

这应该可以使它