在 Spring 项目中使用 @ModelAttribute 时出现错误 SpringInputGeneralFieldAttrProcessor
Error SpringInputGeneralFieldAttrProcessor when using @ModelAttribute in Spring project
我正在尝试使用表单更新用户名。问题是当我使用@ModelAttribute 时出现以下错误:
Error during execution of processor
'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor'
(account:79)
来自 IDE 的完整错误信息:
2017-12-30 09:03:16.082 ERROR 6536 --- [nio-8080-exec-7]
o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for
servlet [dispatcherServlet] in context with path [] threw exception
[Request processing failed; nested exception is
org.thymeleaf.exceptions.TemplateProcessingException: Error during
execution of processor
'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor'
(account:79)] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain
target object for bean name 'user' available as request attribute at
org.springframework.web.servlet.support.BindStatus.(BindStatus.java:144)
~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:401)
~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:328)
~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:294)
~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.processAttribute(AbstractSpringFieldAttrProcessor.java:98)
~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.Node.processNode(Node.java:972)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.Node.processNode(Node.java:990)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.Node.processNode(Node.java:990)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.Node.processNode(Node.java:990)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.Node.processNode(Node.java:990)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.Node.processNode(Node.java:990)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.Node.processNode(Node.java:990)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.Node.processNode(Node.java:990)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.Node.processNode(Node.java:990)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.Node.processNode(Node.java:990)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.Node.processNode(Node.java:990)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.dom.Document.process(Document.java:93)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1155)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335)
~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190)
~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] at
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1286)
~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041)
~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)
~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
~[tomcat-embed-websocket-8.5.23.jar:8.5.23] at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:347)
~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:263)
~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)
~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
~[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
[tomcat-embed-core-8.5.23.jar:8.5.23] at
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
[tomcat-embed-core-8.5.23.jar:8.5.23] at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[na:1.8.0_152] at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[na:1.8.0_152] at
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
[tomcat-embed-core-8.5.23.jar:8.5.23] at
java.lang.Thread.run(Thread.java:748) [na:1.8.0_152]
账户管理员class:
@Controller
public class AccountController {
@Autowired
private UserService userService;
@GetMapping("/account")
public ModelAndView adminPage(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("account");
//modelAndView.addObject("users", userService.getAllUsers());
return modelAndView;
}
@PutMapping("/renameUsername")
public ModelAndView renameUserName(@ModelAttribute("user") User newUserName) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String currentUser = auth.getName();
userService.changeUsername(currentUser, newUserName);
return new ModelAndView("account");
}
模型用户class:
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
public class User {
@Id
private String logonName;
private String firstName;
private String lastName;
@JsonIgnore
private String password;
private String recoveryQuestion;
@JsonIgnore
private String recoveryAnswer;
}
负责修改用户名的用户服务方法:
public void changeUsername(String oldUserName, User newUserName) {
User user = userRepository.findOne(oldUserName);
user.setLogonName(newUserName.getLogonName());
userRepository.save(user);
}
account.html 代码片段:
<div class="jumbotron">
<form th:action="@{/renameUsername}" th:method="put" th:object="${user}">
<dl class="row">
<dt class="col-sm-5">
<p>Change username:</p>
<p class="description">Enter your username, which you will use to login</p>
</dt>
<dd class="col-sm-7">
<p>
<label for="inputUser" class="sr-only">username</label>
<input type="text" id="inputUser" class="form-control" name="logonName" th:field="*{logonName}" placeholder="Username" />
</p>
<p>
<button type="submit" class="btn btn-sm btn-warning">Update username</button>a
</p>
</dd>
</dl>
</form>
</div>
我曾尝试使用人们报告类似问题但解决方案不起作用的文章来解决问题。大多数情况下 variable/model 名称存在错字问题。我检查了这个,但我仍然遇到这个问题。
对我收到此错误的原因有何建议?
在你的代码中/modelAndView.addObject("users", userService.getAllUsers());
.
但在你看来 <form th:action="@{/renameUsername}" th:method="put" th:object="${user}">
:
将 users
更改为 user
。
我正在尝试使用表单更新用户名。问题是当我使用@ModelAttribute 时出现以下错误:
Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (account:79)
来自 IDE 的完整错误信息:
2017-12-30 09:03:16.082 ERROR 6536 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (account:79)] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:144) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:401) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:328) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:294) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.processAttribute(AbstractSpringFieldAttrProcessor.java:98) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.Node.processNode(Node.java:972) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.dom.Document.process(Document.java:93) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1155) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011) ~[thymeleaf-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190) ~[thymeleaf-spring4-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1286) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:347) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:263) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.23.jar:8.5.23] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_152] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_152] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.23.jar:8.5.23] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_152]
账户管理员class:
@Controller
public class AccountController {
@Autowired
private UserService userService;
@GetMapping("/account")
public ModelAndView adminPage(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("account");
//modelAndView.addObject("users", userService.getAllUsers());
return modelAndView;
}
@PutMapping("/renameUsername")
public ModelAndView renameUserName(@ModelAttribute("user") User newUserName) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String currentUser = auth.getName();
userService.changeUsername(currentUser, newUserName);
return new ModelAndView("account");
}
模型用户class:
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
public class User {
@Id
private String logonName;
private String firstName;
private String lastName;
@JsonIgnore
private String password;
private String recoveryQuestion;
@JsonIgnore
private String recoveryAnswer;
}
负责修改用户名的用户服务方法:
public void changeUsername(String oldUserName, User newUserName) {
User user = userRepository.findOne(oldUserName);
user.setLogonName(newUserName.getLogonName());
userRepository.save(user);
}
account.html 代码片段:
<div class="jumbotron">
<form th:action="@{/renameUsername}" th:method="put" th:object="${user}">
<dl class="row">
<dt class="col-sm-5">
<p>Change username:</p>
<p class="description">Enter your username, which you will use to login</p>
</dt>
<dd class="col-sm-7">
<p>
<label for="inputUser" class="sr-only">username</label>
<input type="text" id="inputUser" class="form-control" name="logonName" th:field="*{logonName}" placeholder="Username" />
</p>
<p>
<button type="submit" class="btn btn-sm btn-warning">Update username</button>a
</p>
</dd>
</dl>
</form>
</div>
我曾尝试使用人们报告类似问题但解决方案不起作用的文章来解决问题。大多数情况下 variable/model 名称存在错字问题。我检查了这个,但我仍然遇到这个问题。
对我收到此错误的原因有何建议?
在你的代码中/modelAndView.addObject("users", userService.getAllUsers());
.
但在你看来 <form th:action="@{/renameUsername}" th:method="put" th:object="${user}">
:
将 users
更改为 user
。