Estou a enfrentar um comportamento um tanto quanto estranho no meu projecto que usa Maven+Spring+Hibernate+JSP. Quando acciono um botão no formulário,tudo flui normalmente (a URL correspondente é accionada e a lógica corresponde ao método ligada a mesma URL é executada), porém, o possível retorno do método não se reflecte no formulário porque a URL não é carregada no browser. O mais intrigante é que não arrebenta nenhum erro, o que faz tornar-se mais complicado superar esta anomalia. Alguém sabe dizer qual pode ser a razão desta anomalia? Estou a dias batendo cabeça com esta situação sem sucesso! Preciso de adicionar alguma biblioteca, ou qualquer coisa do género?
Pode mandar o método de quando você clica no botão?
Poderia postar o código?
Viva Ardenghe! Abaixo vai uma parte do código, Todos os botões que clico no formulário os respectivos métídos são accioinados e a lógica é percorrida mas não há reflexo no formulário.
Do de JSP:
function cancelar(){
$.get("cancelar");
}
Do lado do Controller:
@RequestMapping(value="/cancelar", method= {RequestMethod.GET})
public ModelAndView cancelar(){
ModelAndView modeView = new ModelAndView("dept/dept-form","allDept", this.allDept);
modeView.addObject("dept", new DepartamentoVO());
return modeView;
}
Form dept-form.jsp:
<%@ page language=“java” contentType=“text/html; charset=ISO-8859-1” pageEncoding=“ISO-8859-1”%>
<%@ taglib uri=“http://www.springframework.org/tags” prefix=“spring”%>
<%@ taglib uri=“http://www.springframework.org/tags/form” prefix=“form”%>
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
<%@ taglib uri=“http://java.sun.com/jsp/jstl/fmt” prefix=“fmt”%>
<%@ taglib tagdir="/WEB-INF/tags" prefix=“sb” %>
<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>
<!--<script src="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 DepartamentoAction:
package iim.sigra.controller.departamento;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import iim.sigra.model.departamento.DepartamentoDAO;
import iim.sigra.model.departamento.DepartamentoVO;
import iim.sigra.model.usuario.UsuarioVO;
import iim.sigra.utilitarios.Mensagens;
@Controller
@RequestMapping("/dept")
public class DepartamentoAction {
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{
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(){
ModelAndView modeView = new ModelAndView("dept/dept-form","allDept", this.allDept);
modeView.addObject("dept", new DepartamentoVO());
return modeView;
}
}
Já postei o código. Peço a vossa atenção