Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring bean name';的BindingResult或普通目标对象;客户';可用作请求属性_Spring_Thymeleaf - Fatal编程技术网

Spring bean name';的BindingResult或普通目标对象;客户';可用作请求属性

Spring bean name';的BindingResult或普通目标对象;客户';可用作请求属性,spring,thymeleaf,Spring,Thymeleaf,我面临spring和thymeleaf的问题,我试图用一个名为cliente的实体的数据填充表单,但我在浏览器中收到一条白标签错误页面消息,在控制台中收到这条消息 Neither BindingResult nor plain target object for bean name 'cliente' available as request attribute 这是客户 package com.bolsadeideasspringboot.app.models.entity; import

我面临spring和thymeleaf的问题,我试图用一个名为cliente的实体的数据填充表单,但我在浏览器中收到一条白标签错误页面消息,在控制台中收到这条消息

Neither BindingResult nor plain target object for bean name 'cliente' available as request attribute
这是客户

package com.bolsadeideasspringboot.app.models.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import org.springframework.format.annotation.DateTimeFormat;

@Entity
@Table(name="clientes")
public class Cliente implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @NotEmpty
    private String nombre;
    @NotEmpty
    private String apellido;
    @NotEmpty
    @Email
    private String email;
    @Column(name="create_at")
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern="dd/MM/yyyy")
    private Date createAt;


    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getNombre() {
        return nombre;
    }
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
    public String getApellido() {
        return apellido;
    }
    public void setApellido(String apellido) {
        this.apellido = apellido;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Date getCreateAt() {
        return createAt;
    }
    public void setCreateAt(Date createAt) {
        this.createAt = createAt;
    }

}
这是控制器方法

@RequestMapping(value="/form/{id}")
    public String editar(@ModelAttribute("form") @PathVariable(value="id")Long id, Map<String, Object>model) {
    Cliente cliente = null;

    if(id > 0) {
        clientedao.findOne(id);
        model.put("cliente", cliente);
        model.put("titulo", "Editar Cliente");
        return "form";
    }
    else {
        return "redirect:/listar";
    }

}
这是Dao接口方法

public Cliente findOne(Long id);
这是表格

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <title th:text="${titulo}">Insert title here</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

</head>
<body>

    <div class="container">

        <div class="row">
            <div class="col-md-4 col-md-offset-3">
                <h1 class="text-success" th:text="${titulo}"></h1>
                <form th:action="@{/form}" th:object="${cliente}" method="post">

                    <div class="form-group">
                        <label>Nombre</label>
                        <input class="form-control" type="text" th:field="*{nombre}" placeholder="Nombre"/>
                        <small class="form-text text-danger" th:if="${#fields.hasErrors('nombre')}" th:errors="*{nombre}"></small>
                    </div>

                    <div class="form-group">    
                        <label>Apellido</label>
                        <input class="form-control" type="text" th:field="*{apellido}" placeholder="Apellido"/>
                        <small class="form-text text-danger" th:if="${#fields.hasErrors('apellido')}" th:errors="*{apellido}"></small>
                    </div>
                    <div class="form-group">    
                        <label>Email</label>
                        <input class="form-control" type="text" th:field="*{email}" placeholder="correo@ejemplo.com"/>
                        <small class="form-text text-danger" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></small>
                    </div>
                    <div class="form-group">    
                        <label>Fecha</label>
                        <input class="form-control" type="text" th:field="*{createAt}" placeholder="DD/MM/YYYY"/>
                        <small class="form-text text-danger" th:if="${#fields.hasErrors('createAt')}" th:errors="*{createAt}"></small>
                    </div>  
                    <div class="form-group">    
                        <input class="btn btn-primary" type="submit" value="Crear Cliente" />
                    </div>

                    <input type="hidden" th:field="*{id}" />
                </form>
            </div>
        </div>
    </div>

</body>
</html>

在此处插入标题
名义
阿佩利多
电子邮件
德国福查
我正在控制器方法中设置客户机,并且在表单中使用th:object,因此我不知道我做错了什么,任何帮助都会很有帮助,感谢您的建议,而不是
put()
,尝试使用推荐的方法使用

model.addAttribute(“cliente”,clientedao.findOne(id))

您通常还希望使用
@GetMapping
请求填充表单。并使用
@PostMapping
进行提交

旁白:还可以看看ProjectLombok,让您的bean不那么容易出错,更具可读性。您可以删除所有这些getter和setter,只需使用
@Data
注释类


虽然没有被弃用,但您也希望不再使用
java.util.Date
,而是使用更新的日期/时间类。

谢谢兄弟,但我已经解决了这个问题,问题是,我没有将(clientedao.findOne(id))的值赋给clienter变量,但我会给您一个可接受的答案(我不知道如何结束主题xd)你是对的!无论如何,我都会为未来的读者更新答案
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <title th:text="${titulo}">Insert title here</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

</head>
<body>

    <div class="container">

        <div class="row">
            <div class="col-md-4 col-md-offset-3">
                <h1 class="text-success" th:text="${titulo}"></h1>
                <form th:action="@{/form}" th:object="${cliente}" method="post">

                    <div class="form-group">
                        <label>Nombre</label>
                        <input class="form-control" type="text" th:field="*{nombre}" placeholder="Nombre"/>
                        <small class="form-text text-danger" th:if="${#fields.hasErrors('nombre')}" th:errors="*{nombre}"></small>
                    </div>

                    <div class="form-group">    
                        <label>Apellido</label>
                        <input class="form-control" type="text" th:field="*{apellido}" placeholder="Apellido"/>
                        <small class="form-text text-danger" th:if="${#fields.hasErrors('apellido')}" th:errors="*{apellido}"></small>
                    </div>
                    <div class="form-group">    
                        <label>Email</label>
                        <input class="form-control" type="text" th:field="*{email}" placeholder="correo@ejemplo.com"/>
                        <small class="form-text text-danger" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></small>
                    </div>
                    <div class="form-group">    
                        <label>Fecha</label>
                        <input class="form-control" type="text" th:field="*{createAt}" placeholder="DD/MM/YYYY"/>
                        <small class="form-text text-danger" th:if="${#fields.hasErrors('createAt')}" th:errors="*{createAt}"></small>
                    </div>  
                    <div class="form-group">    
                        <input class="btn btn-primary" type="submit" value="Crear Cliente" />
                    </div>

                    <input type="hidden" th:field="*{id}" />
                </form>
            </div>
        </div>
    </div>

</body>
</html>