Spring static content(css) not served properly (spring boot) - reactjs

I have a spring boot app and a controller that server static webpage(React build):
#Controller
#RequestMapping("/test")
public class HomeController {
#GetMapping("/")
public String index() {
return "index.html";
}
...
index.html is located at: ../resources/static/index.html
also in application.yml:
spring:
mvc:
static-path-pattern: /test/**
I am having two problems(problem 2 is the main issue):
I must call the following url with the trailing '/' at the end: http://localhost:8100/test/ I would like for http://localhost:8100/test to also map me to the view(index.html).
during the load of the page I am getting the following error:
the problem as you can see is that the url called is:
http://localhost:8100/static/css/main.6c417d20.chunk.css
and not
http://localhost:8100/test/static/css/main.6c417d20.chunk.css
(please note that the reason for the 'static' in the url is that there is a folder named: static below the resources/static folder so there is no issue with the 'static' in the url)
is it a server side problem or is it something I should fix in the react?
I searched for an answer but didn't find anything helpful.
any help would be highly appreciated,
Tnx

So the answer to my question lies in the following links:
how to build react to a non root path(homepage):
build react non root path
registering zuul client to the following path(that contains all resources):
Zuul configuration with resources
so I am leaving this here in case someone has the same issue(the answer in the second link is for vue.js and webpack,the first link explains how to change root address in react).

Answer 1 : #RequestMapping has a String[] value parameter, so can specify multiple values like this:
#RequestMapping(value={"", "/", "welcome"})
Answer 2 : You are expecting test in URL which is controller mapping not the project context path so it should not come in static resources urls.
see this answer for more clarity adding css and js in spring boot.

Related

Copy static files from one Jekyll post to published site

I would like to copy any static file (image, PDF, etc.) found in a post folder inside _posts to the folder in which the HTML version of the post will be, inside _site.
Let's say I have this structure:
_posts/
2016/
06/
09-so-long-cloudflare/
2016-06-09-so-long-cloudflare-and-thanks-for-all-the-fissh.md
cloudflare-logo.png
performance-report-sample.pdf
My Jekyll settings for permalinks are:
# Permalinks
permalink: /:year/:month/:day/:title/
I would like to generate the site like this:
2016/
06/
09/
so-long-cloudflare-and-thanks-for-all-the-fissh/
index.html
cloudflare-logo.png
performance-report-sample.pdf
I've found this plugin that should do this, but I can't make it work. I get this error:
jekyll 3.1.6 | Error: undefined method `name' for #<Jekyll::Document:0x007fb7a0892b50>
Any idea?
Thanks!
Well, despite having no Ruby knowledge, I managed to build a plugin out of this old Gist! \o/
https://nhoizey.github.io/jekyll_post_files/
I hope this will help people with the same needs.

Symfony3 and Sonata-adminBundle:dev-master Admin route is not automatically generated

I'm using symfony3 and sonata-project/admin-bundle:dev-master. my problem is that when I generate an Admin class by the command line: sonata:admin:generate, now when I try to access the admin dashboard in order to see my admin class...I get the following error: An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "admin_pa_pacomplaint_create" as such route does not exist.") in SonataAdminBundle:Block:block_admin_list.html.twig at line 39.
Edit: Event bin/console debug:router does not show the route of the newly created admin class
Sorry for my question where so easy: I just needed to update the app/config/routing.yml file with the following:
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin

Configure beego.Router to serve index.html for AngularJS non-server routes

I have a route beego.Router("/", &controllers.MainController{}) where it serves the index.html and all other routes are server APIs such as beego.Router("/api/products", &controllers.ProductController{}).
How to configure the beego's router to serve index.html for all others routes that supposed to be handled by ui-router in angularjs such as /products?
beego.Router("/", &controllers.MainController{}) or beego.Router("", &controllers.MainController{}) doesn't work
In my case, I solved the problem by adding following route to my path
beego.Router("/*", &controllers.MainController{})
type MainController struct {
beego.Controller
}
func (c *MainController) Get() {
c.TplName = "index.html"
c.Render()
}
Simply it returns index.html for any other path and angularjs deal with the rest.

Laravel returning root view instead of static content

I have one route:
Route::controller('/', 'HomeController');
And HomeController has one action getIndex which works correctly. However, when browsing to http://localhost:4040/public/style.css, the application returns the view from HomeController::getIndex. What gives?? Shouldn't it return the static file style.css?? I have confirmed that the CSS file does in-fact exist in the public folder.
To be clear, I'm running the app like this:
php -S localhost:4040 server.php
EDIT:
Hmmm, so the following seems to work:
1) Serve the project with:
php artisan serve
2) Reference files without the public/ prefix.
What does the php artisan serve command do so differently??
With using Route::controller(), if you look at the php artisan routes, the getIndex route has a optional parameters {one?}/{two?}/... and since that parameters have no matching pattern style.css get caught by the {one?}. This is the completely expected behavior, with index.php.
However, using php artisan serve, the server.php file gets called first.
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = urldecode($uri);
$paths = require __DIR__.'/bootstrap/paths.php';
$requested = $paths['public'].$uri;
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' and file_exists($requested))
{
return false;
}
require_once $paths['public'].'/index.php';
You can clearly see that it checks if the file is real in if ($uri !== '/' and file_exists($requested)), and if it is, it stops calling the index.php that may process it as a route parameter, and instead returns a real file.

CakePHP 2.1 new install Missing Controller Error

I am completely new to CakePHP and installed 2.1. I am getting this error:
Missing Controller
Error: Index.phpController could not be found.
Error: Create the class Index.phpController below in file: app\Controller\Index.phpController.php
<?php
class Index.phpController extends AppController {
}
Notice: If you want to customize this error message, create app\View\Errors\missing_controller.ctp
Stack Trace
APP\webroot\index.php line 96 → Dispatcher->dispatch(CakeRequest, CakeResponse)
ROOT\index.php line 40 → require(string)
I followed their guide at http://book.cakephp.org/2.0/en/installation/advanced-installation.html and tried everything it stated:
I enabled mod_rewrites (they were already enabled from something else)
I have all the .htaccess files in the directories
I have cake installed under my document root so I access it at localhost/cakephp/index.php
I do not know where to proceed from here. Thanks for any help you can give me.
Update: I just re-read your question and realized you're loading http://localhost/cakephp/index.php. Don't do that. Since you appended "index.php", it is trying to load a controller called "index.php" and the action "index" for that controller. Resulting mapped path to the "index.php controller" is app\Controller\Index.phpController.php.
Since you have the rewrites enabled, browse to http://localhost/cakephp without appending any filename.
Original answer:
Assuming you're using Apache, double-check the .htaccess in your /app/webroot directory. It should include the following:
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
Based on your error, it doesn't look like it's properly appending the path after your index.php file.
If you are using cakephp 2.0 or greater than rename the controller file name as the class name.
ie TaskController.php
class TasksController extends AppController {
--Your code inside class
}
I hope this will help you
the easiest solution is to stick to the "live environment" as close as possible.
this means using vhosts to use a "domain" and correctly root down to your webroot dir:
http://www.dereuromark.de/2011/05/29/working-with-domains-locally/
this leaves almost no room for error and also helps with other potential problems like "absolutely linked asset files" etc

Resources