|
|
How to Set Up Eclipse for Web-application Development
Russell Bateman Spring 2010 last update:
|
|
Step by step
- Create a new workspace (unless you want to use the relevant parts of this
step-by-step to convert an existing one). Launch Eclipse and choose or switch
to this new workspace.
-
- If you have workspace preferences saved (exported) from a previous
workspace that you'd like to see respected in this new one, do that now before
forgetting. (Or, you can do it later.)
For example, you may like to set the width of your Java code to 120 and show
a line in the editor window so that you don't go past it. This is done by
- Going to
Window -> Preferences -> General -> Editors
-> Text Editors
.
- Clicking Show print margin and typing "120" into
Print margin column.
- Tab width in spaces is also set there as is whether to show line
numbers. Etc. There are many tailorings you may wish to pass from
workspace to workspace.
To export these, ...
- Go to
File -> Export... -> General -> Preferences
.
- Click Next, then Export all (or you can get clever,
if you like).
- Name a preference file path, such as
C:\Users\russ\dev\russ-eclipse.epf.
- Click Finish and ensure your preferences were put into the file
you named.
-
- If you have workspace preferences saved that govern how newly set-up code
is automatically formatted by Eclipse, go to
Window -> Preferences
-> Java -> Code Style -> Formatter
, click Import and
then navigate to find the template you saved away after modifying the format.
I obsess over my code format and save my settings to
java-templat-formats.xml.
-
- Establish the workspace JDK by choosing
Window -> Preferences
-> Java -> Installed JREs
. What will be there already is the JRE
installed on your computer host for executing Java programs. In my humble
opinion, you don't want this; see
Setting up the Sun Java Developer's Kit.
To change it, click on the Location path, then click the Edit
button. Navigate to where you've created a copy of Sun's Java Development Kit.
For example, mine is at C:\Users\russ\dev\downloads\jdk1.6.0_20. While
you're in the JRE Definition dialog, be sure to update the JRE
name: so it's obvious that you're using your special one. The name can be
anything you choose; I chose jdk1.6.0_20. You should see something like
this:
-
- Now establish your server container environment. This is what you will use
to run and test your web applications. I'm choosing Tomcat 7 because I get a
lot more "JEE" cooperation, specifically for JavaServer Faces work, out of that
version (although Tomcat 6 works very well too); see
Setting Up Apache Tomcat.
Assuming you have this downloaded, unzippped and available, do this:
- Go to
Window -> Preferences -> Server > Runtime
Environments
.
- Click Add, then select the name that most closely corresponds to
your choice of environment. I'm choosing Apache Tomcat v7.0.
- Click Create a new local server to check. This will set up the
server in your Project Explorer view that you will be using to
run and test.
- Click Next.
- For the Tomcat installation directory:, click Browse
and go find the server you downloaded earlier. Or, you can click the
Download and Install... button (but I've not always been pleased
with that option which installs it for your whole computer).
- Click Finish.
-
- Finally, let's set up the JavaServer Faces (JSF) and JavaServer Pages
Standard Tag Library (JSTL) for JSP work. If you're doing something different,
you may not need or even want to set these up. Moreover, I make a choice here
you may not want in that I'm choosing to use Apache MyFaces. Besides this
option, there are many other implementations of Faces like Mojarra and
RichFaces.
The easiest way to set up these libraries is to create your first project. This
can be a bogus project if you like. We'll delete it afterward.
- Right-click in the Project Explorer view and choose
New. Find the Dynamic Web Project wizard; you may have
to look under
Other... -> Web
.
- Name the project bogus.
- Set the Configuration to JavaServer Faces v1.2 Project.
- Click Next thrice (until you see JSF Capabilities.
- Click the Manage libraries... icon
().
- Use the User Libraries dialog to set up the two sets of JARs.
This dialog is also available from the
Build Path operations you will become
acquainted with when using Eclipse.
First, fetch the downloads we need.
- Get Apache MyFaces
from here. Choose the relevant download link such as
myfaces-core.1.2.8-bin.zip for Windows or
myfaces-core.1.2.8-bin.tar.gz for Linux and Macintosh. I
downloaded it to the path C:\Users\russ\dev\downloads and unzipped
it, reduced the subdirectory depth, etc. so that I could reference it in
the next step as
C:\Users\russ\dev\downloads\myfaces-core-1.2.8-bin\lib.
- The JSTL download is harder to locate because of how unintuitive Sun has
made it by mixing it in with GlassFish. You can look around by Googling
"jstl download", but I found it
here. I clicked on Download
JSTL, then on each of JSTL API and JSTL Implementation
as we need both of these JARs. I put them into a subdirectory named
jstl-1.2 on the path C:\Users\russ\dev\downloads.
Follow these steps to set up the JSF and JSTL libraries:
- In the User Libraries dialog, click New... and type
JSF 1.2 (Apache MyFaces) as the name. Click OK.
- While the new library is selected, fill it with JARs by clicking
Add JARs..., then navigating to where you've downloaded Apache
MyFaces.
- Select all the JAR files on the path myfaces-core-1.2.8-bin/lib
and click Open. This will add them to the JSF library we
created.
- Click New... again and create JSTL 1.2. Add the two JARs
it will contain similarly to how you added those for the JSF library.
You should now see something similar to the following:
Dismiss the User Libraries dialog, then click to check both JSF and
JSTL boxes to cause them to be added to the JSF capabilities for this
workspace. Each time you set up project facets for a new Dynamic Web Project in
Eclipse, you should automatically get these libraries.
Finished!
At this point, you're ready to create your first Dynamic Web Project in
Eclipse. You can delete the bogus project from your workspace.
Appendix A: web.xml
The file web/WEB-INF/web.xml for most projects should appear thus. I
have removed the <description>
elements for brevity and
enhanced the <welcome-file-list>
over what Eclipse
sometimes generates.
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
Faces Servlet
javax.faces.webapp.FacesServlet
1
Faces Servlet
*.faces
/faces/*
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
javax.servlet.jsp.jstl.fmt.localizationContext
resources.application
javax.faces.STATE_SAVING_METHOD
client
org.apache.myfaces.ALLOW_JAVASCRIPT
true
org.apache.myfaces.PRETTY_HTML
true
org.apache.myfaces.DETECT_JAVASCRIPT
false
org.apache.myfaces.AUTO_SCROLL
true
faces
org.apache.myfaces.webapp.MyFacesServlet
1
faces
*.jsf
faces
*.faces
org.apache.myfaces.webapp.StartupServletContextListener
</web-app>
Appendix B: faces-config.xml
The file web/WEB-INF/faces-config.xml for most projects should appear'
thus, usually with a different managed-bean name, a different to-view-id name,
etc. This file is extremely variable and this is only a simple example of what
to expect.
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
/index.jsp
login
/welcome.jsp
user
com.corejsf.UserBean
session
</faces-config>