eclipse dynamic web project file locations

2019-04-24 17:45发布

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 

5条回答
乱世女痞
2楼-- · 2019-04-24 18:11

Here is a sample folder structure of a dynamic web project: enter image description here

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}

查看更多
狗以群分
3楼-- · 2019-04-24 18:26

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"

  1. Create a dynamic Web project if you have not already done so.
  2. 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.
  3. 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.

查看更多
我命由我不由天
4楼-- · 2019-04-24 18:30

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.

查看更多
ら.Afraid
5楼-- · 2019-04-24 18:31

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

查看更多
SAY GOODBYE
6楼-- · 2019-04-24 18:32

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.

查看更多
登录 后发表回答