Skip to content Skip to sidebar Skip to footer

Play Framework: Controller Action Runs Every Load

Does anyone know why my action runs every time I reload a page. I have a page with a button that should run when I click on it. But now it seems to run when I load the page. Here i

Solution 1:

A little explanation:

When writing in your Scala view and you say

@Application.generateExcel(currentPage)

It runs the function generateExcel in controller Application

What to do

You don't want it to run the function immediately. You want it to go there onClick

So use

@routes.Application.generateExcel(currentPage)

This outputs a link to that function

However for this to work there has to be a (GET) link to that function in your routes

Add this to routes

GET  /whatever/:thing                 controllers.Application.generateExcel(thing: List<path.InfoObject> list)

HOWEVER

This is a bad idea.Why? Because putting your entire list in a URL just isn't nice.

  1. Do you ever see a complex list in a URL
  2. URLs have to be less than 2000 characters

WHAT TO DO INSTEAD

Send the list as a POST data. Depending on how you take in your data you'll have to figure it out

Solution 2:

If you are trying to just call a controller when clicking on a button, you could try :

<ahref="@routes.YourController.YourMethod(args)"><button>Mybutton</button></a>

I don't think you need javascript here (if I understood your situation).

Edit: The idea of this answer is to say that the less javascript in your page, the better.

Edit2 : Can't comment the discussion below, so I put it here: As I said here : link, your have to declare your object like this :

controllers.Application.method(list : java.util.List[your.package.Infoobject])

Replace your.package with the package in which your object is (maybe models)

But you will get an errror : No QueryString binder found for type

This is because you can only put Strings and numerals in URLS, so the framework tells you to transform your Object (List) in a String (with the QueryStringBinder).

Solution 3:

I would call routes-> /generateExcelReport from the button/link and make it return the file

How to send a file to browser for downloading?

How to download a file with Play Framework 2.0

Post a Comment for "Play Framework: Controller Action Runs Every Load"