0

I have a table called accounts in which i am searching the data by account name, the search is working perfectly. Suppose if i am trying to search a which is not in my database, it shows an error, so i want to handle the exception by displaying an message that the data is not exist in the table. How can i dot it? I am using spring mvc and hibernate 3 with an back end mysql.

Here is my code.

This is my 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;
}

This is my search Controller:

@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);
    }       
}

This is my search 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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sameer
  • 85
  • 1
  • 3
  • 14

1 Answers1

0

Your accountsDao.searchAccounts can throw customized exception.

e.g

throw new AccountException("Account not Found") instead of returning null;

catch that exception in controller and set that error message to ModelAndView object.

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;   
        }
    }

in jsp

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

Or you can try another approach.

Evaluate if list is empty JSTL

Community
  • 1
  • 1
zawhtut
  • 8,335
  • 5
  • 52
  • 76
  • Thanks zawhtut, but i want to display the message in the jsp page, while when i click at the search button. – sameer Apr 27 '11 at 11:18
  • Hi @sameer It was my fault that previous answer was quite abstract. See the update – zawhtut Apr 27 '11 at 15:41
  • thanks for your effort zawhtut, i tried this method is not working. It shows this error. java.lang.StackOverflowError org.apache.catalina.connector.Request.getAttribute(Request.java:877) – sameer Apr 28 '11 at 10:43