Download - Struts framework
![Page 1: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/1.jpg)
![Page 2: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/2.jpg)
Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
![Page 3: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/3.jpg)
STRUTS FRAMEWORK
Subhin P [email protected]/
subhinvelayudhantwitter.com/111subruin.linkedin.com/in/Subhin P V+91-8129076036
![Page 4: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/4.jpg)
Apache Struts• Struts Frame work is the implementation of
Model-View-Controller (MVC) design pattern for the JSP.
• Struts is maintained as a part of Apache Jakarta project
• Struts is open source.
![Page 5: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/5.jpg)
What is Model-View-Controller (MVC) Architecture?
• Model-View-Controller architecture is all about dividing application components into three different categories Model, View and the Controller.
• Components of the MVC architecture has unique responsibility and each component is independent of the other component.
• Changes in one component will have no or less impact on other component.
![Page 6: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/6.jpg)
MVC ARCHITECTURE
![Page 7: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/7.jpg)
Struts Architecture
• Struts is an open source framework used for developing J2EE web applications using Model View Controller (MVC) design pattern.
• It uses and extends the Java Servlet API to encourage developers to adopt an MVC architecture.
![Page 8: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/8.jpg)
Components
Struts 2 Core components are Action handler, Result Handler and Custom Tags. Action handler
Action handler interacts with other layers.Result Handler
Result handler actually dispatches the response to view.
Custom TagsCustom Tags are used render the dynamic content.
![Page 9: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/9.jpg)
Components
• InterceptorsThe Interceptors are used to specify the "request-processing lifecycle" for an action. Interceptors are configured to apply the common functionalities like workflow, validation etc.. to the request. Interceptors code is executed before and after an Action is invoked
![Page 10: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/10.jpg)
How Struts Works
• In struts JavaServerPages (JSP) are used to design the dynamic web pages.
• In struts, servlets helps to route request which has been made by the web browsers to the appropriate ServerPage.
• The use of servlet as a router helps to make the web applications easier to design, create, and maintain.
![Page 11: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/11.jpg)
STRUTS ARCHITECTURE
![Page 12: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/12.jpg)
Process flowUser Sends request.FilterDispatcher determines the appropriate
action.Interceptors are applied. Execution of Action. Output rendering. Return of Request(reverse order). Display the result to user.
![Page 13: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/13.jpg)
Things to download before you start struts
• Requirements (Downloads)– JDK ver. 1.5 and above.– Tomcat 5X and above(Tomcat ver.6 is better)– Eclipse ver. 3 and above.– Apache struts2 jar files needed• common-logging-1.0.4.jar• freemarker-2.3.8.jar• ognl-2.6.11.jar• struts2-core-2.0.12.jar• xwork-2.0.6.jar
![Page 14: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/14.jpg)
Example program using struts
We suppose to create a login application using struts.
![Page 15: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/15.jpg)
Getting started
• Open eclipse and go to fileNewDynamic project in the new project wizard screen.
• You will have a screen consisting of some wizards as shown in the next slide.
• After giving the project name click Finish.
![Page 16: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/16.jpg)
Getting started
![Page 17: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/17.jpg)
Getting started
• Once you created the project, you can see its structure in the project folder.
• After these processes, now copy all the jar files listed in the previous slides to WebContentWEB INF
![Page 18: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/18.jpg)
Structure of project folder
![Page 19: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/19.jpg)
Import jar files
![Page 20: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/20.jpg)
Mapping program to xml
• Open web.xml file which is under WEB-INF folder and copy-paste the following program.
![Page 21: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/21.jpg)
Mapping program to xml• <?xml version="1.0" encoding="UTF-8"?>• <web-app id="WebApp_9" version="2.4"• xmlns="http://java.sun.com/xml/ns/j2ee"• xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"• xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">• • <display-name>Struts2 Application</display-name>• <filter>• <filter-name>struts2</filter-name>• <filter-class>• org.apache.struts2.dispatcher.FilterDispatcher• </filter-class>• </filter>• <filter-mapping>• <filter-name>struts2</filter-name>• <url-pattern>/*</url-pattern>• </filter-mapping>• <welcome-file-list>• <welcome-file>Login.jsp</welcome-file>• </welcome-file-list>• • </web-app>
![Page 22: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/22.jpg)
Class and methods
• Create a class called LoginAction in com.test.struts2package.
• Note that, above action class contains two fields, username and password which will hold the values from form and also contains an execute() method that will authenticate the user. In this simple example, we are checking if username is admin and password is admin123.
• Also note that unlike Action class in Struts1, Struts2 action class is a simple POJO class with required attributes and method.
• The execute() method returns a String value which will determine the result page. Also, in Struts2 the name of the method is not fixed. In this example we have define method execute(). You may want to define a method authenticate() instead.
![Page 23: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/23.jpg)
![Page 24: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/24.jpg)
Create resource file
• Resource bundle is a java entity that helps in putting the static content away from the source file.
• Here we should define an application file and should be named as ApplicationResources.properties files
• To create it the method is on the next slide
![Page 25: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/25.jpg)
Resource folder
![Page 26: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/26.jpg)
Resource folder
• Click that source folder option and you will be having a following screen.
• And set the source folder as Resources folder.• In that folder create a file called
ApplicationResources.properties .
![Page 27: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/27.jpg)
Resources
• Copy these contents into ApplicationResources.properties
• label.username= Username• label.password= Password• label.login= Login• error.login= Invalid Username/Password. Please try again.
![Page 28: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/28.jpg)
Jsp files to run the application
Login.jsp
![Page 29: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/29.jpg)
Jsp files(2)
Welcome.jsp
![Page 30: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/30.jpg)
Jsp(3)
• Above programs are made in the folder Struts2_HelloworldWebContentWEB-INFLogin.jsp.
• Struts2_HelloworldWebContentWEB-INFWelcome.jsp.
![Page 31: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/31.jpg)
Create a struts file
• Struts2_HelloWorldJava Resourcesresourcescreate file struts.xml
• Copy the following content into struts.xml
![Page 32: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/32.jpg)
Struts.xml
Copy the above text into struts.xml.
![Page 33: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/33.jpg)
Important note
• Our LoginAction.java class is having a method called execute(). If the name of the method is different, e.g. authenticate(). Then we should specify the name of the method in the action tag.
![Page 34: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/34.jpg)
Action
• You can see <s: submit method command in the program Login.jsp
![Page 35: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/35.jpg)
Running the application• Now select Login.jsp then select the run on
server command.• After selecting the command, you will be
having a server lists out of that, select apache-tomcat server version.6. then run the program.
• You will be having the following screen on your browser.
![Page 36: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/36.jpg)
![Page 37: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/37.jpg)
Screens(1)
Enter user_name as admin and password as admin123
![Page 38: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/38.jpg)
Screen(2)
If you login successfully, you will get a screen as output.
![Page 39: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/39.jpg)
Screen(3)
If you login into the application as a wrong user, the appearing screen will be like this. You are instructed to renter the user and password again
![Page 40: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/40.jpg)
That’s all folks
![Page 41: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/41.jpg)
If this presentation helped you, please visit our page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
![Page 42: Struts framework](https://reader034.vdocuments.us/reader034/viewer/2022042614/5560c9d1d8b42a0d088b49a9/html5/thumbnails/42.jpg)
Contact Us
Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Start up VillageEranakulam,Kerala, India.
Email: [email protected]