Play Framework: Controller Action Runs Every Load
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.
- Do you ever see a complex list in a URL
- 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
Post a Comment for "Play Framework: Controller Action Runs Every Load"