I'm working with Java Spring , hibernate and maven and when I see the data in a grid I get this error:
SyntaxError: JSON.parse: unexpected end of data at line 1 column 147704 of the JSON data.
But it only comes up when I add data to the invoice table and I do not understand why. someone could help me
Table code Products
CREATE TABLE 'Producto' (
'IdProducto' int(11) NOT NULL AUTO_INCREMENT,
'Descripcion' varchar(45) DEFAULT NULL,
'Precio' int(11) DEFAULT NULL,
'IdFactura' int(11) DEFAULT NULL,
PRIMARY KEY ('IdProducto')
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
This is the table code of Invoice
CREATE TABLE 'Factura' (
'IdFactura' int(11) NOT NULL AUTO_INCREMENT,
'IdCliente' int(11) DEFAULT NULL,
'IdProducto' int(11) DEFAULT NULL,
'CantidadProducto' int(11) DEFAULT NULL,
'Total' int(11) DEFAULT NULL,
PRIMARY KEY ('IdFactura')
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
This is the invoice
driverpackage com.csye.rooms.web.controller;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
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.bind.support.SessionStatus;
import com.csye.rooms.bo.ClienteBo;
import com.csye.rooms.bo.FacturaBo;
import com.csye.rooms.bo.JqGridBo;
import com.csye.rooms.bo.ProductosBo;
import com.csye.rooms.domain.Cliente;
import com.csye.rooms.domain.Factura;
import com.csye.rooms.domain.Productos;
import com.csye.rooms.utils.JqGridFilter;
import com.csye.rooms.utils.RespuestaService;
import com.csye.rooms.web.propertyEditor.ClientePropertyEditor;
import com.csye.rooms.web.propertyEditor.ProductosPropertyEditor;
@Controller
public class FacturaController {
@Autowired
private ClienteBo clienteBo;
@Autowired
private ProductosBo productosBo;
@Autowired
private FacturaBo facturaBo;
@Autowired
private JqGridBo jqGridBo;
// @Autowired
// private FacturaFormValidator facturaFormValidator;
@Autowired
private ClientePropertyEditor clientePropertyEditor;
@Autowired
private ProductosPropertyEditor productosPropertyEditor;
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Collection.class, "productos", productosPropertyEditor);
binder.registerCustomEditor(Cliente.class, clientePropertyEditor);
}
@RequestMapping(value = "/ListarFactura.do", method = RequestMethod.GET)
public String listar(ModelMap model) {
return "views/Factura/ListarFacturaForm";
}
@RequestMapping(value = "/ListarFacturaAjax.do", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody
RespuestaService listarAjax(HttpServletRequest request, HttpServletResponse response) {
JqGridFilter delimitadores = new JqGridFilter(request, "Factura");
Long total = jqGridBo.contar(delimitadores);
Collection<Object> objetos = jqGridBo.listar(delimitadores);
RespuestaService respuestaService = new RespuestaService();
for (Iterator<Object> iterator = objetos.iterator(); iterator.hasNext();) {
Factura object = (Factura) iterator.next();
respuestaService.getRows().add(object);
}
respuestaService.setTotal(delimitadores.calculaCantidadPaginas(total));
respuestaService.setPage(delimitadores.getPagina());
respuestaService.setRecords(total);
return respuestaService;
}
@RequestMapping(value = "/AgregarFactura.do", method = RequestMethod.GET)
public String agregarFactura(ModelMap model) {
Factura factura = new Factura();
factura.setCliente(new Cliente());
model.addAttribute(new Factura());
this.cargarListaClientes(model);
this.cargarListaProductos(model, null, Boolean.FALSE);
return "views/Factura/AgregarFacturaForm";
}
@RequestMapping(value = "/CrearFactura.do", method = RequestMethod.POST)
public String crearFactura(ModelMap model, @ModelAttribute Factura factura, BindingResult result, SessionStatus status) throws Exception {
try {
if (result.hasErrors()) {
this.cargarListaClientes(model);
this.cargarListaProductos(model, factura, Boolean.FALSE);
return "views/Factura/AgregarFacturaForm";
} else {
status.setComplete();
facturaBo.incluir(factura);
return "redirect:ListarFactura.do";
}
} catch (Exception e) {
throw e;
}
}
@RequestMapping(value = "/ModificarFactura.do", method = RequestMethod.GET)
public String modificarFactura(ModelMap model, @RequestParam("id") Long idFactura) {
Factura factura = facturaBo.obtener(idFactura);
model.addAttribute(facturaBo.obtener(idFactura));
this.cargarListaClientes(model);
this.cargarListaProductos(model, factura, Boolean.TRUE);
return "views/Factura/ModificarFacturaForm";
}
@RequestMapping(value = "/ActualizarFactura.do", method = RequestMethod.POST)
public String actualizarFactura(ModelMap model, @ModelAttribute Factura factura, BindingResult result, SessionStatus status) throws Exception {
try {
if (result.hasErrors()) {
this.cargarListaClientes(model);
this.cargarListaProductos(model, factura, Boolean.TRUE);
return "views/Factura/ModificarFacturaForm";
} else {
status.setComplete();
facturaBo.modificar(factura);
return "redirect:ListarFactura.do";
}
} catch (Exception e) {
throw e;
}
}
@RequestMapping(value = "/EliminarFactura.do", method = RequestMethod.GET)
public String borrarFactura(@RequestParam("id") Long idFactura) throws Exception {
try {
facturaBo.eliminar(facturaBo.obtener(idFactura));
return "redirect:ListarFactura.do";
} catch (Exception e) {
throw e;
}
}
@RequestMapping(value = "/MostrarFactura.do", method = RequestMethod.GET)
public String ceder(ModelMap model, @RequestParam("id") Long idFactura) {
model.addAttribute("factura", facturaBo.obtener(idFactura));
return "views/Factura/MostrarFacturaForm";
}
public void cargarListaClientes(ModelMap model) {
Collection<Cliente> clientes = clienteBo.Listar();
if (clientes != null && clientes.size() > 0) {
model.addAttribute("clientes", clientes);
}
}
public void cargarListaProductos(ModelMap model, Factura factura, Boolean eliminar) {
Collection<Productos> productos = productosBo.listar();
if (productos != null && productos.size() > 0) {
if (eliminar) {
productos.removeAll(factura.getProductos());
}
model.addAttribute("productos", productos);
}
}
}
This is the invoice
grid$("#gridFacturas").jqGrid({
url: "ListarFacturaAjax.do",
datatype: "json",
jsonReader : {
root: "rows",
page: "page",
total: "total",
records: "records",
},
colNames: ["Cliente", "Producto", "Cantidad", "Total","Opciones"],
colModel: [
{ name: "cliente.id", width:100, align:'center', searchrules:{required:true}, searchoptions:{sopt:['eq','cn']}},
{ name: "productos.descripcion", width:170, align:'center', searchrules:{required:true}, searchoptions:{sopt:['eq','cn']}},
{ name: "cantidadProducto", width:170, align:'center', searchrules:{required:true}, searchoptions:{sopt:['eq','cn']}},
{ name: "total", width:170, align:'center', searchrules:{required:true}, searchoptions:{sopt:['eq','cn']}},
{ name: "id", width:200,sortable:false, search:false, align:'center',
edittype:'select', formatter:linksFactura},
]
}).navGrid('#paginacion');
function linksFactura(cellvalue, options, rowObject) {
var mostrar = '<a href = "MostrarFactura.do?id=' + options.rowId + '" class="mostrar" title="Mostrar"/>';
var edit = '<a href = "ModificarFactura.do?id=' + options.rowId + '" class="edit" title="Editar"/>';
var borrar = '<a href = "EliminarFactura.do?id=' + options.rowId
+ '" onclick="return confirm(\'Est\u00E1 seguro que desea eliminar la factura seleccionada\');" class="borrar" title="Eliminar"/>';
acciones = mostrar + edit+ borrar;
return acciones;
}