0
votes

I'm new to the Play framework. I attempted to compile my play framework project following a sample. When I compile the sample, it works fine and has the .scala.html view compiled in the target as .class. I added a new view, but it didn't compile into the target. Any suggestions how to fix this? I tried activator compile with command line, cleaning and re-building the project, building .scala.html individually, but none of the attempts worked. How do you add a new view and compile it in Play 2.4?

package controllers;

import models.Client; 
import models.Server;
import models.TestEvent;
import models.TestPlan;

import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import views.formData.testFormData;

public class Application extends Controller {

// Default path request
// public Result index() {return ok(index.render("Your new application is ready."));}


/* Index Route Page
 * Returns the page where the form is filled with the arguments passed
 * Or an empty form if the id is 0
 */
public static Result getIndex(long id) {
    testFormData testData;
    // Find the corresponding index result to return (depends on the id)
    if(id == 0)
        testData = new testFormData();
    else
        testData = models.TestEvent.makeTestFormData(id);

    Form<testFormData> formData = Form.form(testFormData.class).fill(testData);
    return ok(index.render(formData,
            Client.getClientNameList(),
            Server.getServerNameList(),
            TestPlan.getTestPlanNameList()));
}


// Process a form submission
// Bind HTTP Post data to an instance of testFormData
// If errors are found, re-render the page displaying the error data
// If errors are not found, re-render the page displaying good data
public static Result postIndex() {
    // Retrieve the formData
    Form<testFormData> formData = Form.form(testFormData.class).bindFromRequest();

    // The retrieved formData has errors
    if(formData.hasErrors()) {
        return badRequest(index.render(formData,
                Client.getClientNameList(),
                Server.getServerNameList(),
                TestPlan.getTestPlanNameList()));
    }

    // The formData does not have errors
    else {
        // Convert the form data into a testEvent Instance
        TestEvent testEvent = TestEvent.makeTestEventInstance(formData.get());

        return ok(index.render(formData,
                Client.getClientNameList(),
                Server.getServerNameList(),
                TestPlan.getTestPlanNameList()));
    }
}
}

Routes:

GET     /                           controllers.Application.getIndex(id:Long ?= 0)

 POST    /                           controllers.Application.postIndex()



 # Map static resources from the /public folder to the /assets URL path
 GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)
1
Is your server running and do you have a controller action which returns the newly created view? What do you get when you try to call the controller action from a web browser? This also requires a change in routes.conf.Rado Buransky
The error on the web browser is "value getIndex is not a member of controllers.Application" on: GET / controllers.Application.getIndex(id:Long ?= 0) I configured the routes.conf to match the action from the application.java file. But the views are not in the target.TypeSource
Don't care about the view yet, either your routes are wrong or the controller. Controller is not invoked. Views are compiled when needed so try to fix this issue first.Rado Buransky
The controller is the issue. In my IDE, the only error that I get is "Cannot resolve symbol 'index' ", where index is one of my views, which is why I went back to the issue of views. I attempted to import the views, but they also don't show up.TypeSource

1 Answers

0
votes

If the server isn't running while you're coding, changes won't be compiled. You can remedy this by turning on the continuous compilation feature, with compilation occurring every time a change is written to the file system.

To do this:

  1. Start activator
  2. Use ~compile

The ~ indicates changes should be compiled immediately.

You can do something similar with the server. By using ~run, changes will be compiled immediately on file system changes and not delayed until the browser is refreshed.