如何在使用spring mvc搜索jsp中的数据时显示空值错误消息?

时间:2011-04-27 10:37:51

标签: hibernate jsp spring-mvc

我有一个名为accounts的表,我在其中按帐户名搜索数据,搜索工作正常。假设我试图搜索哪个不在我的数据库中,它显示错误,所以我想通过显示表中不存在数据的消息来处理异常。我怎么能点到它?我使用spring mvc和hibernate 3和后端mysql。

这是我的代码。

这是我的hibernate dao:

@SuppressWarnings("unchecked")
@Transactional(readOnly=true)
public List<Accounts> searchAccounts(String account){
    System.out.println("Executing Search Accounts Query:::::::::::::::::::::::::::::::::::::::::::::::");

    List<Accounts> accounts = getHibernateTemplate().find("from Accounts a here a.accountName = ?", account);
    if (accounts != null)
    {
        return accounts;
    }
    return null;
}

这是我的搜索控制器:

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
    throws Exception{

    String accountName = ServletRequestUtils.getStringParameter(request, "accountName");
    List<Accounts> accounts = accountsDao.searchAccounts(accountName);  
        if (accounts == null || accounts.isEmpty()){            
        ModelAndView mav = new ModelAndView("forward:searchaccounts.htm");
        System.out.println("searching data");
        return mav.addObject("message", "message.form.searchaccounts");         
    } else {
        ModelAndView mav = new ModelAndView();
        return mav.addObject(accounts);
    }       
}

这是我的搜索帐户jsp:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   "http://www.w3.org/TR/html4/loose.dtd">  
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

    <div class="boxed">
            <div class="title">Account Search</div>
            <div class="content">
                <form id="form4" method="get" action="#">
                    <fieldset>
                    <legend>Search</legend>
                    <input id="inputtext3" type="text" name="inputtext3" value="" class="textfield" />
                    <input id="inputsubmit2" type="submit" name="inputsubmit2" value="Search" class="button" />
                    <p class="tiny"><a href="#">Advanced Search</a></p>
                    </fieldset>
                </form>
            </div>
    </div>


</div>  

<div id="col-form">
    <div class="boxed">
        <div class="title">Group Account</div>
        <div class="content">
        <table border="1" width=100% >          
        <tr>
        <td bgcolor="#BBDDFF" >Account Id</td>
        <td bgcolor="#BBDDFF" >Account Group</td>
        <td bgcolor="#BBDDFF" >Closing Balance</td>
        <td bgcolor="#BBDDFF" >Account Type</td>
        <td bgcolor="#BBDDFF" >Remarks</td>
        </tr>                   
        <c:forEach var="accounts" items="${accountsList}">
        <tr bgcolor="#FFFFFF">      
        <td>${accounts.accountId}</td>                 
        <td><a href="accountsledger.htm?accountId=${accounts.accountId}">${accounts.accountGroup}</a></td>
        <td>${accounts.closingBalance}</td>
        <td>${accounts.closingType}</td>
        <td>${accounts.remarks}</td>
        </tr>
        </c:forEach>        
        </table>    
        </div> <!-- content -->
    </div>  <!-- boxed -->

</body>     

</html>

1 个答案:

答案 0 :(得分:0)

您的accountsDao.searchAccounts可以抛出自定义异常。

例如

throw new AccountException("Account not Found")而不是返回null;

在控制器中捕获该异常并将该错误消息设置为ModelAndView对象。

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
    throws Exception{
            String accountName = ServletRequestUtils.getStringParameter(request, "accountName");
            ModelAndView mav = new ModelAndView("forward:searchaccounts.htm");
            List<Accounts> accounts = null;
            try{
                accounts = accountsDao.searchAccounts(accountName); 
            }catch(AccountException ex){
                mav.addObject("errorMsg",ex.getMessage());
            }

            mav.addObject("accountsList",accounts);
            return mav;   
        }
    }

在jsp中

<font color='red'>${errorMsg}</font>

或者您可以尝试其他方法。

Evaluate if list is empty JSTL