I'am trying to run multiple independent Play Framework 2.2 projects on one single Play server.
Problem is that when I use run or start to start the application, then the "sub"-project shows the content of the "root"-project. I have read everything avaliable, but I can't figure out a solution :/
Here's the build.sbt:
lazy val aaaroot = project.in(file(".")).aggregate(project1).dependsOn(project1)
lazy val project1 = project.in(file("project1"))
The routes file of the "root"-project:
GET / controllers.Application.index()
GET /assets/*file controllers.Assets.at(path="/public", file)
-> /project1 project1.Routes
The routes file (called project1.routes) of the "sub"-project:
GET / controllers.project1.Application.index()
GET /assets/*file controllers.project1.Assets.at(path="/public", file)
The Application.java Controller of the subproject uses package controllers.project1;
I also tried to set up the "Router object" in the application.conf file of the "sub"-project ( application.router=project1.Routes ). As described here.
Every suggestion is highly appreciated!!!
Found the error. The controller is used correctly, but there is a name-conflict with the views.
My Application.java says:
package controllers.project1;
import play.*;
import play.mvc.*;
import views.html.*;
public class Application extends Controller {
public static Result index() {
return ok(index.render("Project1"));
}
}
The error is the line import views.html.*;. This calls the views from the main-project. To avoid it I had to rename the view-folder to something else and edit the import like import viewsofproject1.html.*;
Related
Though in dev mode it is possible to upload files by ref.move to() method in public folder or any app folder in play framework and rendering that file by AssetsFinder 's path() method. But in prod mode uploading file in public folder or app folder is not allowed. In this case what I can do to access the app outer folder files and what will be the actual rendering method to render the file (images) in template?
Looking for a detail solution....
package controllers
import javax.inject.{Inject, Singleton}
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents}
#Singleton
class ImageServerController #Inject()(cc: ControllerComponents)
extends AbstractController(cc) {
def serveImages(imageName:String): Action[AnyContent] = Assets.versioned("/tmp/images",imageName)
}
config file
GET /test controllers.TestController.test
GET /dynamicassets/*file controllers.ImageServerController.serveImages(file)
accessing views
package controllers
import javax.inject.{Inject, Singleton}
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents}
#Singleton
class TestController #Inject()(cc: ControllerComponents)(implicit assetsFinder: AssetsFinder)
extends AbstractController(cc) {
def test:Action[AnyContent]=Action{
Ok(views.html.testview("hotelapp.jpeg"))
}
}
here view
#(fileName:String)
<img src="#routes.ImageServerController.serveImages(fileName)">
Here are two options :
1) After the upload of the file, you move it to a folder (for example /uploads) created in your app directory. Then you serve these images via Apache which is more adapted than play to serve assets.
You need to have Apache as a reverse proxy of your application and configure a url to serve assets instead or redirecting to your application.
2) You also move the file to a folder (/uploads) and then you create a route in you application
GET /img/:name controllers.MyController.serveImage(name: String)
and in your controller :
public Result serveImage(String name) {
String path = "uploads/" + name;
return ok(new File(path));
}
The benefit of 2) is that you can manage authorisations when serving the file depending on the user.
Ah,
We need to use Streaming HTTP responses to do this.
Here is the documentation....
https://www.playframework.com/documentation/2.6.x/ScalaStream
my controller code will be
package controllers
import akka.stream.scaladsl.{FileIO, Source}
import akka.util.ByteString
import javax.inject.{Inject, Singleton}
import play.api.http.HttpEntity
import play.api.mvc._
#Singleton
class ImageServerController #Inject()(cc: ControllerComponents)(implicit assetsFinder: AssetsFinder)
extends AbstractController(cc) {
def serveImages(imageName:String):Action[AnyContent] = Action {
val file = new java.io.File("/tmp/images/"+imageName)
val path: java.nio.file.Path = file.toPath
val source: Source[ByteString, _] = FileIO.fromPath(path)
Result(
header = ResponseHeader(200, Map.empty),
body = HttpEntity.Streamed(source, None, Some("image/jpeg"))
)
}
}
I am testing a few Java API, I've created my project called 'MyLearning' where all my src files are located, in src I created another Package callede 'myfiles', now when I import the java.nio.file.Files API, IntelliJ doesn't show me suggestions for this class. But in the main package i.e src folder, the suggestion works totally fine.
Example:
The above picture shows my main src folder, where the Files API works totally fine.
But then in the new Package that I've created i.e myfiles, it is showing error on retrieving the methods of Files API. Error is
Cannot resolve symbol 'exists'
Can anyone tell me what could be the poblem here?
You have to put method calls inside a method.
public void foo()
{
Files.exists(path);
}
I also noticed that one of the tags you put is intellij-14. The latest version of IntelliJ is 2016.2.
You have to call it in a method, not in the class
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
Path path = Paths.get("C:\\log.txt");
System.out.println(Files.exists(path));
}
}
I am trying to implement my custom View with secure social
"ws.securesocial" % "securesocial_2.11" % "3.0-M3",
But I am getting some error:
object TemplatesPlugin is not a member of package securesocial.controllers
So I visit the github project and found there is no TemplatesPlugin
https://github.com/jaliss/securesocial/tree/3.0-M3/module-code/app/securesocial/controllers
package app.com.myApp.plugin;
import play.api.mvc.{RequestHeader, Request};
import play.api.templates.Html;
import securesocial.controllers.TemplatesPlugin;
import securesocial.core.{SecuredRequest, SocialUser};
import play.api.data.Form;
import securesocial.core.SecureSocial._;
import securesocial.controllers.PasswordChange.ChangeInfo;
class MyViews(application: App) extends TemplatesPlugin {
override def getLoginPage[A](implicit request: Request[A], form: Form[(String, String)],
msg: Option[String] = None): Html =
{
views.html.Secure.login(form, msg)
}
//...
}
and my play.plugins
1500:com.typesafe.plugin.CommonsMailerPlugin
9997:app.com.myApp.plugin.MyViews
Then How I go further, What to change?
The module does not use Play plugins any more. All what used to be a plugin is now a service that needs to get configured in the RuntimeEnvironment for your app.
Check out the sample apps to see how they provide their environments. You will need to override the viewTemplates attribute to use your custom views.
I am using play 2.2.1 and trying to implement Page for 404 requests.
For that I have created a errorPage.scala.html in my views and created a Global class inside MyProject->app->controller
Global.java (Source)
import play.*;
import play.mvc.*;
import play.mvc.Http.*;
import play.libs.F.*;
import static play.mvc.Results.*;
public class Global extends GlobalSettings {
public Promise<SimpleResult> onHandlerNotFound(RequestHeader request) {
return Promise.<SimpleResult>pure(notFound(
views.html.errorPage.render(request.uri())
));
}
}
But Its not working.When I am entering the wrong url it shows action not found page and display my routes file in browser.
Is there anything I am missing?
I have been trying other stack threds also but din't get any solution.
Thanks
Your Global class needs to be at the root package, i.e. within app and not wihtin the controller package. The link you posted documents that in the second sentence:
Defining a Global object in your project allows you to handle global
settings for your application. This object must be defined in the root package.
This might be a duplicate, but I cannot find an answer using the below answers and many other sites on the internet...
My conundrum:
I am attempting to (poorly) run some classes from a jar in jsp. Effectively what I have is the following:
<%#page import="edu.cs242.hadoop.*" %>
<%
... do some stuff ...
MRSearcher ss = new MRSearcher();
... do some stuff ...
%>
But every time I try to run the jsp I get the following error:
An error occurred at line: 32 in the jsp file: /hadoop.jsp
MRSearcher cannot be resolved to a type
My webapp structure looks like:
/
|hadoop.jsp
|lucene.jsp
|index.jsp
|WEB-INF/
|lib/
|lucene.jar
|hadoop.jar
|classes/
|*.java for our hadoop.jar
I've tried calling the jar itself and compiling the java through tomcat, both produce the same results.
Here is a snippet from our MRSearcher class:
package edu.cs242.hadoop;
import java.io.*;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
/**
* Created by cloudera on 3/10/14.
*/
public class MRSearcher {
MRSearcher() {}
public String[] run(String arg1, String arg2) {
String[] things = new String[] {};
// do stuff
return things;
}
}
There are other classes, but this one is the one that allows us to interface with the rest of the program. The main is in a file called: Main.java, and it does nothing but runs this for command line output. The syntax is correct as we can run the main and retrieve output.
I don't mean to sound insolent, but please don't comment on the futility and awfulness of including things like JAR files in JSP. This is never going to production, it's a school project that doesn't need the necessity of correctness, it needs the necessity of functioning. If I were doing this for a job I would do it right, but right now I don't care about learning about the correct way to separate logic and presentation layers in JSP -- I can do that just fine in other languages and understand the concept very well.
I have looked through and attempted to use the following solutions before posting this, all of this has failed:
how to run jar file methods in jsp
How to call method from jar file in JSP?
how to reference an external jar in jsp app?
And more to try to solve this problem.
In WEB-INF/classes/ you must have MRSearcher.class file. Keep in mind JSP files are compiled first time you access it, so your project can compile although your JSP are not well.
On the other hand, your MRSearcher() constructor must be public, if you omitte this, by default the constructor will be package and cannot be accessed from other packages. Check if your Main.java class is in same package of MRSearcher. If yes, this is why it can invoke MRSearcher constructor.
I hope this helps you.
Regards,