Viva companheiros!
Alguém por favor pode me ajudar a colmatar este bug?
Tenho um form JSP, no qual embuti Javascript, e uma vez que faço as validações do lado do cliente, criei uma função javaScript “salvar(param)”.
Ao clicar no botão salvar, é invocada função javaScript “salvar(param)” que por sua vez primeiro invoca a função que valída (validar()) os dados do objecto a ser salvo e por último aciona o metódo da controller que faz o save do tal objecto.
O bug que está ocorrer, é que uma vez que este método save do controller, recebe o objecto a ser salvo como parâmetro, ao acionar este mesmo método via javaScrip(salvar(param), Nota param=Objecto a ser salvo), o param chega na controller vazio, ou seja como objecto nulo.
Tambem tentei capturar este objecto a ser salvo, usando um instâcia do HttpServletRequest, da seguinte maneira:
tipopagamento = (TipoPagamentoVO) request.getSession().getAttribute(“tipopagamento”);
Mas na mesma o objecto chega nullo, mesmo tendo passado na validação:
Meu Form JSP:
<%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%>
<%@ taglib uri=“http://www.springframework.org/tags” prefix=“spring”%>
<%@ taglib uri=“http://www.springframework.org/tags/form” prefix=“form”%>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" >
function salvar(tipopagamento) {
if (!validar()) return;
$.post("tipopagamento/save?tipopagamento="+tipopagamento);
}
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>
<form method="post" action="/sigra/tipopagamento/save" name="tipopagamentoForm" modelAttribute="tipopagamento" >
<fieldset>
<table width="100%">
<tr>
<td>${statusMessage}</td>
</tr>
<tr>
<td>
<fieldset>
<table>
<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="${tipopagamento.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%" >${tipopagamento.descricao}</textarea>
</span>
</td>
</tr>
</table>
</fieldset>
</td>
</tr>
<tr>
<td>
<fieldset>
<table>
<tr>
<td>
<!-- <a id="save" href="/sigra/tipopagamento/save.html" onclick="testFunction()">teste</a> -->
</td>
<td>
<input type="button" id="sa" value="Salvar" onclick="salvar(${tipopagamento});">
</td>
<td id="abc">
<input type="button" value="Limpar" onclick="limpar();">
</td>
<td>
<input type="button" value="Cancelar" onclick="testFunction();">
</td>
</tr>
</table>
</fieldset>
</td>
</tr>
</table>
</fieldset>
</form>
</body>
Minha Controller:
package iim.sigra.controller.tipopagamento;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
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.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import iim.sigra.model.parametrizacao.tipopagamento.TipoPagamentoDAO;
import iim.sigra.model.parametrizacao.tipopagamento.TipoPagamentoVO;
import iim.sigra.model.pessoa.usuario.UsuarioVO;
@Controller
@RequestMapping(value={"/tipopagamento"})
public class TipoPagamentoAction {
@RequestMapping(method=RequestMethod.GET)
public ModelAndView listAllPagamentos(){
ArrayList<TipoPagamentoVO> allTipoPagamentos = new ArrayList<TipoPagamentoVO>();
TipoPagamentoDAO dao = new TipoPagamentoDAO();
allTipoPagamentos = dao.getAll();
return new ModelAndView("/tipopagamento/tipopagamento", "allTipoPagamentos", allTipoPagamentos);
}
@RequestMapping(value="/save", method= {RequestMethod.POST, RequestMethod.GET})
public ModelAndView save(TipoPagamentoVO tipopagamento , UsuarioVO user, HttpServletRequest request) throws Exception{
// tipopagamento = (TipoPagamentoVO) request.getSession().getAttribute("tipopagamento");
TipoPagamentoDAO dao = new TipoPagamentoDAO();
ArrayList<TipoPagamentoVO> allTipoPagamentos = new ArrayList<TipoPagamentoVO>();
dao.save(tipopagamento, user);
allTipoPagamentos = dao.getAll();
return new ModelAndView("/tipopagamento/tipopagamento", "allTipoPagamentos", allTipoPagamentos);
}
}