springboot + thymeelaf使用select和object处理表单

时间:2016-12-11 19:06:27

标签: spring-mvc spring-boot thymeleaf

你好,                我现在面临一个问题 - 好几天 - 我需要保存一个带有select / option的表单,该表单包含对另一个类的引用......但是当控制器试图保存它时失败。控制台没有显示任何内容......任何提示?         白标错误页面

for (i = 0;i < dim;i++)
    free(&words[i]);  

表格cadastro.html

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Dec 11 17:00:10 BRST 2016
There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='consulta'. Error count: 1

ConsultaController

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
...
</head>

<body>
    <div th:replace="includes/header :: menu"></div>
    <div id="consu" class="container">

    <form action="#" th:action="@{salvar}" th:object="${consulta}" method="POST">
    <div class="form-inline">
    <label for="select-medico-consulta" class="form-group">Medico</label>
        <select  id="select-medico-consulta" th:field="*{medico}">
            <div>
            <option th:each="medico : ${medicos}" th:value="${medico}"
                        th:text="${medico.nome}" ></option>
            </div>          
        </select>

        <div class="form-group">
         <label id="paciente-label" for="select-paciente-consulta" > Paciente</label>
        </div>                      
        </div>

   <div class="form-group">
  <label for="comment">Consulta</label>
  <textarea class="form-control" rows="5" id="comment"></textarea>
    </div> 

  <div class="checkbox">
    <label>
      <input type="checkbox"/> Internado 
    </label>
  </div>


  <button type="submit" class="btn btn-default">Salvar</button>
</form>
</div>    


<div th:fragment="footer">

        <p class="rodape">
            <img class="img-responsive" src="/img/logo.jpg"
                th:src="@{/img/logo.jpg}" alt="error" />
        </p>

    </div>
</body>
</html>

Model,consulta.java

@Entity 公共阶层咨询{

@Controller
@RequestMapping("/medclin")
public class ConsultaController {

    @Autowired
    private ConsultaDao consultadao;

    @Autowired
    private MedicoDao medicoDao;

    @Autowired
    private PacienteDao pacienteDao;



    @RequestMapping("/consulta")
    public ModelAndView Consulta() {

        ModelAndView modelAndView = new ModelAndView("consulta/consulta");

        ArrayList<Medico> medicos = (ArrayList<Medico>) medicoDao.findAll();

        ArrayList<Paciente> pacientes = (ArrayList<Paciente>) pacienteDao.findAll();

        modelAndView.addObject("medicos", medicos);
        modelAndView.addObject("pacientes", pacientes);


        modelAndView.addObject("consulta", new Consulta());


        return modelAndView;
    }



    @RequestMapping(value = "/salvar", method = RequestMethod.POST)
    public String salvar(@ModelAttribute Consulta consulta) {


        consultadao.save(consulta);

        return "redirect:medclin/home";
    }

}

// TODO corrigir esse erro

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@ManyToOne
private Medico medico;

@ManyToMany
@JoinTable(name = "CONSULTA_PACIENTES")
private List<Paciente> pacientes = new ArrayList<>();


@ManyToMany
@JoinTable(name = "CONSULTA_RECEITA")
private List<Receita> receita = new ArrayList<>();

public Consulta(Medico med, List<Paciente> paciente) {
    this.medico = med;
    this.pacientes = paciente;
}

public Consulta() {

}

public Consulta(Medico medico2) {
    this.medico = medico2;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public List<Paciente> getPct() {
    return pacientes;
}

public void setPct(List<Paciente> pct) {
    this.pacientes = pct;
}

public Medico getMedico() {
    return medico;
}

public void setMedico(Medico medico) {
    this.medico = medico;
}

public List<Receita> getReceita() {
    return receita;
}

public void setReceita(List<Receita> receita) {
    this.receita = receita;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    result = prime * result + ((medico == null) ? 0 : medico.hashCode());
    result = prime * result + ((pacientes == null) ? 0 : pacientes.hashCode());
    result = prime * result + ((receita == null) ? 0 : receita.hashCode());
    return result;
}

}

1 个答案:

答案 0 :(得分:1)

尝试

    <select  id="select-medico-consulta" th:field="*{medico.id}">
        <option th:each="medicoEntry : ${medicos}" th:value="${medicoEntry.id}"
                    th:text="${medicoEntry.nome}" ></option>
    </select>