FaradayZhangChina 2017-04-13 12:09 采纳率: 0%
浏览 1366
已结题

如何把报错信息反馈到JSP页面?

大家好,我想知道类似这样的一个servicebean如何把报错的message返回到页面?
代码片段如下:

     private static final String ERROR = "数据库错误:";
    private static final String RETRIEVE_STMT = 
            "SELECT * FROM Alliance WHERE lyear=? AND season=?";
                ......
                public List<Alliance> retrieveAll() throws AllianceException{
        Connection connection = null;
        PreparedStatement stmt = null;
        ResultSet results = null;

        try {
            connection = ds.getConnection();
            stmt = connection.prepareStatement(RETRIEVE_ALL_STMT);
            results = stmt.executeQuery();

            List<Alliance> AllianceList = new LinkedList<Alliance>();
            while (results.next()) {
                League league = new League(results.getInt("lid"), 
                results.getInt("lyear"),
                results.getString("season"), 
                results.getString("title"));
                allianceList.add(alliance);
            }

            return allianceList;
        } catch (SQLException e) {
            throw new AllianceException(ERROR + e.getMessage());
        } finally {
            if (results != null) {
                try { results.close(); } catch (SQLException se) { }
            }
            if (stmt != null) {
                try { stmt.close(); } catch (SQLException se) { }
            }
            if (connection != null) {
                try { connection.close(); } catch (SQLException e) { }
            }
        } 
    } 

如果用ajax传后台数据到前端页面,如何传递这些报错消息?请教。谢谢。

  • 写回答

3条回答 默认 最新

  • 一颗梭梭树 2017-04-13 12:15
    关注

    我一般会建一个实体bean,包含status、data、msg分别对应状态、数据、消息,在后台将数据通过set方法封装好,用ajax通过json格式传送至前台,前台可获取全部信息。
    当然你也可以用response,不过比较之下个人觉得第一种更合适

    评论

报告相同问题?