Boa tarde, gostaria de saber como é utilizado a tag importada da seguinte maneira
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
porque no meu projeto acaba sendo lançado um Bad Request, toda vez que utilizo os checkboxs.
Aqui está o codigo JSP, Controller e Model:
JSP
<form:form action="${s.mvcUrl('AC#formCadastra').build() }" commandName="usuario" method="POST">
<table class="table-bordered">
<tr>
<td>User:</td>
<td><form:input type='text' path='username'/></td>
</tr>
<tr>
<td>Password:</td>
<td><form:input type='password' path ='password' /></td>
</tr>
<tr>
<td>Nome:</td>
<td><form:input type='text' path='nome' /></td>
</tr>
<tr>
<td>Restrição</td>
<td>
<form:checkboxes items="${usuario.authorities}" itemLabel="nome" path="authorities" delimiter="<br/>"/>
</td>
<tr>
<td colspan='2'><input name="submit" type="submit" value="Enviar" /></td>
</tr>
</table>
</form:form>
Controller
@RequestMapping(value = "/cadastra", method = RequestMethod.GET)
public ModelAndView cadastra() {
ModelAndView modelAndView = new ModelAndView();
Usuario usuario = new Usuario();
List<Role> roles = new ArrayList<>();
for(RolesEnum role: RolesEnum.values()){
Role modelo = new Role();
modelo.setNome(role.name());
roles.add(modelo);
}
usuario.setAuthorities(roles);
modelAndView.addObject("usuario", usuario);
return modelAndView;
}
@RequestMapping(method = RequestMethod.POST)
public ModelAndView formCadastra(Usuario usuario){
//Imprime no console todas as infos do Usuario
System.out.println(usuario.getNome());
System.out.println(usuario.getUsername());
System.out.println(usuario.getPassword());
for(Role role:usuario.getAuthorities()){
System.out.println(role);
}
ModelAndView modelAndView = new ModelAndView("redirect:/");
for(Role role:usuario.getAuthorities()){
role.setUsuario(usuario);
}
usuario.setPassword(bCryptPasswordEnconder.encode(usuario.getPassword()));
usuarioDao.cadastra(usuario);
return modelAndView;
}
Model
public class Usuario implements UserDetails{
private static final long serialVersionUID = 7659681460734527479L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_usuario")
private long id;
@OneToMany(fetch=FetchType.EAGER,mappedBy="usuario")
private List<Role> authorities;
private String username;
private String password;
private String nome;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public List<Role> getAuthorities() {
return authorities;
}
public void setAuthorities(List<Role> authorithies) {
this.authorities = authorithies;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}