thymeleaf找不到声明

5kgi1eie  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(315)

我对百里香有意见。在html文件中找不到newuser模型。

<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>

从这个班级:

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";
    }
}

问题是,从其他类和html来看,它工作得很好。
你能告诉我怎么了吗?

nbewdwxp

nbewdwxp1#

在您的loginuser中,请确保添加以下行:

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

或者更改registeruser,只返回视图,不重定向到其他操作:

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

相关问题