如何将对象的Arraylist转换为Javascript?

时间:2014-09-16 16:10:43

标签: java javascript extjs arraylist

我使用struts和javascript来显示一个元素表,方法

public List<Object> preparaInformeRiesgoActivos( int dimension, String idTipoActivo, String idActivo, String tipoActivo, String nombreActivo, ResourceBundle recursos ) {
    // TO-DO: Cambiar query de else.
    String consulta = "";    
    if(dimension!=0){            
        consulta = "SELECT A.ID_AMENAZA, A.CODIGO, A.DENOMINACION, D.ID_RANGO, D.IMPACTO, D.RIESGO FROM (  SELECT ID_AMENAZA, ID_RANGO, SUM(IMPACTORESIDUAL) AS IMPACTO, SUM(RIESGORESIDUAL) AS RIESGO "
                +  "FROM AGR_IMP_RISK_REAL WHERE ID_TIPOACTIVO="+idTipoActivo+" AND ID_ACTIVO='"+idActivo+"' and ID_RANGO = 7 "
                +  "GROUP BY ID_AMENAZA, ID_RANGO) D , (  SELECT ID_AMENAZA, SUM(IMPACTORESIDUAL) AS IMPACTOTOTAL "
                +  "FROM AGR_IMP_RISK_REAL WHERE ID_TIPOACTIVO="+idTipoActivo+" AND ID_ACTIVO='"+idActivo+"'  and ID_RANGO = 7 "
                +  "GROUP BY ID_AMENAZA) T , AGR_AMENAZA A WHERE T.IMPACTOTOTAL<>0 AND T.ID_AMENAZA = D.ID_AMENAZA AND D.ID_AMENAZA = A.ID_AMENAZA ORDER BY D.ID_AMENAZA, D.ID_RANGO";
    }else{
        consulta = "SELECT A.ID_AMENAZA, A.CODIGO, A.DENOMINACION, D.ID_RANGO, D.IMPACTO, D.RIESGO FROM (  SELECT ID_AMENAZA, ID_RANGO, SUM(IMPACTORESIDUAL) AS IMPACTO, SUM(RIESGORESIDUAL) AS RIESGO "
                +  "FROM AGR_IMP_RISK_REAL WHERE ID_TIPOACTIVO="+idTipoActivo+" AND ID_ACTIVO='"+idActivo+"' and ID_RANGO = 7 "
                +  "GROUP BY ID_AMENAZA, ID_RANGO) D , (  SELECT ID_AMENAZA, SUM(IMPACTORESIDUAL) AS IMPACTOTOTAL "
                +  "FROM AGR_IMP_RISK_REAL WHERE ID_TIPOACTIVO="+idTipoActivo+" AND ID_ACTIVO='"+idActivo+"'  and ID_RANGO = 7 "
                +  "GROUP BY ID_AMENAZA) T , AGR_AMENAZA A WHERE T.IMPACTOTOTAL<>0 AND T.ID_AMENAZA = D.ID_AMENAZA AND D.ID_AMENAZA = A.ID_AMENAZA ORDER BY D.ID_AMENAZA, D.ID_RANGO";
    }
    Query q = dao.createNativeQuery(consulta);    
    List<Object> resultado = q.getResultList();    
    return resultado;
}

此方法返回对象集合。在我的jsp中我使用ExtJS作为框架来显示一个带有这个元素的表。如何在没有框架的情况下在一个javascript商店中转换我的java Arraylist(如果可能的话)?

<%@page import="com.dominion.procop.agr.struts.forms.AGRInformesForm"%>
<script type="text/javascript">
Ext.onReady(function() {    
    var amenazaStore = Ext.create('Ext.data.Store', {
        model: 'Amenazas',
        data: [
            { id: 1, codigo: 'E.1', denominacion: 'Errores de los usuarios', id_rango: '0', impacto: '0', riesgo: '0'},
            { id: 2, codigo: 'E.5', denominacion: 'Deficiencias en la organización', id_rango: '0', impacto: '0', riesgo: '0'},
            { id: 3, codigo: 'E.8', denominacion: 'Escapes de información', id_rango: '0', impacto: '0', riesgo: '0'},
            { id: 4, codigo: 'E.9', denominacion: 'Alteración accidental de la información', id_rango: '0', impacto: '0', riesgo: '0'}
        ]
    });

...
</script>

1 个答案:

答案 0 :(得分:1)

  

如何在没有框架的情况下在一个javascript商店中转换我的java Arraylist(如果可能的话)?

从技术上讲,这是可能的。您必须将List<Object> resultado中的每个对象解析为JavaScript可以理解的格式,例如JSON。如果您不使用任何第三方框架,那么将任何类型的Object转换为JSON都是非常麻烦的。

如果您被允许使用第三方框架,那么请使用像Gson或Jackson这样的Java JSON库。如果您无法在代码中添加第三方库,那么您可以复制/粘贴/调整位于http://json.org/中的Java的基本JSON支持,它是开源的,因此您可以在项目的任何包中移动java类。然后与他们合作。