Friday 3 January 2014

Basic Struts2 Application in Eclipse

This tutorial demonstrates how to create a basic Struts 2 Application in Eclipse using Tomcat 7.0 server (A simple login application).

Before we start with the application we need the following:



Step 1: Create a dynamic web project in Eclipse.


Step 2: Enter the project name and make sure the target runtime is set to the downloaded Apache tomcat server as shown in the below picture and click on Next. If it’s set to ‘None’, Click on ‘New Runtime’ tab and select the downloaded Apache version from the list.

Step 3: Click again on next and make sure ‘Generate web.xml deployment descriptor’ field is checked on the last page before you click on finish.


Step 4: Add the following struts2 jars (present in the downloaded struts2 -> lib folder) in the
 WebContent -> WEB-INF -> lib location of the project.


Step 5: Edit the web.xml file ( WebContent -> WEB-INF -> lib) and the following code just after the  </welcome-file-list> end tag as follows:


The new web.xml should look as follows:


Step 6: Create struts.xml file in the Java Resources -> src folder of the project and copy the following code into struts.xml:



Step 7: Create a package ‘com.action’ and create a class named ‘TutorialAction’ within that package and paste the following code in the class file:


Step 8: Create 3 JSP files in Webcontent folder namely index.jsp, output.jsp and error.jsp and paste the following codes into the respective JSP files.


index.jsp

output.jsp

error.jsp

Step 9: The final directory structure should look as follows:



Step 10: Run the application by right clicking on the ‘MyFirstStruts2App’ and click on ‘Run on Server’. If you see the following screen everything went right. (In case you get some error check all the steps again if you have missed anything).



Enter the username as ‘admin’ and password as ‘admin123’ and click on Submit. You would get the welcome screen of output.jsp



Test your application by entering a wrong username or password and click on Submit. You would get the error screen of error.jsp





Notes:
  •    The index.jsp is created as the first page because it’s already present by default in the web.xml file. If you want to add a new file as the first page (e.g. home.jsp), add the appropriate file name in the <welcome-file-list> tag of web.xml
  •    Make sure you have all the jar files in the proper location. (In case you get an error of NoClassDefFoundError, it’s probably because of a missing jar file).
  • The username and password can be changed by modifying the execute method of the TutorialAction class.
  • By default the execute method is invoked whenever the action class is called and this method should always return a String which is mapped in the struts.xml. If you need to call your own method modify the struts.xml by adding a method attribute with the method name as the value in the action tag as follows:  
  •  The main entry point to the Struts2  part of the application is the filter tag part of the web.xml
  • The line <%@ taglib prefix="s" uri="/struts-tags" %> in the output.jsp and index.jsp is used to enable struts tags.



No comments:

Post a Comment