I'm developing a web app in java using the Spring MVC framework. However, a part of the project has broken out!
First I'll show the print on the screen to make it easier to understand.
Image:
WhatIwanttodois:whentheuserselectsthecompany,theelementsbelow(salary,13thsalaryandIncomeReport)willbeloadeddynamically.Notallcompanieshavealltheoptions.Obs:Thatbuttongeneratedemowouldnotexistanymore.
AftersomeresearchIthoughtaboutdoingthis:
Createajavascriptfunctiontograbtheselectactionandsendtheselecteditemviaajax.AsfarasIunderstand,Icanuseannotation
@RequestBody
toreadthejson
sentbyajax.Withtheselectediteminhand,IcanpassitasaparameterintheselectImakeinthebankthatbringstheoptionsavailabletothatcompany.
Withannotation
@ResponseBody
Isendajson
withtheoptionsandcreatethetagswithjavascript.
Idonotknowiflogicisright,buttheproblemwithitallisthatIdonotknowhowtodoit.
Myquestions:
HowdoIretrievetheselecteditemandsendittothespringcontroller?Forexample:IhaveacompanyobjectthathasthenameandCNPJattributes,howdoIgettheCNPJattributeoftheselecteditemandsendtocontroller?
ThenhowdoIreturntheitemsavailableforthatcompanytothepage?
Seehowmycontrollermethodis:
@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 do not know how to select. From the first form using c:forEach
or using the spring select itself.
EDIT
Personally, I'm getting the item selected 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);
});
No controller methods:
@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;
}
However in the second method you are giving null pointer execption in the line:
List<UltimoPeriodoAberto> opcoes = daoUltimoPeriodoAberto.getPeriodoHolerite(empresa);
I can not identify the error.
EDIT
Well, I got it sorted out. I did so
I changed the annotation @RequestBody
to @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 error 406
in ajax:
The resource identified by this request is only capable of generating responses not acceptable according to the request "accept" headers
Ajax can not get my list, does anyone have any suggestions?