2
I am developing a web app in java using the Spring MVC framework. However, a part of the project has arrived that I am breaking my head!.
Before I will show the screen print to facilitate understanding.
Imagery:
What I want to do is this: when the user selects the company, the following elements (salary, 13th salary and income report) will be dynamically charged. Not all companies have all the options. Obs: That button generate demo will no longer exist.
After some research I thought of doing so:
Create a javascript function to take the select action and send the selected item via ajax. As I understand it, I can use Annotation
@RequestBody
to read thejson
sent by ajax.With the selected item in hand, I can pass it as a parameter in the select I do in the bank that brings the options available to that company.
with the Annotation
@ResponseBody
sending onejson
with options and create tags with javascript.
I don’t know if the logic is right, but the problem with that is that I can’t do.
My doubts:
How do I retrieve the selected item and send it to the spring controller? For example: I have a company object that has the name and CNPJ attributes, as I take the CNPJ attribute of the selected item and send to controller?
Then as I return to the page the items available to that company?
Check out my controller’s method:
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Model model,
@ModelAttribute("usuarioLogado") Usuario usuario,
Empresa empresa) {
Funcionario funcionario = daoFuncionario.getFuncionario(usuario);
List<Empresa> empresas = daoEmpresa.listaEmpresas(funcionario);
model.addAttribute("empresa", empresa);
model.addAttribute("funcionario", funcionario);
model.addAttribute("empresas", empresas);
return "usuario/menu";
}
Now the page:
<form:form commandName="empresa" class="form-horizontal">
<fieldset>
<legend>Olá, ${funcionario.nome}</legend>
<!-- Select Empresa -->
<div class="form-group">
<label for="empresa">Empresa </label>
<!--
<select class="form-control">
<c:forEach items="${empresas}" var="empresa">
<option value="${empresa.cnpj}">
${empresa.cnpjFormatado}- ${empresa.razaoSocial}
</option>
</c:forEach>
</select>
-->
<form:select path="razaoSocial" class="form-control">
<form:options items="${empresas}" />
</form:select>
I also don’t know the best way to select. The first way using c:forEach
or using spring select itself.
EDIT
Guys, I’m getting the selected item in the controller like this:
$(document).ready(function() {
function empresaSelecionaClick() {
var cnpj = $("#razaoSocial option:selected" ).val();
var json = {cnpj : cnpj}
$.ajax({
type: 'POST',
url: 'menuDinamico',
contentType: 'application/json',
data: JSON.stringify(json),
success : function(resposta){
// pegar a lista e montar os elementos
}
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
});
}
$("#razaoSocial").change(empresaSelecionaClick);
});
Methods in the controller:
@RequestMapping(value = "/menu", method = RequestMethod.GET)
public @ResponseBody ModelAndView menu(@ModelAttribute("usuarioLogado") Usuario usuario, Empresa empresa) {
ModelAndView mav = new ModelAndView("usuario/menu");
Funcionario funcionario = daoFuncionario.getFuncionario(usuario);
mav.addObject("funcionario", funcionario);
mav.addObject("empresa", empresa);
mav.addObject("empresas", daoEmpresa.listaEmpresas(funcionario));
return mav;
}
@RequestMapping(value = "/menuDinamico", method = RequestMethod.POST)
private @ResponseBody List<UltimoPeriodoAberto> listaOpcoes(@RequestBody Empresa empresa) {
List<UltimoPeriodoAberto> opcoes = daoUltimoPeriodoAberto.getPeriodoHolerite(empresa);
System.out.println("CNPJ empresa: " + empresa.getCnpj());
for (UltimoPeriodoAberto ultimoPeriodoAberto : opcoes) {
System.out.println(ultimoPeriodoAberto.getDescricao());
}
return opcoes;
}
But in the second method is giving null Pointer execption on the line:
List<UltimoPeriodoAberto> opcoes = daoUltimoPeriodoAberto.getPeriodoHolerite(empresa);
I can’t identify the mistake.
EDIT
Well, I did it. I did it like this
I changed the note @RequestBody
for @ModelAttribute
:
@RequestMapping(value = "/menuDinamico", method = RequestMethod.POST)
public @ResponseBody List<UltimoPeriodoAberto> opcoesDinamicas(@ModelAttribute("empresa") Empresa empresa) {
System.out.println(empresa.getCnpj());
List<UltimoPeriodoAberto> opcoes = daoUltimoPeriodoAberto.getPeriodoHolerite(empresa);
System.out.println(opcoes.size());
return opcoes;
}
I changed the ajax request too..
$(document).ready(function() {
function empresaSelecionaClick() {
var empresa = $(this).serialize();
$.ajax({
type: 'POST',
url: 'menuDinamico',
data: empresa,
})
.done(function(data) {
console.log("success");
console.log(data)
})
.fail(function() {
console.log("error");
});
}
$("#cnpj").change(empresaSelecionaClick);
});
Now I’m getting the bug 406
in ajax:
The Resource identified by this request is only capable of generating Responses with Characteristics not acceptable According to the request "Accept" headers
ajax is not getting my list, anyone has any suggestions?
Felipe, in your query you will pass what as parameter for query? ID, CNPJ? Expect as a result a JSON, for example? If it is, I make an example for you, just let me know the version of Spring is using.
– Bruno César
I am passing as parameter the CNPJ. The query I am able to do, I believe that my problem is in receiving the return of the ajax request. For example, I should return a list of objects, and assemble the tags with the attributes. I am using version 4.1.6 of spring.
– Felipe Renan