【问题标题】:How to display null value error message in searching a data in jsp using spring mvc?使用spring mvc在jsp中搜索数据时如何显示空值错误消息?
【发布时间】:2011-08-13 17:58:05
【问题描述】:

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

这是我的代码。

这是我的休眠道:

@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>

【问题讨论】:

    标签: hibernate jsp spring-mvc


    【解决方案1】:

    您的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

    【讨论】:

    • 感谢 zawhtut,但我想在 jsp 页面中显示消息,而当我点击搜索按钮时。
    • 嗨@sameer 以前的答案很抽象是我的错。查看更新
    • 感谢您的努力 zawhtut,我试过这种方法不起作用。它显示了这个错误。 java.lang.StackOverflowError org.apache.catalina.connector.Request.getAttribute(Request.java:877)
    猜你喜欢
    • 2012-06-21
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 2014-12-30
    相关资源
    最近更新 更多