Struts 1 Struts.xml, Action, Forms Example with Eclipse Step 11
Struts requires a Struts config file. Under your content directory and your WEB-INF folder, you need to create a xml file called struts.xml. This should be the same name of the struts file you mentioned in your web.xml. After you create the struts file, then paste the following code in the file if you are following my tutorial.
Struts.xml
<?xml version="1.0" encoding="ISO-8859-1" ?> |
At the top of the configuration file you have the DOCTYPE. Here is where you declare the version of your struts. Currently it is incorrect for the struts I am using, but I will show you how we will change this when I introduce tiles. You then have your form bean definitions. The form bean is the class objects that will be attached to your JSP paged. The form bean we are using will be called UserForm. I will show you the use of the Action Form shortly.
After the form beans you have your action mappings. These mappings are how the user will call your action classes. You will forward the user to where you want the user to go after the action is performed.
There are other options that you can put into your struts config file, and I will get into some of them later. The Action Class we are using in your struts.xml file is called UserAction.
So the next thing we need to do is create your Form and your Action classes.
Under your src (source folder) you should create two packages. You should already be familiar with Java's packages. You can create a package in eclipse by selecting the right mouse button on the src folder and select new and then package. Name one package called com.action and the other one called com.actionform. Create a class called UserAction under the com.action package and create a class called UserForm under the com.actionform package. Those should be the same name you used in your struts.xml file.
Paste the following code into your class:
UserAction.java
|
UserForm.java
|
Currently the UserForm is just a skeleton so we will get back to that later.
The UserForm class will extend the ActionForm class and the UserAction class will extend the DispatchAction class. All I have in this class right now is a System.out.println so you will know that you have arrived to this class. Do not get into the habit of adding a bunch of System.out into your program. You should use the log4j which I will get into later.
You will also want to create a temporary jsp page to visit. Create a page called user.jsp under the jsp folder which is in your WEB-INF folder. Call the file user.jsp and copy the below code into it.
user.jsp
|
The last file that you will create will end up becoming a login page. For now, we are going to redirect the user to an Action Class. The index.jsp page is the first page the user will come to when visiting your application. Create a file under your content directory called index.jsp and copy the below code. The code redirects the user to the Action class. After we build your WAR file, I will walk you through the system so you know how everything works.
index.jsp
|
The above code redirects the user to the UserAction.do class when they first arrive at your application. In my coming posts, I will show you how to create a WAR file using ANT, and how to use Tiles within your Struts Application.
Go To Step 12

2 comments:
i build war file using antBuilder but after put the UserBook.war file and start the tomcat server from browser when i typed
http://localhost:8080/UserBook it's automatically opening eclipse debuger and showing error:
source not found for index.jsp at line 1
i maintain exactly the struts directory structure what mention on this tutorial.
can any body solve this problem urgent. thanks in advance..
Hi hhjghj,
I am assuming that you have an index.jsp file located in the UserBook directory at its root.
In my index.jsp page, I have a redirector to forward user to the correct Action class. That Action class does exist correct? You should also make sure that the URL forwards the user to the correct action method in your Struts Action. If this doesn't help, why don't you post your web.xml file, your index.jsp page, and the Action class. If you post any code, you can make it friendly by visiting this url: http://www.elliotswan.com/postable/
Thanks...
<%
session.setAttribute("useractive","active");
String redirectURL = "/UserBook/UserAction.do?action=forwardToUser";
response.sendRedirect(redirectURL);
%>
Post a Comment