-1
I have a JSP form, in which I embed Javascript, and once I do client-side validations, I created a Java function "save(param)".
When clicking on the save button, the function Avascript "save(param)" is invoked, which in turn first invokes the function that calls the data of the object to be saved through the "validate()" method and finally triggers the controller method (save()) that saves the object.
The bug that is occurring is that once this method save()
from the controller, takes the object to be saved as parameter, when triggering this same method via javaScrip(save(param), Note param=Object to be saved), param arrives in the empty controller, or as null object.
I also tried to capture this object to be saved, using a HttpServletRequest
, as follows:
tipopagamento = (TipoPagamentoVO) request.getSession().getAttribute(“tipopagamento”);
But in the same the object arrives null, even having passed the validation.
I don’t understand why the object is getting there in the save() method of my controller (type gamentoaction) as null
My Form JSP:
<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>
Typopagamentovo()
package Iim.sigra.model.parametrizacao.type;
import javax.persistence.Entity; import javax.persistence.Generatedvalue; import javax.persistence.Id; import javax.persistence.Table; import javax.servlet.http.Httpservletrequest; import javax.validation.constraints.Notnull; import org.hibernate.Annotations.Genericgenerator;
@Entity @Table(name = "TYPE") public class Typopagamentovo {
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
protected long selfId;
@NotNull
protected String designacao;
protected String descricao;
public TipoPagamentoVO() {
}
public TipoPagamentoVO(HttpServletRequest rq) {
}
public TipoPagamentoVO(long SelfId, String designacao, String descricao){
this.selfId = 0;
this.designacao = designacao;
this.descricao = descricao;
}
public long getSelfId() {
return selfId;
}
public void setSelfId(long selfId) {
this.selfId = selfId;
}
public String getDesignacao() {
return designacao;
}
public void setDesignacao(String designacao) {
this.designacao = designacao;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
@Override
public boolean equals(Object object) {
TipoPagamentoVO tipo = (TipoPagamentoVO) object;
return (this.selfId==tipo.selfId && tipo.selfId!=0) || (this.designacao!=null && this.designacao.equalsIgnoreCase(tipo.designacao));
}
@Override
public String toString() {
return "SelfId: "+this.selfId +","+" Designação: "+this.designacao +","+" Descrição: "+this.descricao;
}
}
My Controller:
@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);
}
}
Edit your question and add class code
TipoPagamentoVO
, please.– Felipe Marinho
Viva @Felipe Marinho! All right? I edited my question, I don’t know if it’s clearer now, the fact is that the object to be saved is coming in my action as null and so can’t be saved. Thank you very much for your attention. Please ask for your help
– gtamele