1

I have a controller class that makes a search on the Student Database and displays its information. Right now no matter if a particular student is found or not, it displays the same screen. I am planning to show a different view if backend search doesnt return any data. For this I coded my controller with if else block (data found: show view, else show different view) but it doesnt seem to be working. In any case I am seeing the same view returned back. In this sample student/homePage. What am I doing wrong here?

@Controller
public class StudentController extends BaseClassController
{
 @RequestMapping( value = "/student/studentSearch.html", method = RequestMethod.POST )
  public String searchStudent( Arguments )
  {

    if( bundleStudentBean.getRollNum() != null)
    {

        try
        {
            //Call Service layer and get the data
            //Set into a model

        }
        catch( ServiceException e )
        {
           // Some exception occured
        }
        catch( Exception e )
        {
            //print error trace
        }
        //Student Found: Show student homepage
        return "student/homePage";  
    }

    //No Student Found: Show splash page
    return "student/noDataPage";
      }
 } 
t0mcat
  • 4,613
  • 19
  • 42
  • 57
  • Is the view that's being returned always `student/noDataPage`, or `student/homePage`? – Pat Jun 06 '11 at 18:33
  • The view its always returning is student/homePage (This was the old page). I added noDataPage and placed it outside the "if". – t0mcat Jun 06 '11 at 18:35
  • Use the debugger and find out why getRollNum() never returns null. If you cannot find out, post bundleStudentBean source. – abalogh Jun 06 '11 at 19:12
  • Hi abalogh, Thanks for the response. So issue is with the conditional check only, right? The logic would be the same for this purpose? Correct? – t0mcat Jun 06 '11 at 19:19
  • Check if you have a default non null values for getRollNum() method. Ex. If a String type, returning "" instead of null or if Integer type returning some non null value – Rajendra Jun 06 '11 at 20:18
  • I assume that getRollNum is a simple getter for a field. If the field is of type `int` and unset it'll end up being `0`. If the field is `Integer` and the return value of the getter is `int` it'll throw a `NullPointerException` when unset. – hleinone Jun 06 '11 at 20:54
  • Folks I will test it out and post back the results. – t0mcat Jun 06 '11 at 21:11

2 Answers2

1

Instead of checking whether the rollNum to null,better check whether it's value is zero. Chances are more that the function returns zero even when youi give no value into it.Most probably in the database you would have set the column to be not null and int

Deepak
  • 54
  • 2
  • 10
1

Good practice: Controller methods should be as lightweight as possible.

Bad practice: using Exceptions as control flow.

Spring MVC has a nice way of mapping business exceptions to custom views using ExceptionHandlers. I assume this is only one of the cases where a Controller is looking for a student and finds none - using ExceptionHandlers should help you write readable, lightweight Controllers.

Community
  • 1
  • 1
Brian Clozel
  • 46,620
  • 12
  • 129
  • 152