The server encountered an internal error that prevented it from fulfilling this request

I'm doing an application using Spring MVC and I have a problem when I try to register an bank account to a specific person in my application. In this case, one Person has many accounts:

There are two classes in this relationship:

Sumarizing:

@Entity
public class Pessoa {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int idPessoa;

    @OneToMany(mappedBy = "pessoa", targetEntity = ContaCorretora.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<ContaCorretora> contaCorretora;


@Entity
public class ContaCorretora {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int idConta;
    private TipoConta tipoConta;
    private TipoRisco tipoRisco;
    private String login;
    private String senha;
    private BigDecimal valorAtual;
     @ManyToOne
     @JoinColumn(name="idPessoa")
     private Pessoa pessoa;

Controller Class:

@RequestMapping("/pessoa")
@Controller
public class ContaCorretoraController {


    @Autowired
    private ContaCorretoraDAO contaCorretoraDao;

    @Autowired
    private PessoaDAO pessoaDao;

    @RequestMapping("/pessoacorretora/{id}") 
    public ModelAndView pessoaCorretora(@PathVariable("id") int id, ContaCorretora contaCorretora ) {
        contaCorretora = new ContaCorretora();
        Map<String, Object> model = new HashMap<String, Object>();
        Pessoa pessoa = pessoaDao.find(id);
        model.put("contaCorretora", contaCorretora);
        model.put("pessoa", pessoa);
        model.put("tipoConta", TipoConta.values());
        model.put("tipoRisco", TipoRisco.values());
        return new ModelAndView("pessoa/contacorretora", "model", model);       
    }


    @RequestMapping(value="/pessoacorretora/finalizar", method=RequestMethod.POST) 
    public ModelAndView gravar(ContaCorretora contaCorretora)   {       
        contaCorretoraDao.gravar(contaCorretora);
        return new ModelAndView("redirect:pessoa/contacorretora"); 
    }

I really don't know if the controller class is correct, but probably the problem is there. Nevertheless, I'll paste the JSP code as well:

<form:form action="${s:mvcUrl('CCC#gravar').build() }" method="post" commandName="pessoa" enctype="multipart/form-data" >


    <div class="form-group" >
        <label>Conta</label>
        <select name="tipoConta">
            <c:forEach items="${model.tipoConta}" var="tipoConta">
                  <option value=${tipoConta}>${tipoConta}</option>
            </c:forEach>
        </select>
    </div>

    <div class="form-group" >
        <label>Risco</label>
        <select name="tipoRisco">
            <c:forEach items="${model.tipoRisco}" var="tipoRisco">
                  <option value=${tipoRisco}>${tipoRisco}</option>
            </c:forEach>
        </select>
    </div>

            <div class="form-group">
        <label>Login</label>
        <form:input path="login" cssClass="form-control" /> 

    </div>

    <div class="form-group">
        <label>Senha</label>
        <form:input path="senha" cssClass="form-control" /> 

    </div>

    <div class="form-group">
        <label>Valor Atual</label>
        <form:input path="valorAtual" cssClass="form-control" /> 

    </div>  



        <button type="submit" class="btn btn-primary">Cadastrar</button>
</form:form>

When I try to open the website (..../pessoa/pessoacorretora/6) - 6 is a valid ID, this is the error:

HTTP Status 500 - java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'pessoa' available as request attribute type Exception report message java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'pessoa' available as request attribute description The server encountered an internal error that prevented it from fulfilling this request.

Actually I really don't know how to handle with a relationship of two classes when I want to do CRUD actions.

Upvotes: 0

Views: 1695

Answers (1)

Rafik BELDI
Rafik BELDI

Reputation: 4158

The problem is that spring can't bind the commandName="pessoa" in your JSP form.

Are you sure that the value of Pessoa returnd by :

    Pessoa pessoa = pessoaDao.find(id); // is not Null ??

try this as a test :

    model.put("pessoa", pessoa == null ? new Pessoa() : pessoa );

another way to solve this is to add it through Model Attribute annotation on you controller:

@ModelAttribute("pessoa ")
public Pessoa defaultInstance() {
    return new Pessoa();
}

Upvotes: 1

Related Questions