Spring Thymeleaf classappend не добавление классов - PullRequest
0 голосов
/ 25 февраля 2019

Я пытаюсь добавить несколько классов в зависимости от ошибок, обнаруженных в процессе проверки формы.Но по какой-то причине это не добавление классов.Мой CSS правильный, так как он работает без использования проверки ошибок classappend, а также моя проверка правильна, поскольку он обнаружит ошибку в форме, но не добавит классы CSS.Конкретный метод используется updateRole () в классе RoleController.Ниже приведен весь мой код:

RoleController.java

@Controller
public class RoleController {

    @Autowired
    private RoleServiceImpl roleService;

    @RequestMapping("/roles")
    public String viewAllRoles(ModelMap model) {
        List<Role> roles = roleService.findAll();
        model.put("roles", roles);

        if (!model.containsAttribute("role")) {
            model.put("role", new Role());
        }

        return "roles";
    }


    @RequestMapping(value = "/roles", method = RequestMethod.POST)
    public String addRole(@Valid Role role, BindingResult result, RedirectAttributes redirectAttributes) {

        if (result.hasErrors()) {
            // Add the error message as a flash attribute
            redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.role", result);
            // Add the role object created so the user can see their inital input after redirect
            redirectAttributes.addFlashAttribute("role", role);

            return "redirect:/roles";
        }

        roleService.save(role);

        redirectAttributes.addFlashAttribute("flash", new FlashMessage("Role successfully added", FlashMessage.Status.SUCCESS));


        return "redirect:/roles";
    }

    @RequestMapping(value = "/roles/{roleId}")
    public String viewRole(@PathVariable Long roleId, ModelMap model) {
        Role role = roleService.findById(roleId);
        model.put("role", role);

        return "role";
    }

    @RequestMapping(value = "/roles/{roleId}", method = RequestMethod.POST)
    public String updateRole(@Valid Role role, BindingResult result, RedirectAttributes redirectAttributes) {

        if (result.hasErrors()) {
            redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.role", result);
            redirectAttributes.addFlashAttribute("role", role);
            return String.format("redirect:/roles/%s/edit", role.getId());
        }

        roleService.save(role);
        redirectAttributes.addFlashAttribute("flash", new FlashMessage("Role successfully updated", FlashMessage.Status.SUCCESS));

        return "redirect:/roles";
    }

    @RequestMapping(value = "/roles/{roleId}/edit")
    public String editRole(@PathVariable Long roleId, ModelMap model) {
        Role role = roleService.findById(roleId);
        model.put("action", String.format("/roles/%s", roleId));
        model.put("role", role);

        return "role_edit";
    }

}

role_edit.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head th:replace="base :: head('role_edit')"></head>
    <body>
        <header th:replace="base :: header"></header>
        <nav>
            <ul>
                <li><a th:href="@{projects}">Projects</a></li>
                <li><a th:href="@{collaborators}">Collaborators</a></li>
                <li class="selected"><a th:href="@{roles}">Roles</a></li>
            </ul>
        </nav>
        <section>
            <div class="container wrapper">
                <form th:action="@{${action}}" method="post" th:object="${role}">
                    <input type="hidden" th:field="*{id}">
                    <div class="edit-input" th:classappend="${#fields.hasErrors('name')}? 'edit-error test' : ''">
                        <label> Role Name:</label>
                        <input type="text" th:field="*{name}">
                    </div>
                    <div th:class="${#fields.hasErrors('name')}? 'error' : ''">
                        <div class="testtwo error-message" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></div>
                    </div>
                    <div class="actions">
                        <button class="button" type="submit" value="Submit">Save</button>
                        <a th:href="@{|/roles/*{id}|}" class="button button-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </section>
    </body>
</html>

Если бы кто-то мог помочь, это было бы здорово, поскольку я действительнозастрял на этом.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...