user812612
user812612

Reputation: 399

Spring form tag error

I think It is easy for a lot of peoples but I don't know what I'm doing wrong.

My controller:

@RequestMapping(value = "/novo", method = RequestMethod.GET)
public ModelAndView novoTimeSheet() {

    Usuario usuario1 = new Usuario(1L,"Leandro1","[email protected]");
    Usuario usuario2 = new Usuario(2L,"Leandro2","[email protected]");
    Usuario usuario3 = new Usuario(3L,"Leandro3","[email protected]");
    List<Usuario> usuarioList = new ArrayList<Usuario>();
    usuarioList.add(usuario1);
    usuarioList.add(usuario2);
    usuarioList.add(usuario3);

    Map<String, Object> model = new HashMap<String, Object>();
    model.put("usuarios", usuarioList);

    return new ModelAndView("timesheetcrud/novo", "timesheetcruddto", model);
}

It's working:

    <td>Usuário :</td>
    <td>
        <select id="usuarios" name="usuarios">
            <c:forEach items="${timesheetcruddto.usuarios}" var="usuario">
                <option value="${usuario.id}"><c:out value="${usuario.nome}"/></option>
            </c:forEach>
        </select>
    </td>

But I'd like to use spring tag and I put this:

    <td>Usuário :</td>
    <td>
        <td>
            <form:select id="id_usuario" path="usuarios">
                <form:option value="0" label="--- Select ---" />
                <form:options items="${usuarioList}"/>
            </form:select>
        </td>
    </td>

It isn't work:

        <td>
            <form:select id="id_usuario" path="usuarios">
                <form:option value="0" label="--- Select ---" />
                <form:options items="${usuarios}"/>
            </form:select>
        </td>

And It's wrong:

org.springframework.beans.NotReadablePropertyException: Invalid property 'usuarios' of bean class [java.util.HashMap]: Bean property 'usuarios' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

I'd like to know what's benefit to use spring tags instead of JSTL.

Thanks

Upvotes: 1

Views: 455

Answers (3)

himanshu bisht
himanshu bisht

Reputation: 79

You can improve your code like

`<form:select path="usuarioList">
   <form:option value="NONE" label="--- Select ---"/>
   <form:options items="${usuarios}" />
</form:select>
`

Also please change your variable name for more suitable one like

     List<Usuario> usuarios= new ArrayList<Usuario>();
    usuarios.add(usuario1);
    usuarios.add(usuario2);
    usuarios.add(usuario3);

    Map<String, Object> model = new HashMap<String, Object>();
    model.put("UserList", usuarios);

It looks much good than earlier

Upvotes: 1

himanshu bisht
himanshu bisht

Reputation: 79

You can do like this

<form:select path="usuarioList"> <form:options items="${usuarios}" /> </form:select>

Upvotes: 1

Sanath
Sanath

Reputation: 4886

usuarios is a list .. for a form, you need to bind an object

object will have a list with getters and setters.

  object.setUsuarioList(usariolist);
  model.put("object", object);

and then

  <form:options items="${object.usuarioList}"/>

Upvotes: 1

Related Questions