Pass JSP model to SpringMVC controller

3

Could someone please explain to me how to fix the error of my JSP?

This form will serve to register products in my system, it receives a list of product categories from the database.

My Controller ProductController.java:

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import br.com.estoque.Dao.CategoriaProdutoDao;
import br.com.estoque.Dao.ProdutoDao;
import br.com.estoque.Modelo.Produto;

@Controller
@RequestMapping("controllerProduto")
public class ProdutoController {

    @RequestMapping("produtoForm")
    public String loginForm(Model model){
        CategoriaProdutoDao dao = new CategoriaProdutoDao();
        model.addAttribute("lista", dao.listar());
        return "produtos/frmCadastroProduto";
    }

    /*@RequestMapping("/home")
    public ModelAndView home(ModelMap model){
        model.put("lista",dao.listar());
        System.out.println("categoria achada");
        return new ModelAndView("categoriaProduto",model);
    }*/

    @RequestMapping(value = "/inserirProduto", method = RequestMethod.POST)
    @ResponseBody()
    public String inserirProduto(@ModelAttribute("inserirProdutoForm")Produto produto, ModelMap model){
        ProdutoDao dao = new ProdutoDao();
        model.addAttribute("categoriaProduto", produto.getCategoriaProduto().getCdCategoriaProduto());
        model.addAttribute("descricao", produto.getDescricao());

        dao.adiciona(produto);
        return "produtoForm";
    }
}

My JSP:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>   
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Registration</title>
    </head>
    <body>
<div align="center">
    <form:form action="/inserirProduto" method="post">

        <table border="0">   

        <tr>
    <td><form:label path="descricao">Nome Produto</form:label></td>
    <td><form:input path="descricao" /></td>
</tr>
<tr>
    <td><form:label path="categoriaProduto">id</form:label></td>
    <td><form:select path="categoriaProduto" items="${lista}"  /></td>
</tr>


            <tr>
                <td colspan="2" align="center"><input type="submit" value="Register" /></td>
            </tr>
        </table>


    </form:form>
</div>
</body>
</html>

And the error that is returning me:

HTTP Status 500 - An exception occurred processing JSP page /WEB-INF/views/produtos/frmCadastroProduto.jsp at line 18

type Exception report

message An exception occurred processing JSP page /WEB-INF/views/produtos/frmCadastroProduto.jsp at line 18

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

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/produtos/frmCadastroProduto.jsp at line 18

15:             <table border="0">   
16:             
17:             <tr>
18:         <td><form:label path="descricao">Nome Produto</form:label></td> 
19:         <td><form:input path="descricao" /></td>
20:     </tr>
21:     <tr>
    
asked by anonymous 16.09.2014 / 06:36

2 answers

2

According to the taglibs documentation a>, the path attribute of the <form:*> tags should be given an attribute of a Java Bean to get binding , that is, getter in> setter will be invoked to respectively retrieve and set the bean value according to the value of the field.

In other words: not can bind in a direct form field with a String , needs to be with an attribute of an object.

Solution : Create a Java Bean containing attributes for form fields to serve as a model .

    
16.09.2014 / 14:47
2

Friend, take a look at these examples .

You need to specify a command object on your model there in your controller.

model.addAttribute("command", new MyObject());

Remembering that this command object must have a method getter and setter for the attribute you specify in path of input .

A very simple example:

Object:

public class Pessoa {

  private String nome;

  public void setNome(String nome) {
    this.nome = nome;
  }

  public String getNome() {
    return nome;
  }
}

Controller:

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

  @RequestMapping("/form")
  public String form(Model model){
    model.addAttribute("command", new Pessoa());
    return "pessoa/form";
  }
}

form.jsp:

<form:form>
    <table>
        <tr>
            <td>Nome:</td>
            <td><form:input path="nome"/></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="Salvar"/>
            </td>
        </tr>
    </table>
</form:form>
    
16.09.2014 / 15:29