Where to put static files for Spark Web Framework? - java

Where do I put files when trying to serve static files with the Spark web framework?
I haven't been able to find anything online - I'm beginning to suspect I don't understand anything about class paths, relative paths etc. for an Eclipse and Java project.
This paragraph about static files in Spark refers to /public, but I have no idea where that would be. Using windows, Eclipse Luna and my project is converted to use Maven.
I've tried looking at the code on GitHub, but I'm a little out of my depth trying to find it.

First you have to tell Spark where to search for the static files like this:
Spark.staticFiles.location("/public");
In Spark versions prior to 2.5, you should use:
Spark.staticFileLocation("/public");
Then your project should have a public folder under the resources folder like this
/src/main/resources/public/style.css
For example I added a style.css file there, so you should then access it like this:
http://localhost:4567/style.css
If you want to serve a non-classpath folder, then you should use
Spark.staticFiles.externalLocation("/path/to/dir");
In Spark versions prior to 2.5, you should use:
Spark.externalStaticFileLocation("/path/to/dir");

I put my style sheets below my static content as follows:
staticFileLocation( "/web" );
/web/
|-- index.html
+-- styles/
+
+--- default.css
And the index.html
... <link href="styles/default.css" rel="stylesheet" type="text/css" />
I also have other generated HTML pages as with freemarker. They just collect the path:
/styles/default.css, or
localhost:8081/styles/default.css
Shows the CSS way index gets it.
Source: https://groups.google.com/d/msg/sparkjava/5vMuK_5GEBU/vh_jHra75u0J

Right click your project on Eclipse, select create New -> Package. Give the new package a name, etc.
Put your static resources under that package, so we can be sure they're under your classpath.
In your Main class colde, call staticFileLocation("yourpackagename/");

Place your public directory into src/main/resources
Replace Spark.staticFileLocation("/public"); to Spark.staticFileLocation("public");

Related

CSS File Not Load In Maven Project

My Maven Project Structure is Like Below.I want To Load Css In mainMenu.jsp File.
<link rel="stylesheet" href="../Main.css">
Above Line Is Not Working.So How Can I Load Main.css in mainMenu?
Don't confuse your WEB-INF layout with what the browser sees. Move your Main.css to "webapp" (or, more commonly "webapp/style"), and then reference it as "Main.css" (if in webapp) or "style/Main.css" (if in webapp/style).
Under WEB-INF, you typically only want files which are processed server-side, eg. JSP (when using a ViewRenderer) or web.xml.
Spring Boot automatically adds static resources located within any of the following directories as mentioned here:
/META-INF/resources/
/resources/
/static/
/public/
Hence, the mistake you are making is by placing static content in wrong directory.
I Have Solved Issue By Adding Below Line OF Code In Web Configuraion And Add All Front End File In resources Folder.
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}

How to externally add a javascript file to a JSP in a maven project?

I have a maven project with spring-mvc configurations with the hierarchy mentioned in below image. I've placed list.js file inside all the possible places in src folder and its sub folders but I couldn't access it from my list.jsp file. What am I doing wrong here? Can anyone help me to figure it out?
Static Resources like JS, CSS, Images,etc visible from webapp folder in a web application.
So, you can create js folder under webapp folder and place your list.js file inside it.
Also, you need to tell Spring to not process these static resources path.
Looks like you had defined your Spring Configuration in Java Classes i.e. WebConfig. You need to add below code
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
}
Lastly, change your path in jsp file like this
<script
src="${pageContext.request.contextPath}/js/list.js"></script>
Hope it helps to solve your problem.

Spark Framework and relative path

I'm using the Spark framework to create a web app and I've been going through the tutorials on their site. At the moment, I'm working on their templates section, however I can't seem to get the project to recognize that my css and my templates are in my resources folder.
I'm using Netbeans and Maven to manage my dependencies.
Can anyone help me figure out how to set up my relative paths/create my project folders appropriately in this environment? I'm a newbie to both Maven and Spark, so go easy please.
Static Files:
If your resources directory looked like this:
resources
└───public
├───css
│ style.css
├───html
│ hello.html
└───templates
template.ftl
You could use staticFiles.location("/public"). This would make /public the root staticFiles directory.
You could then access hello.html like this: http://{host}:{port}/html/hello.html
If you wanted to use an external location on the filesystem, you could use staticFiles.externalLocation(...), which works pretty much the same way above.
Note: staticFiles.externalLocation(...) can be set to your project's resources directory, which means that the files will be automatically refreshed (useful for development)
A more in depth explanation can be found in the spark documentation
Configuring your template engine:
If you have already set the staticFiles location, but spark is still having trouble finding your templates, try this.
Note: These examples are for the FreeMarker engine, though they should apply to other engines with minor tweaking.
After looking through the examples, it seems that by default, a new FreemarkerEngine() looks for templates in spark/template/freemarker, and not your staticFiles location.
You have two options:
1: Move all of your templates to that directory
or
2: Configure your own engine, and pass it instead when defining routes
FreeMarkerEngine freemarker = new FreeMarkerEngine();
Configuration config = new Configuration();
config.setTemplateLoader(
new ClassTemplateLoader(YOUR_CLASS.class, "/templatedir"));
freemarker.setConfiguration(config);

How to make a local JBOSS AS 7 serve static resources?

As a standard, my static resources need to stay in a centralized location, out of my EARs / WARs (so that they can be updated without rolling out a new release for minor JS/CSS changes).
My HTML imports are relative, eg:
<script type="text/javascript" src="/resources/myApp/js/bootstrap.min.js"></script>
This means that the URLs to resources are different on every server (development, test, production..):
On dev-foobar.com:
dev-foobar.com/resources/myApp/js/bootstrap.min.js
On tst-foobar.com:
tst-foobar.com/resources/myApp/js/bootstrap.min.js
On foobar.com:
foobar.com/resources/myApp/js/bootstrap.min.js
But when I'm on my LOCAL Jboss (localhost:8080), how can I instruct it to serve the same static resources to achieve the following URL ?
localhost:8080/resources/myApp/js/bootstrap.min.js
JBoss handles all the static resources through a folder called welcome-content under $JBOSS_HOME:
C:\Program Files\EAP-6.2.0\jboss-eap-6.2\welcome-content
Simply put something there, and it will be exposed in the root context.
For example, it's sufficient to copy the sample folders and file described in the question:
C:\Program Files\EAP-6.2.0\jboss-eap-6.2\welcome-content\resources\myApp\js\bootstrap.min.js
to end with the file (but not the folders) being accessible through the URL:
localhost:8080/resources/myApp/js/bootstrap.min.js
Well done, Red Hat !

adding external js in gwt

I writed and checked my js code in GWT.
For checking I added my js code in (projectName).html file and it is worked.
But when I try added external js file I get an error:
WARN] 404 - GET <path to js file>someJsFile.js (127.0.0.1) 1452 bytes
Request headers
I added this line to (projectName).gwt.xml file:
<script src="src/main/resources/<projectName>/someJsFile.js"></script>
To use this technique you have to place your someJsFile.js in your public folder so as the gwt compiler copy it to the final folder where it places the html and js stuff.
If you are using maven you have to check if the resources plugin is copying this file to the war and in which path.
By the way, there are other techniques to insert external javascript in your document:
Placing the script tag in your .html file.
Using ScriptInjector.fromUrl().
A better approach is to use a TextResource and ScriptInjector.fromString() so as the compiler reads the javascript file and includes the content in the final compiled file.
You can use gwtquery Ajax.getScript to get the script via ajax and inject it in the dom.
A new way, recently added to gwtquery, allows you to include your javascript as a JSNI block, so as the compiler can optimize and obfuscate it.
I'd rather the last one because it offers much more advantages.
You can define in your_project.gwt.xml which folders to include as public. The paths should be relative to the xml:
resources/
|-your_project.gwt.xml
|-subfolder/
|-stuff/
|-images/
|-js/
|-someJsFile.js
In your xml add:
<public path="subfolder/stuff" />
This should copy images/ and js/ folders into your webapp directory and you can use sth like this for the js file
<script src="js/someJsFile.js"></script>

Categories

Resources