Call a Angular js page in Play framework - angularjs

I am finding trouble to display my AngularJs page from Play framework 2.2
The Angular js page is located in the same project directory which is
C:\webProj\test\app\www\index.html
Note this index.html is not the index.scala.html that we have in play views directory
I need to render this page from my Play project.
I have tried this
GET /masterid controller.Assets.at("/app/www/",index.html)
but it gives a compilation error.

Explanation of the error
There are some syntax errors in this route configuration:
GET /masterid controller.Assets.at("/app/www/",index.html)
It should be controllers instead of controller, the path is wrong, and the parameters are not specified correctly. It could be written as:
GET /masterid controllers.Assets.at(path="/app/www", file="index.html")
Note that as written, this route will map only to the index.html file, not to any other resources under /masterid.
Solution with a separate directory
To behave exactly as asked, with a separate directory and a custom URL, you would need to specity a second asset route in addition to the default one. This would also require changing all usages of #routes.Assets.at to specify two parameters (folder and file), and adding a configuration to build.sbt:
playAssetsDirectories <+= baseDirectory / "app/www"
Solution using the public directory and a custom URL
The path of least resistance is to create the custom index.html file in the project's public directory. To use a custom URL as asked in the question, you could change the default asset path to "masterid" by changing this line in the routes file:
GET /assets/*file controllers.Assets.at(path="/public", file)
to this:
GET /masterid/*file controllers.Assets.at(path="/public", file)
In this case the custom index.html file could be accessed as:
http://localhost:9000/masterid/index.html
Relative URLs to other resources under the /public folder would work as well.
Solution using the public directory and the default URL
If you don't require the /masterid URL under the root, you can save your index.html file under public/app and refer to it as:
#routes.Assets.at("app/index.html")
This will resolve to:
http://localhost:9000/assets/app/index.html
Documentation
For more extensive instructions see Working with public assets.

Make it faster, just place your file in i.e.: /public/angular-app/index.html, so you can use it via:
#routes.Assets.at("angular-app/index.html")
Next (assuming that you have standard routes) you can just use static paths to your public assets, i.e. if image is placed in folder public/img/logo.png you can access it with:
<img src="/assets/img/logo.png" alt=""/>
So just by replacing public/ to /assets/ (slash at beginning to make sure you don't need to use base tag in head of document).

Related

download sample file in public folder (react)

I want to put a sample xlsx file in my public folder (react project) and others can download this file.
<Button><link rel="chargeSample" href="%PUBLIC_URL%/chargeSample.xlsx"></link></Button>
How can I do this?
The tag defines a link between a document and an external resource.
To let download funcionality on your website you should change link tag
import chargeSample from './public/chargeSample.xlsx';
<Button>Download</Button>
First of all, we have to think about where you store the file, thinking about what react is going to do in build time. In develop, you can't trust 100% that the paths you use are going to be the same after building the app.
Here in the create react app doc there is some info about when to use the public folder and how to use it.
To download a file stored in the public folder, you have to reference it with the PUBLIC_URL. There are two ways of doing this:
Reference a file from inside the public folder
You will have to use de %PUBLIC_URL% as you mentioned.
For example, if you want to use a fav icon in the main HTML file, you have to add the icon in the public folder and then in the index.html you will reference this file with the %PUBLIC_URL% prefix.
// public/index.html
// ...other stuff
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
// ...
In build time, React will replace this hash with the path to that folder
Reference a file in the src folder
To reference a file from inside the src folder, you have to access the public folder URL. React saves this path in an environment variable. To be able to use that file you have to use the process.env.PUBLIC_URL path.
To download a file from the public folder, you can access it like this:
<a
href={process.env.PUBLIC_URL + "/my-file.txt"}
download={"file-name-to-use.txt"}
>
Download file
</a>
When clicking the anchor tag it will try to download the file stored in /public with the name my-file.txt and it will use as a placeholder name for that file the one specified in the download property.
If you are using creat react app, you can pass public path into your components at build time by using
process.env.PUBLIC_URL
I believe that Lucas gave the best answer. Just to add since I was looking for a way to programmatically create a link element (let's say when you received a new excel file from an API call in runtime/deployment) then you can do something as follows:
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', `${Date.now()}.xlsx`); //set the attribute of the <a> link tag to be downloadable when clicked and name the sheet based on the date and time right now.
document.body.appendChild(link);
link.click(); //programmatically click the link so the user doesn't have to
document.body.removeChild(link);
URL.revokeObjectURL(url); //important for optimization and preventing memory leak even though link element has already been removed.
Credits to top answers here:
How to download files using axios
How to download excel in response from api react.js

Url rewrite angularjs

so in my angularjs project i'm trying to change my url from
http:/localhost/(project name)/#/home?ref=(page name) to
http:/localhost/(project name)/home/(page name)
i have added url rewrite in iis.
also i have added html5mode true and base url,now when i try to run my project
all my resource files,app files points to
localhost/app/controllers/controllers.js instead of
localhost/(project name)/app/controllers/controllers.js..
now if i try to add my project name in base url it add project name twice like this,
localhost/(project name)/(project name)/app/controllers/controllers.js
Can some one please tell me where i'm wrong?or why is this not working?
Thank you.

How to serve a file if URL doesn't match to any pattern in Go?

I'm building a Single Page Application using Angular 2 and Go, and in Angular I use routing. If I open the site at, say, http://example.com/, Go will serve me my index.html file, which is good because I wrote this:
mux.Handle("/", http.FileServer(http.Dir(mysiteRoot)))
Now I have a route in Angular, let's say, /posts, and If it's a default route (that is, when useAsDefault is true) or if I just manually go to http://example.com/posts, I'll get a 404 error from Go, which means that no handler is specified for this path.
I don't think that creating a handler in Go for every single Angular route is a good idea, because there may be a lot of routes. So my question is, how can I serve index.html in Go if the request URL doesn't match any other pattern that I set in my ServeMux?
Well, that was pretty easy actually.
The net/http documentation says this:
Note that since a pattern ending in a slash names a rooted subtree,
the pattern "/" matches all paths not matched by other registered
patterns, not just the URL with Path == "/".
So I needed to do something with my "/" handler. http.FileServer looks for files in a directory that is specified in the pattern string, so I just replaced it with this:
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, mysiteRoot + "index.html")
})
And it works just fine.
I think you will need to change the URL provider settings in your angular2 app to use HashLocationStrategy. Using this, your routes will be of the form
#/posts
and will not trigger any route in your golang app.

Using $javascript helper issue in CakePHP: "Undefined variable: javascript"

I have problem when I want to use $javascript->link('prototype') in the default.ctp layout. It returns:
Undefined variable: javascript [APP\views\layouts\default.ctp, line 6]
I also added this code into app_controller.php:
<?
class AppController extends Controller {
var $Helpers = array('Html','Javascript','Ajax','Form');
}
?>
The file prototype.js is already in webroot/js folder.
Where is the problem?
I have had this problem many times. It's usually either caused by the controller code being overwritten somewhere or some weirdness happening with Cake's automagic stuff. If you remove all of your helpers and then add them one by one it will probably work eventually.
Another perfectly valid way of generating JavaScript links is by using the following which doesn't access the $javascript variable:
echo $html->script(array('prototype'));
It has to be $helpers instead of $Helpers.
You just open the error console of the Firefox browser (shortcut key ctrl+shift+j).
Find the error and click on it.
After clicking, you will see the head portion.
Note the location of the JavaScript file (*.js) which you want to locate (you will see the location is not correct).
Cut the JavaScript file from webroot and paste it in given location of head block.
Example:
This will show on error console. Map_demo is my project, and in its place your project name will display:
<script type="text/javascript" src="/map_demo/js/test.js"></script>
Cut the JavaScript file from webroot
Make the JavaScript folder in your project application folder, /map_demo/js
Paste test.js (your script file) here
Now your JavaScript function will work.
Just in case somebody else comes across this bug/issue: it was also happening to me, until I commented out the line $session->flash(); in my default layout. Realising that the error was being caused by flash messages, I went back to the controller and noticed that I was using separate layouts for flash messages (e.g. 'message_alert') and that those layouts didn't actually exist in the view folder!
So remember, errors like this could mean that a file is not defined yet. Best of luck.

Linking a CSS or JS to default layout

I'm trying to undestand how to link CSS or JS I'll use it all over my CakePHP 1.3 application. I've read about putting assets in folder /app/webroot/css or /app/layouts/css (for css only in this case).
I've put a file named main.css and default.css but I'm missing something.
How can I do to fix it and which are the default rules for the default layout?
Normal
$this->Html->css('my_file'); corresponds to /app/webroot/css/my_file.css
$this->Html->script('my_file'); corresponds to /app/webroot/js/my_file.js
you add the php part from above to your layout file which is by default in /app/views/layouts/default.ctp (or in cake dir if you have not created one)
you can set the layout in the controller/app_controller setting $this->layout = 'foo'; which points to /app/views/layouts/foo.ctp
Themes
Setting the controller to $this->view = 'Theme'; will make cake use themes then setting $this->theme = 'SomeTheme'; in the controller will make cake use /app/views/themed/some_theme/* files
using $this->Html->script('my_file'); now points to /app/views/themed/some_theme/js/my_file.js and the same goes for the css.
css = http://book.cakephp.org/view/1437/css
js = http://book.cakephp.org/view/1589/script
themes = http://book.cakephp.org/view/1093/Themes
themes have the problem of serving css, js and other assets through php (ob_start(); include etc) and this is obviously slower than normal http serving. you can either copy files across to the webroot folder as explained in the bottom or be lazy and do something like the following https://gist.github.com/712622

Resources