Spring boot SpelEvaluationException:属性或字段';用户名';在null上找不到
我的项目基于springboot和bootstrap,我将保留用户名 当一个人错误地登录时,但我不想使用ajax。所以我使用thymeleaf来显示来自模型的消息。这是我的项目入口:Spring boot SpelEvaluationException:属性或字段';用户名';在null上找不到,spring-boot,thymeleaf,Spring Boot,Thymeleaf,我的项目基于springboot和bootstrap,我将保留用户名 当一个人错误地登录时,但我不想使用ajax。所以我使用thymeleaf来显示来自模型的消息。这是我的项目入口: @Configuration public class WebMvcConfigure extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) {
@Configuration
public class WebMvcConfigure extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
ModelMap modelMap = new ModelMap();
modelMap.addAttribute("user", new User());
registry.addViewController("/").setViewName("pages/admin3/login_soft");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
super.addViewControllers(registry);
String os = System.getProperty("os.name");
if(os.toLowerCase().startsWith("win")){
Browse("http://localhost:8085");
}
}
这是我的控制器:
@RestController
@RequestMapping("user")
public class UserController extends BaseController {
@Autowired
UserRepository userRepository;
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView login(@ModelAttribute User user) {
User byUsernameOrEmail =
userRepository.findUserByUsernameOrEmail(user.getUsername(),
user.getUsername());
ModelAndView mv = new ModelAndView();
mv.addObject("user", user);
if (CommonUtils.isEmpty(byUsernameOrEmail)) {
mv.addObject("message", ExceptionMsg.LoginNameNotExist.getMsg());
mv.setViewName("redirect:/");
return mv;
} else if (!PasswordUtils.getMD5(user.getPassword() +
byUsernameOrEmail.getSalt())
.equals(byUsernameOrEmail.getPassword())) {
mv.addObject("message",ExceptionMsg.LoginNameOrPassWordError.getMsg());
mv.setViewName("redirect:/");
return mv;
}
mv.setViewName("pages/admin3/login_soft");
return mv;
}
这是我的html:
<form class="login-form" th:action="@{/user/login}" th:object="${user}" method="post" >
<h3 class="form-title">登录</h3>
<div class="alert alert-danger" th:if="${#httpServletRequest.getParameter('message')}">
<button class="close" data-close="alert"></button>
<span th:text="${#httpServletRequest.getParameter('message')}"></span>
</div>
<div class="form-group">
<!--ie8, ie9 does not support html5 placeholder, so we just show field title for that request.getCookies()-->
<label class="control-label visible-ie8 visible-ie9">Username</label>
<div class="input-icon">
<i class="fa fa-user"></i>
<input class="form-control placeholder-no-fix" type="text" autocomplete="off" placeholder="用户名|邮箱"
name="username" th:value="${user.username}" />
</div>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Password</label>
<div class="input-icon">
<i class="fa fa-lock"></i>
<input class="form-control placeholder-no-fix" type="password" autocomplete="off" placeholder="密码" name="password"/>
</div>
</div>
<div class="form-actions">
<label class="checkbox">
<input type="checkbox" name="remember" value="1"/> 记住密码 </label>
<button type="submit" class="btn blue pull-right">
登录 <i class="m-icon-swapright m-icon-white"></i>
</button>
</div>
</form>
我搜索了许多其他问题,他们建议将[用户]放入模型,我是如何做到的,为什么它仍然显示此错误 您在th:objectth:object=“${user}”
尝试使用其他方法重命名用户对象,并确保传递给模型的对象已正确填充
类似于mv.addObject(“userDetails”,user)代码>然后在html中:
<input class="form-control placeholder-no-fix" type="text" autocomplete="off" placeholder="用户名|邮箱" name="username" th:value="${userDetails.username}"/>
您还在th:objectth:object=“${user}”中使用user
尝试使用其他方法重命名用户对象,并确保传递给模型的对象已正确填充
类似于mv.addObject(“userDetails”,user)代码>然后在html中:
<input class="form-control placeholder-no-fix" type="text" autocomplete="off" placeholder="用户名|邮箱" name="username" th:value="${userDetails.username}"/>