我在评估SpringEL表达式的异常时遇到了一些麻烦:“#fields.hasErrors ..”

时间:2018-07-19 16:48:56

标签: java html spring maven

因此,当我在代码中使用“ redirect:/ index”时;一切正常,但当我使用返回“索引”;我有这个错误:

异常评估SpringEL表达式:“#fields.hasErrors('author')”(索引)

一个不知道为什么。...

哦,我从控制台收到此错误:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'ticketForm' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE]

/

@Controller
public class LoginController {

@PostMapping("/")
public String log(@ModelAttribute("loginForm") @Valid LoginForm form, BindingResult result, Model model) {
    if (result.hasErrors()) {
        return "login";
    }
    if (form.getLogin().equals("admin") || form.getPassword().equals("admin")) {

        return "index";
    } else {

        model.addAttribute("infoLoginError", "Login Error");
        return "login";
    }

}


@GetMapping("/")
public String log(Model model, LoginForm form) {
    model.addAttribute("loginForm", new LoginForm());
    return "login";
  }
}

还有我的下一堂课

@Controller
public class MainController {
@Autowired
TicketRepository ticketRepository;


@GetMapping("/index")
public String index(Model model) {
    model.addAttribute("ticketForm", new TicketForm());
    return "index";
}

@PostMapping("/index")
public String index(@ModelAttribute("ticketForm")@Valid TicketForm form, BindingResult result, Model model) {
    if (result.hasErrors()){
        return "index";
    }else{
        ticketRepository.save((new TicketModel(form)));
        return "index";
    }

}
}

还有我的下一堂课

public class LoginForm {

@NotBlank
private String login;
@NotBlank
private String password;

public LoginForm(String login, String password) {
    this.login = login;
    this.password = password;
}

public LoginForm() {

}

public String getLogin() {
    return login;
}

public void setLogin(String login) {
    this.login = login;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}
}

还有我的下一堂课

public class TicketForm {


@Getter
@Setter
@NotBlank
@Size(min = 3, max = 40)
private String author;
@Getter
@Setter
@NotBlank
@Size(min = 3, max = 500)
private String message;

}

但也许我在html文件中做错了事

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>

<body>
<center>
    <div class="container">
        <form th:action="@{/index}"  th:object="${ticketForm}" method="post">
            <div class="row">
                <div class="col-25">

                    <label>Author:</label>
                    <br>
                    <span th:if="${#fields.hasErrors('author')}" th:errors="*{author}" style="color: red"></span>
                </div>
                <div class="col-75">

                    <input placeholder="Name & Lastname" type="text"
                           th:field="*{author}">
                </div>
            </div>
            <div class="row">
                <div class="col-25">
                    <label>Message:</label>
                    <br>
                    <span th:if="${#fields.hasErrors('message')}" th:errors="*{message}" style="color: red"></span>

                </div>
                <div class="col-75">

                    <textarea  placeholder="Message" type="text" th:field="*{message}" style="height:200px"
                              th:field="*{message}"></textarea>
                </div>
            </div>
            <div class="row">
                <input type="submit"  value="send" >
            </div>

        </form>
    </div>



</center>


<style>
* {
    box-sizing: border-box;
}


input[type=text], select, textarea {
    width: 100%;
    padding: 12px;
    border: 1px solid #ccc;
    border-radius: 4px;
    resize: vertical;
}

label {
    font-size: 20px;
    padding: 12px 12px 12px 0;
    display: inline-block;
}

input[type=submit] {
    background-color: #4CAF50;
    color: white;
    padding: 12px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    float: right;
}

input[type=submit]:hover {
    background-color: #45a049;
}

.container {
width: 70%;
    border-radius: 5px;
    background-color: #f2f2f2;
    padding: 20px;
}

.col-25 {
    float: left;
    width: 25%;
    margin-top: 6px;
}

.col-75 {
    float: left;
    width: 75%;
    margin-top: 6px;
}


.row:after {
    content: "";
    display: table;
    clear: both;
}


@media screen and (max-width: 600px) {
    .col-25, .col-75, input[type=submit] {
        width: 100%;
        margin-top: 0;
    }
}



</style>
</body>
</html>

和我的下一个html文件

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<center><span><h1>Witamy w systemie Logowania</h1></span></center>
</br>
<center><span th:text="${info}"></span></center>
<center><span th:text="${infoLoginError}" th:style="'color: red'"></span></center>
</br>
<form th:action="@{/}" method="post" th:object="${loginForm}">
    <p th:if="${#fields.hasErrors('login')}" th:errors="*{login}"></p>
    Login: <input type="text" th:field="*{login}">
    <p></p>
    <p th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></p>
    Password: <input type="password" th:field="*{password}">
    <p></p>
    <input type="submit" value="send">
</form>



</body>
</html>

0 个答案:

没有答案