错误转换String BigDecimal

时间:2016-02-29 10:38:51

标签: java apache type-conversion struts

我在一个项目中,当我第一次使用struts时,当我点击JSP页面的提交按钮时,我收到以下错误,任何人都可以找到我这个

例外:

javax.servlet.ServletException: BeanUtils.populate
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:495)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:805)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

根本原因

javax.servlet.ServletException: BeanUtils.populate
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:495)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:805)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

根本原因

java.lang.NumberFormatException
java.math.BigDecimal.<init>(BigDecimal.java:534)
java.math.BigDecimal.<init>(BigDecimal.java:728)
org.apache.commons.beanutils.converters.BigDecimalConverter.convert(BigDecimalConverter.java:112)
org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:428)
org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1004)
org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811)
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:493)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:805)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

这是我的操作页面:

 package it.gepo.cflow.tracking.viewcontroler;

import it.gepo.cflow.tracking.dao.CtrkCheckListBean;
import it.gepo.cflow.tracking.dao.CtrkCheckListDAO;
import it.gepo.cflow.tracking.dao.CtrkCliClassiDAO;
import it.gepo.cflow.tracking.dao.CtrkModuliIndiciBean;
import it.gepo.cflow.tracking.dao.CtrkModuliIndiciDAO;
import it.gepo.cflow.utility.UtilityCtrk;
import it.gepo.utility.GepoUtility;
import it.gepo.web.ConnectionFactory;
import it.gepo.web.GepoUtente;
import it.gepo.web.MyAction;

import java.io.IOException;
import java.math.BigDecimal;

import java.sql.Connection;

import java.sql.SQLException;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.log4j.Logger;

public class TrackingChecklistAction extends MyAction {

    public static Logger logger = Logger
            .getLogger(TrackingChecklistAction.class);
    public static int righeTabella = 10;
    public static int righeTabellaNew = 1;
    public static boolean unRecordXPagina = false;
    public static String nomeForm = "TrackingChecklistForm";

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        logger.debug("execute");
        Connection conn = null;
        HttpSession session = request.getSession(true);
        GepoUtente gepoUtente = (GepoUtente) session.getAttribute("GepoUtente");
        String dataSourceName = gepoUtente.getDataSource();

        try {

            conn = ConnectionFactory.getConnection(dataSourceName);
            TrackingChecklistForm myForm = (TrackingChecklistForm) form;
            if (mapping.getParameter().equals("visualizza")) {
                return visualizza(mapping, myForm, request, response, session,
                        conn);
            } else if (mapping.getParameter().equals("gestione")) {
                return gestione(mapping, myForm, request, response, session,
                        conn);
            } else {
                throw new IllegalStateException();
            }

        } catch (Exception e) {
            logger.info("errore " + e.getMessage());
            ActionMessages errors = new ActionMessages();
            ActionMessage error = new ActionMessage("errore.generico",
                    e.getMessage(), e);
            errors.add(ActionMessages.GLOBAL_MESSAGE, error);
            saveErrors(request, errors);
            return mapping.getInputForward();
        } finally {
            try {
                conn.close();
            } catch (Exception e) {
                throw new ServletException(e.getMessage());
            }
        }
    }

    private ActionForward visualizza(ActionMapping mapping,
            TrackingChecklistForm myForm, HttpServletRequest request,
            HttpServletResponse response, HttpSession session, Connection conn)
            throws IOException, ServletException, Exception {

        logger.debug("visualizza");

        List lista = null;
        CtrkCheckListDAO dao = null;

        //
        // Primo Ingresso
        //
        if (myForm.getUtilEseguiRicerca() == null) {
            myForm.setUtilRigaInizio(0);
            myForm.setUtilRigheTabella(righeTabella);
            myForm.setUtilRigheTabellaNew(righeTabellaNew);
            myForm.setUtilImpostaRicerca(Boolean.FALSE);
            myForm.setUtilEseguiRicerca(Boolean.TRUE);
            myForm.setUtilFormModificata(Boolean.FALSE);
        }

        if (myForm.getUtilEseguiRicerca().booleanValue()) {

            lista = loadByWhere(conn, myForm, session);
            myForm.setListaDati(lista);
            myForm.setUtilEseguiRicerca(Boolean.TRUE);
            myForm.setUtilRigaInizio(0);

        } else {
            dao = new CtrkCheckListDAO();
            lista = dao.loadAll(conn);

            myForm.setListaDati(lista);
            myForm.setUtilEseguiRicerca(Boolean.TRUE);
            myForm.setUtilRigaInizio(0);

        }

        myForm.setListaDati(lista);

        //
        // Controllo Rientro da eventuale Elimina
        //
        if (myForm.getUtilRigheTabella() != 0) {
            if (myForm.getUtilRigaInizio() > myForm.getNumRecord() - 1) {
                myForm.setUtilRigaInizio(((myForm.getNumRecord() - righeTabella) > 0) ? myForm
                        .getNumRecord() - righeTabella
                        : 0);
            }
        }

        //
        // Controlli da eseguire se visualizzo un record per pagina
        //
        if (unRecordXPagina) {
            if (myForm.getNumRecord() == 0) {
                myForm.setUtilRigaInizio(0);
                myForm.setUtilRigheTabella(0);
                myForm.setUtilRigheTabellaNew(righeTabellaNew);
            } else {
                myForm.setUtilRigheTabella(righeTabella);
                myForm.setUtilRigheTabellaNew(0);
            }
        }

        myForm.setListaDatiNew(myForm.getUtilRigheTabellaNew());
        myForm.setUtilFormModificata(Boolean.FALSE);

        return mapping.findForward("success");
    }

    private List loadByWhere(Connection conn, TrackingChecklistForm myForm,
            HttpSession session) throws Exception {

        String id_modulo =(String) ((myForm.getN_id_modulo() == null) ? "": myForm.getN_id_modulo());
        String domanda = (myForm.getC_domanda() == null) ? "" : myForm.getC_domanda();
        String flag_attivo = (myForm.getC_flag_attivo() == null) ? "" : myForm.getC_flag_attivo();
        String data_inserimento = (myForm.getD_data_inserimento() == null) ? "": myForm.getD_data_inserimento();
        String data_disattivazione = (myForm.getD_data_disattivazione() == null) ? "": myForm.getD_data_disattivazione();

        String where = "";
        String ctlWhere = "";
        Object[] elementi = new Object[0];

        if (!id_modulo.equals("")) {
            where = where + ctlWhere + "ID_MODULO = '" + id_modulo + "'";
            ctlWhere = " AND ";
        }

        if (!domanda.equals("")) {

            where = where + ctlWhere + "DOMANDA = '" + domanda + "'";
            ctlWhere = " AND ";
        }

        if (!flag_attivo.equals("")) {
            where = where + ctlWhere + "FLAG_ATTIVO = '" + flag_attivo + "'";
            ctlWhere = " AND ";
        }
        if (!data_inserimento.equals("")) {
            where = where + ctlWhere + "DATA_INSERIMENTO = '"
                    + data_inserimento + "'";
            ctlWhere = " AND ";
        }

        if (!data_disattivazione.equals("")) {

            where = where + ctlWhere + "DATA_DISATTIVAZIONE = '"
                    + data_disattivazione + "'";
            ctlWhere = " AND ";
        }

        logger.debug("where : " + where);

        if (!ctlWhere.equals("")) {
            CtrkCheckListDAO dao = new CtrkCheckListDAO();
            String orderBy = "";
            return dao.loadByWhere(conn, where, elementi, orderBy);
        } else {
            return null;
        }
    }

    private ActionForward gestione(ActionMapping mapping,
            TrackingChecklistForm myForm, HttpServletRequest request,
            HttpServletResponse response, HttpSession session, Connection conn)
            throws IOException, ServletException, Exception {

        logger.debug("gestione");

        String action = request.getParameter("c_request");
        //
        // Controllo Richiesta Immissione / Aggiungi Righe Immissione
        //
        if (unRecordXPagina) {
            if (action.equalsIgnoreCase("Immissione")) {
                myForm.setUtilEseguiRicerca(Boolean.FALSE);
                return mapping.findForward("success");
            }
        } else {
            if (action.equalsIgnoreCase("Aggiungi Righe Immissione")) {
                myForm.addListaDatiNew(5);
                myForm.setUtilRigheTabellaNew(myForm.getUtilRigheTabellaNew() + 5);
            }
        }

        //
        // Controllo Richiesta Aggiungi Righe Immissione
        //
        if (!unRecordXPagina) {
            if (action.equalsIgnoreCase("Aggiungi Righe Immissione")) {
                return mapping.getInputForward();
            }
        }

        //
        // Controllo Richiesta Uscita
        //
        if (action.equalsIgnoreCase("Uscita")) {
            session.removeAttribute(nomeForm);
            GepoUtente utente = (GepoUtente) session.getAttribute("GepoUtente");
            response.sendRedirect(utente.getReturnURL());
            return null;
        }

        //
        // Controllo Richiesta Esegui Ricerca
        //
        if (action.equalsIgnoreCase("Esegui Ricerca")) {
            try {
                validateRicerca(conn, myForm);
                return mapping.findForward("success");
            } catch (Exception e) {
                logger.debug(e.getMessage());
                ActionMessages errors = new ActionMessages();
                ActionMessage error = new ActionMessage("errore.generico",
                        e.getMessage(), e);
                errors.add(ActionMessages.GLOBAL_MESSAGE, error);
                saveErrors(request, errors);
                return mapping.getInputForward();
            }
        }

        //
        // Validate Records
        //
        if (!validate(conn, session, myForm)) {
            return mapping.getInputForward();
        }

        //
        // Aggiornamento Tabelle
        //
        if (!aggiorna(conn, myForm)) {
            try {
                conn.rollback();
            } catch (SQLException e) {
                throw new Exception("Errore ROLLBACK : " + e.getMessage());
            }
            return mapping.getInputForward();

        } else {
            try {
                conn.commit();
            } catch (SQLException e) {
                throw new Exception("Errore COMMIT : " + e.getMessage());
            }
        }

        //
        // Aggiornamento Riuscito
        // Imposto default per righe immissione
        //
        if (!unRecordXPagina) {
            myForm.setUtilRigheTabellaNew(righeTabellaNew);
        }

        //
        // Controllo Richiesta Successivo / Precedente
        //
        if (action.equalsIgnoreCase("Successivo")) {
            myForm.setUtilRigaInizio(myForm.getUtilRigaInizio()
                    + myForm.getUtilRigheTabella());
            return mapping.getInputForward();
        }

        if (action.equalsIgnoreCase("Precedente")) {
            myForm.setUtilRigaInizio(((myForm.getUtilRigaInizio() - myForm
                    .getUtilRigheTabella()) > 0) ? myForm.getUtilRigaInizio()
                    - myForm.getUtilRigheTabella() : 0);
            return mapping.getInputForward();
        }

        return mapping.findForward("success");

    }

    private void validateRicerca(Connection conn, TrackingChecklistForm myForm)
            throws Exception {

        String n_id_modulo = (String)  ((myForm.getN_id_modulo() == null) ? ""
                : myForm.getN_id_modulo());
        String c_domanda = (myForm.getC_domanda() == null) ? "" : myForm
                .getC_domanda();
        String c_flag_attivo = (myForm.getC_flag_attivo() == null) ? ""
                : myForm.getC_flag_attivo();
        String d_data_inserimento = (myForm.getD_data_inserimento() == null) ? ""
                : myForm.getD_data_inserimento();
        String d_data_disattivazione = (myForm.getD_data_disattivazione() == null) ? ""
                : myForm.getD_data_disattivazione();

        if (n_id_modulo.equals("") && c_domanda.equals("")
                && c_flag_attivo.equals("") && d_data_inserimento.equals("")
                && d_data_disattivazione.equals("")) {
            myForm.setUtilEseguiRicerca(Boolean.FALSE);
        } else {
            myForm.setUtilEseguiRicerca(Boolean.TRUE);
        }

        myForm.setUtilImpostaRicerca(Boolean.FALSE);
        myForm.setUtilRigaInizio(0);

    }

    private boolean validate(Connection conn, HttpSession session,
            TrackingChecklistForm myForm) throws Exception {

        boolean validato = true;
        TrackingChecklistRecord[] rec = myForm.getListaDati();
        TrackingChecklistRecord[] recNew = myForm.getListaDatiNew();

        for (int x = 0; x < myForm.getUtilRigheTabellaNew(); x++) {

            if (recNew[x].isModificato()) {
                if (!validateRecord(conn, recNew[x])) {
                    validato = false;
                }
                myForm.setDatiNew(recNew[x], x);
                myForm.setUtilFormModificata(Boolean.TRUE);
            }

        }

        int begin = myForm.getUtilRigaInizio();
        int end = (begin + myForm.getUtilRigheTabella()) > rec.length ? rec.length
                : (begin + myForm.getUtilRigheTabella());

        for (int x = begin; x < end; x++) {

            if (rec[x].isModificato()) {
                if (!validateRecord(conn, rec[x])) {
                    validato = false;
                }
                myForm.setDati(rec[x], x);
                myForm.setUtilFormModificata(Boolean.TRUE);
            }

        }
        return validato;
    }

    private boolean validateRecord(Connection conn, TrackingChecklistRecord rec) throws Exception {

        UtilityCtrk rowExist = new UtilityCtrk();
        CtrkCheckListBean vo = new CtrkCheckListBean();
        CtrkCheckListDAO dao = new CtrkCheckListDAO();
        //
        // Control Id modulo
        //
        if (rec.getN_id_modulo().equals("")) {
            rec.setC_errore("Inserire ID MODULO");
            return false;
        }
        //
        // Control Domanda
        //
        if (rec.getC_domanda().equals("")) {
            rec.setC_errore("Inserire DOMANDA");
            return false;
        }
        //
        // Control Flag attivo
        //
        if (rec.getC_flag_attivo().equals("")) {
            rec.setC_errore("Inserire FLAG ATTIVO");
            return false;
        }
        vo.setIdModulo(new BigDecimal(rec.getN_id_modulo()));
        vo.setDomanda(rec.getC_domanda());
        vo.setFlagAttivo(rec.getC_flag_attivo());

        vo.setId(new BigDecimal(GepoUtility.getSequenceNext(conn,
                "SEQ_ID_TRACCIATURA")));

        if (dao.exist(conn, vo)) {
            rec.setC_errore("Record già presente!");
            return false;
        } else {
            return true;
        }
    }

    private boolean aggiorna(Connection conn, TrackingChecklistForm myForm)
            throws Exception {

        boolean aggiornato = true;

        TrackingChecklistRecord[] rec = myForm.getListaDati();
        TrackingChecklistRecord[] recNew = myForm.getListaDatiNew();

        int begin = myForm.getUtilRigaInizio();
        int end = (begin + myForm.getUtilRigheTabella()) > rec.length ? rec.length
                : (begin + myForm.getUtilRigheTabella());

        //
        // Loop per DELETE
        //
        for (int x = begin; x < end; x++) {

            try {

                CtrkCheckListBean vo = new CtrkCheckListBean();
                CtrkCheckListDAO dao = new CtrkCheckListDAO();

                if (rec[x].isC_delete()) {
                    vo.setRowid(rec[x].getR_rowid());
                    dao.delete(conn, vo);
                }

            } catch (Exception e) {
                aggiornato = false;
                if (unRecordXPagina) {
                    rec[x].setC_delete(false);
                }
                rec[x].setC_errore(e.getMessage());
                myForm.setDati(rec[x], x);
            }

        }

        //
        // Loop per INSERT
        //
        for (int x = 0; x < myForm.getUtilRigheTabellaNew(); x++) {

            try {

                CtrkCheckListBean vo = new CtrkCheckListBean();
                CtrkCheckListBean voOld = new CtrkCheckListBean();
                CtrkCheckListDAO dao = new CtrkCheckListDAO();

                vo.setIdModulo(string2BigDecimal(recNew[x].getN_id_modulo()));
                voOld.setIdModulo(string2BigDecimal(recNew[x]
                        .getN_id_moduloOld()));
                vo.setDomanda(recNew[x].getC_domanda());
                voOld.setDomanda(recNew[x].getC_domandaOld());
                vo.setFlagAttivo(recNew[x].getC_flag_attivo());
                voOld.setFlagAttivo(recNew[x].getC_flag_attivoOld());
                vo.setDataInserimento(string2Date(recNew[x]
                        .getD_data_inserimento()));
                voOld.setDataInserimento(string2Date(recNew[x]
                        .getD_data_inserimentoOld()));
                vo.setDataDisattivazione(string2Date(recNew[x]
                        .getD_data_disattivazione()));
                voOld.setDataDisattivazione(string2Date(recNew[x]
                        .getD_data_disattivazioneOld()));

                if (!vo.hasEqualMapping(voOld)) {

                    dao.create(conn, vo);
                    logger.debug("Create " + vo);
                }

            } catch (Exception e) {
                aggiornato = false;
                recNew[x].setC_errore(e.getMessage());
                myForm.setDatiNew(rec[x], x);
            }

        }

        //
        // Loop per UPDATE
        //
        for (int x = begin; x < end; x++) {

            try {

                if (!rec[x].isC_delete()) {

                    CtrkCheckListBean vo = new CtrkCheckListBean();
                    CtrkCheckListBean voOld = new CtrkCheckListBean();
                    CtrkCheckListDAO dao = new CtrkCheckListDAO();
                    vo.setRowid(rec[x].getR_rowid());
                    voOld.setRowid(rec[x].getR_rowid());
                    vo.setId(string2BigDecimal(rec[x].getN_id()));
                    voOld.setId(string2BigDecimal(rec[x].getN_idOld()));
                    vo.setIdModulo(string2BigDecimal(rec[x].getN_id_modulo()));
                    voOld.setIdModulo(string2BigDecimal(rec[x]
                            .getN_id_moduloOld()));
                    vo.setDomanda(rec[x].getC_domanda());
                    voOld.setDomanda(rec[x].getC_domandaOld());
                    vo.setFlagAttivo(rec[x].getC_flag_attivo());
                    voOld.setFlagAttivo(rec[x].getC_flag_attivoOld());
                    vo.setDataInserimento(string2Date(rec[x]
                            .getD_data_inserimento()));
                    voOld.setDataInserimento(string2Date(rec[x]
                            .getD_data_inserimentoOld()));
                    vo.setDataDisattivazione(string2Date(rec[x]
                            .getD_data_disattivazione()));
                    voOld.setDataDisattivazione(string2Date(rec[x]
                            .getD_data_disattivazioneOld()));
                    if (!vo.hasEqualMapping(voOld)) {
                        dao.save(conn, vo);
                    }
                }
            } catch (Exception e) {
                aggiornato = false;
                rec[x].setC_errore(e.getMessage());
                myForm.setDati(rec[x], x);
            }
        }
        return aggiornato;
    }
}

1 个答案:

答案 0 :(得分:1)

可能有一种方法可以通过例外。还有其他一些原因可以在这里下载。我希望这有帮助

  1. 标记的action属性必须与struts-config.xml文件中操作定义的path属性完全匹配。这就是Struts如何将ActionForm bean与动作相关联。
  2. 当您指定了标记中不存在的表单名称时,通常会发生此错误。例如,您指定并且'myForm'不是与struts-config文件中的myAction关联的表单的名称
  3. 当Struts无法将HTML表单中的数据映射到ActionForm bean中的属性时,会收到此消息。确保bean上的每个属性都是String或boolean。你有java.util.Date类型的任何属性或其他对象吗?这可能会导致此错误。还要检查每个属性是否有公共getter和setter。
  4. 您可以查看此网站:

    http://www.coderanch.com/t/53114/Struts/ServletException-BeanUtils-populate

    http://javaexceptions1.blogspot.com/2009/08/javaxservletservletexception.html

相关问题