我试图从主从表单捕获数据返回一个 JSON 对象,如下所示我的表单类似于这个http://www.vitarara.org/cms/struts_2_cookbook/creating_a_theme我遵循了这个指南http://struts.apache.org /2.1.6/docs/tabular-inputs.html我可以捕获主数据,但如何访问详细信息?我此刻得到的结果是
{"codigo":"RQ-201000-1210-00001","fecha":"Oct 25, 2012 12:00:00 AM","dreqproductos":[]}
我的表格是:
我怎样才能获得 dreqproductos?
<input name="reqproducto.codigo" />
<input name="reqproducto.fecha" />
<input name="reqproducto.dreqproductos[1].masterproducto" />
<input name="reqproducto.dreqproductos[1].cantidad" />
<input name="reqproducto.dreqproductos[2].masterproducto" />
<input name="reqproducto.dreqproductos[2].cantidad" />
我的课是
public class Reqproducto implements java.io.Serializable {
private Integer id;
private String codigo;
private Date fecha;
private Set dreqproductos = new HashSet(0);
}
和
public class Dreqproducto implements java.io.Serializable {
private Integer id;
private Masterproducto masterproducto;
private Reqproducto reqproducto;
private BigDecimal cantidad;
}
我的行动是
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package actions;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import hibernate.domain.Centrocosto;
import hibernate.domain.Dreqproducto;
import hibernate.domain.Reqproducto;
import hibernate.util.hibernateUtil;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;
import model.dao.impl.DaoReqproductoImpl;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
*
* @author DEV11
*/
public class ActionReqProducto extends ActionSupport{
private InputStream inputStream;
public InputStream getInputStream() {
return inputStream;
}
public Reqproducto reqproducto;
public void setReqproducto(Reqproducto reqproducto) {
this.reqproducto = reqproducto;
}
public String ReqproductoIns() throws UnsupportedEncodingException {
//CAPTURA
String msg = "VOID";
Session session = hibernateUtil.getSessionFactory().getCurrentSession();
//Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
//////////////////////////////////////////////////////////////
if (reqproducto.getId() == null) {//INSERTAR
//CABECERA INSERTAR GERISTRO
session.save(reqproducto);
msg = "INSERT ";
} else {//ACTUALIZAR
//CABECERA ACTUALIZAR REGISTRO
String queryString = "from Reqproducto where id = :id";
Query query = session.createQuery(queryString);
query.setInteger("id", reqproducto.getId());
session.update(reqproducto);
msg = "UPDATE ";
}
//////////////////////////////////////////////////////////////
//session.getTransaction().commit();
transaction.commit();
msg += "OK" + reqproducto.getId();//+session.getIdentifier(reqproducto);;
} catch (HibernateException e) {
transaction.rollback();
//e.printStackTrace();
msg = "ERROR: " + e.getMessage();
} finally {
//session.close();
}
//RESPUESTA
Gson gson = new Gson();
//String json = "{cabecera:{'id':'"+id+"','codigo':'"+codigo+"','descripcion':'"+descripcion+"','abreviatura':'"+abreviatura+"','visible':'"+visible+"'}, msg:'Devolviendo Resultado via Ajax en \"Español\"'}";
String json = gson.toJson(this.reqproducto);
inputStream = new ByteArrayInputStream(json.getBytes("UTF-8"));
return SUCCESS;
}
}