scalaimage-processingmoduleplayframework-2.3broken-image

Why is image-src method-return-type instead of URL/route for Play Framework Module Controller?


I have created a Play Framework module "Ean2BarcodePlayModule" which includes this template "barcode.scala.html" saved in the "views.barcodePackage.tags" package:

@(ean: Long)
<img class="barcode" alt="@ean" src="@controllers.barcodePackage.BarcodeController.getBarcode(ean)" />

After using "activator publish-local", I've then referenced this in the "index.scala.html" template of a test project:

@(message: String)

@main("Welcome to Play") {

    @barcodePackage.tags.barcode(5010255079763L)

}

The expected result is

<!-- Redacted for brevity -->
  <img class="barcode" alt="5010255079763" src="/barcodeRoutes/5010255079763">
<!-- Redacted for brevity -->

But the actual result is:

<!-- Redacted for brevity -->
  <img class="barcode" alt="5010255079763" src="Action(parser=BodyParser(anyContent))">
<!-- Redacted for brevity -->

As will be noted, the controller's action method method's return-type appears where instead the source URL should be, but I don't understand why that is happening or know how to fix it.

The module includes this line in "barcodePackage.routes":

GET     /:ean   controllers.barcodePackage.BarcodeController.getBarcode(ean:Long)

Meanwhile, the route file in the test project contains:

->      /barcodeRoutes              barcodePackage.Routes

This is the code for the module controller:

package controllers.barcodePackage


import models.barcodePackage.Barcode
import play.api.mvc.{Action, Controller}
import play.api.libs.concurrent.Execution.Implicits._
import scala.util.{Failure, Success}

/**
 * Created by Brian_Kessler on 3/11/2015.
 */
object BarcodeController extends Controller
{
  def getBarcode(ean:Long) = Action.async{
      Barcode.renderImage(ean) map {
        case Success(imageData) => Ok(imageData).as(Barcode.mimeType)
        case Failure(error)     => BadRequest("Couldn't generate bar code. Error: " + error.getMessage)
    }
  }

}

Notes:

  1. Most importantly, I'd like to see the module functionality working as expected.

  2. But I'd also prefer a solution which allows me to successfully specify routes within the module, rather than be required to transcribe them all individually within projects which incorporate the module.


Solution

  • With thanks to Sarvesh Kumar Singh whose response helped put me on the right track, I've figured this out.

    So, as I came to understand, the problem was not that the template was not finding the controller, but rather that it was calling the method in the controller instead of the reverse router for the controller.

    So, what I really needed to do was tweak the module's template (all the other changes were probably irrelevant) to make sure it called the router instead.

    With my present version of play, this meant changing barcode.scala.html to:

    @(ean: Long)
    <img class="barcode" alt="@ean" src="@controllers.barcodePackage.routes.BarcodeController.getBarcode(ean:Long)" />
    

    Note, the source does NOT start with "@routes", but rather "routes" comes between the package name ("barcodePackage") and the controller name ("BarcodeController").