Eclipse Ant Tutorial Building War File Struts Step 12
Now that you have your Struts set up with an Action class, a Action Form, the Struts.xml, a jsp page, your libraries and the web.xml files, you are ready to create your War file and test your configuration on the Tomcat Server. If you haven't gotten this far, see my prior posts on setting up your file structure in eclipse. You now need to create a build.xml file.
Create a build.xml file under the UserBook directory. The build.xml file should be at the same tree level as your src file. If you are following my tutorial, paste the following code into your build.xml file.
build.xml
|
At the top of the build.xml file you have the project name which in our case is UserBook.
Looking at the project settings, you will see a list of variables that will be used in your build.xml file. The environment="env" is pulling in the environmental variables that will be used. We need to know the environmental variables to know what the Tomcat path is. The Project_Home is the home of your current project. To use the Project_Home variable, you can put it like this: ${Project_Home}. So the other variables are using the Project_Home variable to declare variables in other areas of your project. We have one that goes to your library directory, your src directory, your content directory, antBuild directory and your classes directory.
The first thing you do when building your project is clean the project by deleting the Ant Build directory.
The next thing you do is to initialize the system by stamping a time date on your war file, and then making sure you have the ant build directory created.
The depends says "clean" which means that this target depends on clean. That means that clean will happen first, and then the init will take place.
The next target you have is compile which compiles your java code and puts the classes in your ant build directory.
Below that you also create a jar file. This jar file can be used later if you need to use these classes in another project. You have a copy that copies the jar file into the lib directory in your ant build directory. Within the war file section, you want to tell ant what files to include in the war file.
After you create your war file, you will copy the war file to your tomcat webapps directory.
Now you need to start your tomcat server to see if everything is working correctly.
The first time you build your file you need to select the right mouse button on your build.xml file and select "run as" ant build. After the first time you run the build, you can select the green icon at the top of eclipse to build the project.
After you build your file, you should see printing in your console that will tell you the order and what is taking place while eclipse is building your War file. First, the build file should be cleaning your system, then running init, then compile, then create your jar files, and then it will create your war file. If your build was successful, it will say Build Successful. If there was a problem, then eclipse will give you an error that the code didn't compile correctly and you will need to fix it.
Before starting Tomcat, I usually will delete the directory from the webapps folder. You don't have to delete the War file, but just the directory that says UserBook. After you build your War file, you should be able to look in this directory and see a file called UserBook.war which is the file you just created.
If you installed the Tomcat plugin for eclipse, you should see a cat at the top of the menu in eclipse. When you select the cat, tomcat will start and you should see information in your console. If you see any stack traces of Java code while Tomcat is starting, then there might be a problem with your xml mappings. If you see
INFO: Server startup in 27813 ms then your Tomcat was started successfully.
Now you need to open a browser and goto this URL to see if your project is working correctly.
http://localhost:8080/UserBook/
The localhost is the tomcat on your machine, and the 8080 is the port number for this Tomcat instance. The UserBook is the name of your project and your War file.
If you see "This is a test" then your project is working and you successfully configured Struts 1.
Your index.jsp page forwarded the user to your UserAction.do which is in your struts mapping. Struts will call the action method in your action class called forwardToUser. After the forwardToUser method is finished, it will return a mapping.findForward("success") which will read the struts.xml file and send the user to the user.jsp page.
In my next post I will show you how to set up tiles with struts. Tiles is used to seperate your application into sections to make it easier to manage.

6 comments:
Thanks for your effort, this tutorial is really nice,
I am getting "Servlet action is unavailable"
Please help me
Sorry I didn't look at your comment earlier. If you are still getting that error, post some of your stack trace, and your struts config file and your web.xml file. Thanks.
Hi Greg,
Hope you are doing good.
Once I completed up to this step I got only a blank page, so I added another method called "execute" in the UserAction class and deleted "?action=forwardToUser" in the index.jsp.
After that I got the desired out put. Can you imagine where I got wrong here.
Thanks for your blog, I am enjoying with your writings regarding java/struts/hibernate.
You will use the execute method when extending action class. For example:
public class UserAction extends Action
If you extend Action, then you will use the execute method. In our case, we want to extend DispatchAction. This way, you can name your action methods.
Make sure your struts action mapping contains the parameter="action" like this:
<action path="/UserAction" name="userForm" type="com.action.UserAction" parameter="action" scope="session">
Thank you so much greg
sure, anytime.
Post a Comment