I am experiencing somewhat strange behavior in my project that uses Maven + Spring + Hibernate + JSP. When I click a button on the form, everything flows normally (the corresponding URL is triggered and the logic corresponds to the method linked to the same URL that is executed), however, the possible return of the method is not reflected in the form because the URL is not loaded in the browser . The most intriguing is that it does not break any errors, which makes it more complicated to overcome this anomaly. Does anyone know what the reason for this anomaly might be? I am beating days head on with this unsuccessful situation! Do I need to add any library, or anything? See the code:
Form:
<link href="<c:url value="/resources/css/sigraStyle.css" />" rel="stylesheet">
<link type="text/css" href="resources/css/jquery-ui-1.10.1.custom.css" rel="stylesheet">
<script type="text/javascript" src="resources/js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="resources/js/jquery-ui-1.10.1.custom.js"></script>
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><!--<scriptsrc="http://code.jquery.com/jquery-latest.min.js"></script> -->
<script type="text/javascript" >
function remove(selfId){
$.get("remove",{'selfId':selfId});
}
function cancelar(){
$.get("cancelar");
}
$(document).ready(function() {
$("#deptForm").submit(function(e) {
if(!validar()) {
e.preventDefault();
}
});
});
function validar(){
if (document.getElementById('designacao').value == ''){
alert("O campo 'Designacao' deve ser preenchido");
return false;
}
if (document.getElementById('descricao').value == ''){
alert("O campo 'Descricao' deve ser preenchido");
return false;
}
return true;
}
</script>
</head>
<body>
<c:import url="/cabecalho.jsp" />
<form id="deptForm" action="/sigra/dept/save" method="post" modelAttribute="dept" >
<fieldset>
<h4 align="center"> CADASTRO DE DEPARTAMENTO</h4>
<table width="100%">
<tr>
<td align="center" colspan="3">
<font color="blue">
${statusMsg}
</font>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>
<fieldset>
<table>
<tr>
<td>
<c:if test="${dept.selfId!=null && dept.selfId>0}">
<input name="selfId" id="selfId" value="${dept.selfId}" hidden="true">
</c:if>
</td>
</tr>
<tr>
<td width="15%">
<label> Designação:</label>
</td>
<td width="85%">
<span id="refresh_01">
<input type="text" id="designacao" name="designacao" style=" width: 100%" value="${dept.designacao}" >
</span>
</td>
</tr>
<tr>
<td width="15%">
<label>Descrição:</label>
</td>
<td width="85%">
<span id="refresh_02">
<textarea name="descricao" id="descricao" rows="5" cols="40" style="width: 100%" >${dept.descricao}</textarea>
</span>
</td>
</tr>
</table>
</fieldset>
</td>
</tr>
<tr>
<td>
<fieldset>
<table>
<tr>
<td>
<!-- <a id="save" href="/sigra/dept/save.html" onclick="testFunction()">teste</a> -->
</td>
<td>
<c:choose>
<c:when test="${(dept.selfId == null )|| (dept.selfId==0) }">
<input type="submit" name="action" value="Salvar" >
<input type="reset" value="Limpar" >
</c:when>
<c:otherwise>
<!-- <input type="button" id="Actualizar" value="Actualizar" onclick="actualizar(${dept});" > -->
<input type="submit" name="action" value="Actualizar" >
<input type="button" value="Remover" onclick="remove(${dept.selfId});">
<input type="button" value="Cancelar" onclick="cancelar();">
</c:otherwise>
</c:choose>
<input type="button" name="voltar" id="voltar" value="Voltar">
</td>
<td >
<!-- <button type="button" id="post-btn">$.Post Submit</button> -->
</td>
<td>
</td>
</tr>
</table>
</fieldset>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<fieldset>
<legend>Lista de Departamentos</legend>
<table width="100%">
<tr>
<!-- <td width="15%"></td> -->
<td width="85%">
<table >
<tr align="left">
<th width="5%" >EDITAR</th>
<th width="45%">DESIGNAÇÃO</th>
<th width="50%">DESCRIÇÃO</th>
</tr>
<c:forEach items="${allDept}" var="departamento">
<tr>
<td height="20" valign="middle">
<a href="/sigra/dept/selecteddept?selfId=${departamento.selfId}" >
<img src="<c:url value="/imagens/edit_big.gif" />" width="22px" height="22px" title="Clica aqui para Detalhar!"/>
<!-- <img src="comuns/images/edit_big.gif" width="22px" height="22px" title="Clica aqui para Detalhar!" /> -->
</a>
</td>
<td>${departamento.designacao}</td>
<td>${departamento.descricao}</td>
</tr>
</c:forEach>
</table>
</td>
</tr>
</table>
</fieldset>
</td>
</tr>
<tr>
</tr>
</table>
</fieldset>
</form>
<c:import url="/rodape.jsp" />
</body>
Controller:
@Controller @RequestMapping ("/ dept") public class DepartmentAction {
ArrayList<DepartamentoVO> allDept = new ArrayList<DepartamentoVO>();
@RequestMapping(method= RequestMethod.GET)
public ModelAndView step0(){
DepartamentoDAO deptDao = new DepartamentoDAO();
allDept = deptDao.getAll();
ModelAndView modelView = new ModelAndView("dept/dept-form","allDept", this.allDept);
return modelView;
}
@RequestMapping(value="/save", method= {RequestMethod.POST})
public String save(DepartamentoVO dept, UsuarioVO user, @RequestParam("action") String action, RedirectAttributes redirectAttributes) throws Exception{
DepartamentoDAO dao = new DepartamentoDAO();
if (action.equalsIgnoreCase("Salvar")) {
dao.save(dept, user);
}
else{
dao.update(dept, user);
}
redirectAttributes.addFlashAttribute("statusMsg", Mensagens.OPERATION_SUCCESS_MSG);
return "redirect:";
}
@RequestMapping(value="/remove")
public ModelAndView remove( DepartamentoVO dept , UsuarioVO user, RedirectAttributes redirectAttributes, HttpServletRequest request) throws Exception{
DepartamentoVO deptAux = (DepartamentoVO) request.getSession().getAttribute("dept");
DepartamentoDAO deptDao = new DepartamentoDAO();
deptDao.deleteByID(dept.getSelfId(), user);
redirectAttributes.addFlashAttribute("statusMsg", Mensagens.OPERATION_SUCCESS_MSG);
return new ModelAndView("dept/dept-form","allDept", this.allDept);
}
@RequestMapping(value="/selecteddept", method= {RequestMethod.POST, RequestMethod.GET})
public ModelAndView loadpagamento(@RequestParam("selfId") long id, UsuarioVO user,Model model){
DepartamentoVO dept = new DepartamentoVO();
dept.setSelfId(id);
for ( DepartamentoVO departamento : this.allDept) {
if(departamento.getSelfId()== dept.getSelfId()){
dept = departamento;
model.addAttribute("dept", dept);
}
}
return new ModelAndView("dept/dept-form","allDept", this.allDept);
}
@RequestMapping(value="/cancelar", method= {RequestMethod.GET})
public ModelAndView cancelar(){
System.out.println("Cancelando....!");
ModelAndView modeView = new ModelAndView("dept/dept-form","allDept", this.allDept);
modeView.addObject("dept", new DepartamentoVO());
return modeView;
}
}