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";
      }
 } 
7
  • Is the view that's being returned always student/noDataPage, or student/homePage? Commented Jun 6, 2011 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". Commented Jun 6, 2011 at 18:35
  • Use the debugger and find out why getRollNum() never returns null. If you cannot find out, post bundleStudentBean source. Commented Jun 6, 2011 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? Commented Jun 6, 2011 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 Commented Jun 6, 2011 at 20:18

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.