NoobProgrammer
NoobProgrammer

Reputation: 81

When url is change prompt page not found. Play! + Scala

I have the following URL on my webpage upon pagination

http://localhost:9000/employee?p=2

I need to prompt to not found page whenever the parameter "p" is change. example:

http://localhost:9000/employee?b=2

It need the controller to input a notFound. what kind of condition will i do to do this?

Reference:

Controller:

@Transactional(readOnly=true)
public static Result list(int pageNum, int listSize) {
  employeeMap.clear();
  Page page = appModel.page(pageNum, listSize);
  employeeMap = ListUtil.getEmpMap(employeeMap, page.getEmpList());
  AppModel employees = new AppModel(employeeMap, searchMap);
  /* if statement initiate a notFound page if pageNum us not the expected value */
  if (pageNum < 0 || pageNum > page.getPage().get("intLastPage")) {
  return notFound("<h1>Page not found</h1>").as("text/html");
}
  /* if statement that put a not search found message if no employee is found */
  if (page.getEmpList().size() == 0) {
  flash("success", "There is no search results for the specified conditions");
}
return ok(index.render(appModelForm.fill(employees),page));
}

Routes:

# Employee list (look at the default values for pagination parameters)
GET     /employee                   controllers.Application.list(p:Int ?= 1,l:Int ?= 125)

Upvotes: 2

Views: 42

Answers (1)

NateH06
NateH06

Reputation: 3574

You could prevent people from switching that name of the parameter overall by changing your routing. But to achieve all the possibilities outlined by what you want to do, you could do the following:

  GET     /employee/:p/:l        controllers.Application.list(p:Int ?= 1,l:Int ?= 125)
  GET     /employee/p/:p         controllers.Application.list(p:Int, 125)
  GET     /employee/l/:l         controllers.Application.list(1, l:Int) 

It depends on how you handle the URL calling in the template, but if you can have that auto-generate the default parameters into the URL if the user does not put them in, you could just keep the first one by itself.

The URL to summon your controller will now instead be:

    http://localhost:9000/employee/p/2
    http://localhost:9000/employee/l/4
    http://localhost:9000/employee/2/4

And then you can route anything else to a not found controller method:

 GET     /employee/--String, empty or whatever else---       controllers.Application.returnNotFound

@Transactional(readOnly=true)
public static Result returnNotFound() {

   return notFound("<h1>Page not found</h1>").as("text/html");

}

Upvotes: 1

Related Questions