Pass JSP model to Springmvc controller

Asked

Viewed 1,762 times

3

Could someone please explain to me how to correct my JSP error?

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

My Product Controller Ntroller.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 mistake that’s coming back to 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>
  • I believe that ${lista} contains a list of some class of yours. I’m not sure, but in this case you do not need to set the attributes itemValue="id" itemLabel="nome" of form:select? According to the documentation, should work, but it doesn’t hurt to try :)

  • I’ve already made the appropriate modifications to my form:select, thank you!

2 answers

2

In accordance with the Taglibs documentation, the attribute path of tags <form:*> must receive an attribute from a Java Bean to make the Binding, that is, the methods getter and Setter shall be invoked to respectively recover and define the value of the bean according to field value.

In other words: nay can do Binding in a direct formwork field with a String, need to be with an object attribute.

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

2

friend, take a look at these examples.

You need to specify an object command in its model there in its controller.

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

Remembering that this object command 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.