I'm creating a new dynamic web project in Eclipse and was wondering what best practices are for folder taxonomy. Here's what I believe it is <> are folders. Can someone please verify?
<Eclipse project name>
<src>
-- .java files
<WebContent>
-- .html pages
<images>
<css>
<js>
<META-INF>
MANIFEST.MF
<WEB-INF>
web.xml
<app name>
-- .jsp pages
Here is a sample folder structure of a dynamic web project:
As you can see all static files are placed as sub-folders under the WebContent folder. By naming conventions .css files are places in the css sub-folder. JavaScript .js files are placed under the js sub-folder and any image files such as .jpeg or .png are placed in the images sub-folder. I also have an extra lib sub-folder where I placed an angularjs library to be used.
By default after creation of a dynamic web project your web.xml file looks like so:
`<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>`
meaning it will first call the listed default name files when you run your application. This is why most projects will name the files as index.html or index.jsp. NOTE: that my index.html file is directly below the WebContent folder and not in a sub-folder
Finally you can call/include your static files (.css .js and image files) from your 'index' file like so:
<link rel="stylesheet" href=css/bootstrap.min.css>
<link rel="stylesheet" href=css/bootstrap-theme.min.css>
<script type="text/javascript" src="lib/angular.min.js"></script>
<script src="js/contactsApp.js"></script>
Also your .java files will properly go in the Java Resources -> src -> {place java files here}
Put your pages under WEB-INF folder, in that way they cannot be accessed directly.
Also look at maven directory layout http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html.
To what Aleksandr M said,
WebContent folder:
The mandatory location of all web resources, including HTML, JSP, graphic files, and so on. If the files are not placed in this directory(or in a sub directory structure under this directory), the files will not be available when the application is executed on the server.
WEB-INF
Based on the Sun Microsystems Java Servlet 2.3 Specification, this directory contains the supporting Web resources for a Web application, including the web.xml file and the classes and lib directories.
Source: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.wst.webtools.doc.user%2Ftopics%2Fccwebprj.html
I am not sure why having an app-name directory under WebContent would be considered a "best practice".
Other than that, one primary rule you should be following when coming up with a directory structure is to have all static resources under one directory. In your example, I would have a subdirectory called static under WebContent, and place the js, css and images directories under it.
That way, it'd be easier for you to (later on) configure your HTTP server to pick static resources directly from the file system rather than route requests for static resources through the servlet container.
I had this question too and can't comment yet, but Upendra Bittu's answer helped me.
http://help.eclipse.org/neon/index.jsp
Search 'jsp', click on "Creating JavaServer Pages (JSP) files"
Create a dynamic Web project if you have not already done so.
In the Project Explorer, expand your project and right click on your WebContent folder or on a subfolder under WebContent. Note that
if you choose any other folder in which to create the JSP, then it
will not be included in the WAR file that is deployed to the server.
In addition, link validation will not encompass files that are not
under the WebContent folder.
From the context menu, select New > JSP. The New Java Server Page window appears with your folder selected
I'm trying out tutorials and get lost when people don't say where they create their files, and this helped me understand what's going on, so I'm just passing it on.
Related
I created Dynamic Web Project In Eclipse and created some html pages.
I have another Maven Project Of Java REST API. In Maven Project src->main->webapp->WEB-INF , I pasted my html, so i want to access this html pages, so what should be the path ?
Please Check Image,
Your UI resources should not be under WEB-INF folder. You should place your html and CSS folder directly under webapp. You can then access it by URL http://localhost:port/InteractDemo/html (port must be modified as per the server)
Always I store an all my front-end sources in Site Root Folder.
But today I update netbeans to 8.1 and I notice the Source Folder in the project properties.
What this folder for?
It is good explained in NetBeans Help -> Project Properties Window: Sources
Site Root Folder
The contents of this folder will be available through the web server on the production site. It contains the HTML, CSS and JS files that are publicly accessible/visible. This folder typically contains the index.html file.
Source Folder
The folder in the project that serves for server-side sources (typically node.js JS files). If the project is a JS library, JavaScript files should be placed in this folder (not in the Site Root Folder).
When you use Grunt or Gulp you should store sources and final site and different folders.
I created a Dynamic Web Project in Eclipse and wanted to use some font faces in the JSP files. I added a .css file to the project in Eclipse and put the following code in the file:
#font-face{
font-family: "Font Name";
src: url("FontName.ttf") format("truetype");
}
I put the FontName.ttf file manually (NOT by adding it using Eclipse) in the WebContent folder. .css file also lies in the same folder.
Problem:
I am using Tomcat server and whenever the browser requests this file, server responds with 404 Resource Not Found . Why does this occur?
Checks done:
The web.xml in of Tomcat contains appropriate mime types to handle the .ttf files. Specifically, these lines are present in this file:
<mime-mapping>
<extension>ttf</extension>
<mime-type>application/x-font-ttf</mime-type>
</mime-mapping>
I also put the FontName.ttf file in the main project directory. That was also not working.
I had thought that the problem was caused because of not adding the .ttf through Eclipse? If this is indeed the case, how can I add this file to the project using Eclipse? I could not find an appropriate way.
First sails didn't create .tmp/public, so i did it manually. But it also doesn't copy stuff from my assets folder to my public folder. Can someone explain why that is?
#
At that time, the answers i got weren't helping,
I've updated to 9.8 now, and i don't seem to have any problem.
#
I had this same issue. When running sails lift the .tmp folder wasn't created. What finally worked for me was installing Grunt locally in the root folder of my sails app. So just run npm install grunt in your sails app folder. Having Grunt installed globally with the -g flag was apparently not enough. After the local installation, you can run sails lift again, and the .tmp folder will be created.
Hope this helps!
Where do I put my css and javascript assets in sails?
Sails uses grunt to manage assets. Some of this "management" involves syncing files between your project folder structure and the server's public folder, but as always, I'm getting ahead of myself.
The configuration of grunt is based upon the Gruntfile.js file found in the root of your sails project. There’s a lot going on in this file, however, I’m going to concentrate on the javascript and css assets.
Your Project's Assets
When you first create a project, you have the option of using the --linker flag. An example of using the flag would be sails new projectName --linker. Here’s the directory structure of the /assets folder under both scenarios:
USING the --linker flag
/assets
/images
/linker
/js
/styles
/templates
NOT USING the --linker flag
/assets
/images
/js
/styles
Note, you can “upgrade” a project that wasn't created with the --linker flag by manually creating the /linker folder and inserting it into your /assets path. You can then add /js, /styles, and /templates under /linker.
The Server's Public Folder
When starting the sails server via sails lift the following folder structure is created/sync'd via grunt within the .tmp folder:
.tmp
/public
If any of the other project folders (e.g. /images, /js, /styles, /templates) contain content they are copied/sync'd to the .tmp/publicfolder. The distinction being that if a /linker folder exists, the /js, /styles, and an additional /templates folder is created under /linker.
What happens to my layout.ejs file?
If you use the /linker folder, sails will alter your layout.ejs file to include links to your javascript and css files. Therefore, any page served from the project's /views folder will have access to the javascript and css contained in these files.
Grunt uses commented tags in layout.ejs to as placehodler for these links. For example, anything placed in the /style folder will automatically be linked in layout.ejs between these two tags:
<!--STYLES-->
<!--STYLES END-->
Anything in the /js folder will be linked between these two tags:
<!--SCRIPTS-->
<!--SCRIPTS END-->
Anyting in the /templates folder will be linked between these two tags:
<!--TEMPLATES-->
<!--TEMPLATES END-->
Accessing Sail's Assets
Here's how you access the assets under either scenario:
USING the /linker folder
/js --> /linker/js/yourFile.js
/styles --> /linker/styles/yourCSS.css
NOT USING the /linker folder
/js --> /js/yourFile.js
/styles --> /styles/yourCSS.css
It didn't appear that Grunt was doing anything on my installation including copying the assets folder. I found this post on the Google Group by Rob Wormald that finally got it working for me:
In your .sailsrc file, in the root of your project, remove the line that says "grunt" : false. That should get things working.
This was an issue with one of the generators that I believe should be corrected in the next release.
You will need to check Gruntfile.js in your sails project root directory and everything will be much easier to understand. Here is some short explanation:
Sails 'magic' during lift process are hidden in Grunt tasks.
If Sails not create .tmp/public directories in your project, it can be because permissions or something similar (its happen on Windows as I know, I not have it on Linux). Solution is to create manually .tmp/public directories and to be sure that is writable.
To get your assets copied to .tmp/public you will need to keep it inside assets/linker directory, or to update Gruntfile.js based on your specific need.
I hope this help :).
In Eclipse, the folder structure when I create a Dynamic Web Project is
[srikanth#hana Sample]$ ls -R
.:
build src WebContent
./build:
classes
./build/classes:
./src:
./WebContent:
index.html META-INF scripts WEB-INF
./WebContent/META-INF:
MANIFEST.MF
./WebContent/scripts:
jquery-1.7.1.js
./WebContent/WEB-INF:
lib web.xml
./WebContent/WEB-INF/lib:
As you can see, there is this WebContent directory, When I copy the directory structure from Eclipse workspace and put in webapps directory of Tomcat, it didn't work. But, if I moved all the directories and files under WebContent directory a level above, it worked fine.
This is the folder structure in Tomcat's webapps directory under application:
[srikanth#hana Sample]$ ls -R
.:
build index.html META-INF scripts src WEB-INF
./build:
classes
./build/classes:
./META-INF:
MANIFEST.MF
./scripts:
jquery-1.7.1.js
./src:
./WEB-INF:
lib web.xml
./WEB-INF/lib:
So, now I can just go to http://localhost:8080/Sample and can go to index.html properly
What am I doing wrong? Why didn't it work just copying the application
from Eclipse to tomcat webapps directory directly? Why do I have to
change the directory structure?
I had the same problem, ./WebContent/index.html not appearing in the Eclipse-exported .war. This occurred on my new Eclipse system after I recreated a working project from my old system with Kepler Eclipse to a new system with Luna Eclipse.
I fixed this by going to Project / Properties / Deployment Assembly. I discovered that the old working system had this rule, while my new non-working systems was missing this rule ...
Source = /WebContent
Deploy Path = /
I corrected this on the new system by Pressing "Add..." and adding the missing rule.
Eclipse allows deploying the webapp directly to a server, without needing to copy anything by yourself. It also allows generating a deployable war file when you're ready to deploy to a test or production server.
Open the "Servers" view, right-click, and choose to create a new server. Once created, right-click on the server and choose to add your web-app into it. And then Eclipse will deploy your web-app to the server.
You're not supposed to manually copy anything from Eclipse. And if you need or want to do this, then you should probably use some ant script which generates the proper deployment structure. This structure is described in the servlet specification. It should have, under the root directory of the webapp, a WEB-INF directory containing:
classes: a directory containing your classes
lib: a directory containing all the jar files your app depends on
web.xml the webapp's deployment descriptor
All the other directories and files that are not under WEB-INF can be served by the web container.
When I copy the directory structure from Eclipse workspace and put in
webapps directory of Tomcat, it didn't work. But, if I moved all the
directories and files under WebContent directory a level above, it
worked fine
Not sure what you mean if I moved all the directories and files under WebContent directory a level above, it worked fine here and what you mean by saying a level above.
What you should be doing is not copy anything manually but right-click on the project and select Export as WAR option.
This will create a file named Project.war that contains the proper file structure i.e. WEB-INF etc that you are supposed to put under tomcat's webapps dir