简体   繁体   English

Thymeleaf 找不到声明

[英]Thymeleaf cannot find declaration

I have a problem with thymeleaf.我对 thymeleaf 有疑问。 It can not find newUser model in the html file.在 html 文件中找不到 newUser model。

<form th:action="@{/users/add}" th:object="${newUser}" method="post">
    <p>User Name: <input type="text" th:field="*{userName}"></p>
    <p>Email:     <input type="text" th:field="*{email}"></p>
    <p>Password:  <input type="text" th:field="*{password}"></p>
    <p>Role:
        <select th:field="*{role}">
            <option th:each="role: ${roles}" th:value="${role.getId()}" th:utext="${role.getUserRole()}">
            </option>
        </select>
    </p>
    <p><input type="submit" value="Add User"></p>
</form>

From this class:从此 class :

package Application.Controllers;

@Controller
@RequestMapping("/")
public class TemplateController {

    private final RoleService roleService;

    @Autowired
    public TemplateController(RoleService roleService) {
        this.roleService = roleService;
    }

    @GetMapping("register")
    public String registerUser(Model model){
        model.addAttribute("roles", roleService.listRoles());
        model.addAttribute("newUser", new Users());
        return "redirect:login";
    }
}

The problem is that from other class and html it works fine.问题是从其他 class 和 html 可以正常工作。

Could you tell me what is wrong?你能告诉我有什么问题吗?

In your LoginUser make sure to add this line:在您的 LoginUser 确保添加此行:

 model.addAttribute("roles", roleService.listRoles());
 model.addAttribute("newUser", new Users());

Or change the registerUser return just the view don't redirect to another action:或者更改 registerUser 返回只是视图不要重定向到另一个操作:

   @GetMapping("register")
    public String registerUser(Model model){
        model.addAttribute("roles", roleService.listRoles());
        model.addAttribute("newUser", new Users());
        return "login";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM