[go: up one dir, main page]

0% found this document useful (0 votes)
101 views11 pages

How Struts Works

Apache Struts is an open source MVC framework for developing Java web applications. Struts 2 uses a servlet filter as the controller to map request URLs to actions. Actions handle requests and determine the result view. Interceptors provide cross-cutting functionality before and after actions. The example shows integrating Struts 2 with Hibernate by using a DAO class to save user registration data to a database configured via Hibernate files. Spring can also be integrated with Struts 2 by defining a ContextLoaderListener in web.xml and specifying action classes as beans in configuration files.

Uploaded by

rina mahure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views11 pages

How Struts Works

Apache Struts is an open source MVC framework for developing Java web applications. Struts 2 uses a servlet filter as the controller to map request URLs to actions. Actions handle requests and determine the result view. Interceptors provide cross-cutting functionality before and after actions. The example shows integrating Struts 2 with Hibernate by using a DAO class to save user registration data to a database configured via Hibernate files. Spring can also be integrated with Struts 2 by defining a ContextLoaderListener in web.xml and specifying action classes as beans in configuration files.

Uploaded by

rina mahure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Struts

Apache Struts is an open source framework for developing Java Enterprise web
applications. It uses Java Servlet API to implement the web applications based on
Model-View-Controller (MVC) design pattern.
As the latest version of Struts is currently Struts 2, we describe the Struts 2 framework
here.

1. How Struts works


Struts 2 MVC is realised by three core framework components: actions, results, and the
ServletFilter. The diagram below shows how these three components interact with each
other.

1.1. Servlet Filter


The servlet filter acts as a controller in Struts 2. It inspects each incoming request to
determine which Struts 2 action should handle the request. The framework handles all of the
controller work for the application. All request URLs need to be mapped to actions with
XML-based configuration files or Java annotations.

1.2. Interceptors
Interceptors execute before and after the request processing. They provide cross-cutting tasks
so that they can be easily reused as well as separated from other architectural concerns. For
example, validation parameters before invoking login action.
1.3. Action
Action handles the client requests in two different ways. First, the action plays an important
role in the transfer of data from the request through to the view, whether its a JSP or other
type of result. Second, the action must assist the framework in determining which result
should render the view that will be returned in the response to the request.

1.4. Result
Result is a JSP or HTML page to create a view for client response. Struts 2 provides their
own tags that we can use in JSP pages to create a response. Struts tags are great
example of JSP Custom Tags.

Struts with hibernate

Hibernate and Struts 2 Integration


1. Hibernate and Struts2 Integration
2. Example of Hibernate and struts2 integration

We can integrate any struts application with hibernate. There is no requirement of


extra efforts.

In this example, we going to use struts 2 framework with hibernate. You need to have
jar files for struts 2 and hibernate.

Example of Hibernate and struts2 integration


In this example, we are creating the registration form using struts2 and storing this data
into the database using Hibernate. Let's see the files that we should create to integrate
the struts2 application with hibernate.

o index.jsp file to get input from the user.


o User.java A action class for handling the request. It uses the dao class to store
the data.
o RegisterDao.java A java class that uses DAO design pattern to store the data
using hibernate.
o user.hbm.xml A mapping file that contains information about the persistent
class. In this case, action class works as the persistent class.
o hibernate.cfg.xml A configuration file that contains informations about the
database and mapping file.
o struts.xml file contains information about the action class and result page to be
invoked.
o welcome.jsp A jsp file that displays the welcome information with username.
o web.xml A web.xml file that contains information about the Controller of Struts
framework.

index.jsp

In this page, we have created a form using the struts tags. The action name for this
form is register.

1. <%@ taglib uri="/struts-tags" prefix="S" %>  
2.   
3. <S:form action="register">  
4. <S:textfield name="name" label="Name"></S:textfield>  
5. <S:submit value="register"></S:submit>  
6.   
7. </S:form>  

User.java

It is a simple POJO class. Here it works as the action class for struts and persistent class
for hibernate. It calls the register method of RegisterDao class and returns success as
the string.

1. package com.javatpoint;  
2.   
3. public class User {  
4. private int id;  
5. private String name;  
6. //getters and setters  
7.   
8. public String execute(){  
9.     RegisterDao.saveUser(this);  
10.     return "success";  
11. }  
12.   
13. }  

RegisterDao.java

It is a java class that saves the object of User class using the Hibernate framework.

1. package com.javatpoint;  
2.   
3. import org.hibernate.Session;  
4. import org.hibernate.Transaction;  
5. import org.hibernate.cfg.Configuration;  
6.   
7. public class RegisterDao {  
8.   
9. public static int saveUser(User u){  
10.           
11. Session session=new Configuration().  
12. configure("hibernate.cfg.xml").buildSessionFactory().openSession();  
13.           
14. Transaction t=session.beginTransaction();  
15. int i=(Integer)session.save(u);  
16. t.commit();  
17. session.close();  
18.           
19. return i;  
20.   
21. }  
22.   
23. }  

user.hbm.xml

This mapping file contains all the information of the persitent class.

1. <?xml version='1.0' encoding='UTF-8'?>  
2. <!DOCTYPE hibernate-mapping PUBLIC  
3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
5.   
6. <hibernate-mapping>  
7. <class name="com.javatpoint.User" table="user451">  
8. <id name="id">  
9. <generator class="increment"></generator>  
10. </id>  
11. <property name="name"></property>  
12. </class>  
13.             
14. </hibernate-mapping>  
hibernate.cfg.xml

This configuration file contains informations about the database and mapping file. Here,
we are using the hb2ddl.auto property, so you don't need to create the table in the
database.

1. <?xml version='1.0' encoding='UTF-8'?>  
2. <!DOCTYPE hibernate-configuration PUBLIC  
3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
5.   
6. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
7. <hibernate-configuration>  
8.   
9. <session-factory>  
10. <property name="hbm2ddl.auto">update</property>  
11. <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
12. <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</propert
y>  
13. <property name="connection.username">system</property>  
14. <property name="connection.password">oracle</property>  
15. <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</pro
perty>  
16. <mapping resource="user.hbm.xml"/>  
17.       
18. </session-factory>  
19.   
20. </hibernate-configuration>  

struts.xml

This files contains information about the action class to be invoked. Here the action class
is User.

1. <?xml version="1.0" encoding="UTF-8" ?>  
2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation  
3. //DTD Struts Configuration 2.1//EN"   
4. "http://struts.apache.org/dtds/struts-2.1.dtd">  
5. <struts>  
6.   
7. <package name="abc" extends="struts-default">  
8. <action name="register" class="com.javatpoint.User">  
9. <result name="success">welcome.jsp</result>  
10. </action>  
11. </package>  
12. </struts>      

welcome.jsp

It is the welcome file, that displays the welcome message with username.

1. <%@ taglib uri="/struts-tags" prefix="S" %>  
2.   
3. Welcome: <S:property value="name"/>  

web.xml

It is web.xml file that contains the information about the controller. In case of Struts2,
StrutsPrepareAndExecuteFilter class works as the controller.

1. <?xml version="1.0" encoding="UTF-8"?>  
2. <web-app version="2.5"   
3.     xmlns="http://java.sun.com/xml/ns/javaee"   
4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
7.   <welcome-file-list>  
8.     <welcome-file>index.jsp</welcome-file>  
9.   </welcome-file-list>  
10.   <filter>  
11.     <filter-name>struts2</filter-name>  
12.     <filter-class>  
13.   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
14.     </filter-class>  
15.   </filter>  
16.   <filter-mapping>  
17.     <filter-name>struts2</filter-name>  
18.     <url-pattern>/*</url-pattern>  
19.   </filter-mapping>  
20. </web-app>  

Spring and Struts 2 Integration


Spring framework provides an easy way to manage the dependency. It can be easily
integrated with struts 2 framework.
The ContextLoaderListener class is used to communicate spring application with struts
2. It must be specified in the web.xml file.

You need to follow following steps:

1. Create struts2 application and add spring jar files.


2. In web.xml file, define ContextLoaderListener class.
3. In struts.xml file, define bean name for the action class.
4. In applicationContext.xml file, create the bean. Its class name should be action
class name e.g. com.javatpoint.Login and id should match with the action class of
struts.xml file (e.g. login).
5. In the action class, define extra property e.g. message.

Example of Spring and Struts 2 Integration


You need to create following files for simple spring and struts 2 application:

1. index.jsp
2. web.xml
3. struts.xml
4. applicationContext.xml
5. Login.java
6. welcome.jsp
7. error.jsp

1) index.jsp

This page gets the name from the user.

1. <%@ taglib uri="/struts-tags" prefix="s"%>  
2.   
3. <s:form action="login">  
4. <s:textfield name="userName" label="UserName"></s:textfield>  
5. <s:submit></s:submit>  
6. </s:form>  

2) web.xml

It defines controller class for struts 2 and ContextLoaderListener listener class to


make connection between struts2 and spring application.

1. <?xml version="1.0" encoding="UTF-8"?>  
2. <web-app version="2.5"   
3.     xmlns="http://java.sun.com/xml/ns/javaee"   
4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
7.   <welcome-file-list>  
8.     <welcome-file>index.jsp</welcome-file>  
9.   </welcome-file-list>  
10.   <filter>  
11.     <filter-name>struts2</filter-name>  
12.     <filter-class>  
13.         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
14.     </filter-class>  
15.   </filter>  
16.     
17. <listener>   
18. <listener-
class>org.springframework.web.context.ContextLoaderListener</listener-class>   
19. </listener>   
20.     
21.   <filter-mapping>  
22.     <filter-name>struts2</filter-name>  
23.     <url-pattern>/*</url-pattern>  
24.   </filter-mapping>  
25.     
26.   </web-app>  

3) struts.xml

It defines the package with action and result. Here, the action class name is login which
will be searched in the applicationContext.xml file.

1. <?xml version="1.0" encoding="UTF-8" ?>  
2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configura
tion 2.1//EN"   
3. "http://struts.apache.org/dtds/struts-2.1.dtd">  
4. <struts>  
5. <package name="abc" extends="struts-default">  
6. <action name="login" class="login">  
7. <result name="success">welcome.jsp</result>  
8. </action>  
9.   
10. </package>  
11.   
12. </struts>      
4) applicationContext.xml

It defines a bean with id login. This beans corresponds to the mypack.Login class. It will
be considered as the action class here.

It should be located inside the WEB-INF directory.

1. <?xml version="1.0" encoding="UTF-8"?>  
2. <beans  
3.     xmlns="http://www.springframework.org/schema/beans"  
4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
5.     xmlns:p="http://www.springframework.org/schema/p"  
6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
8.   
9. <bean id="login" class="mypack.Login">  
10. <property name="message" value="Welcome Spring"></property>  
11. </bean>  
12.   
13. </beans>  

5) Login.java

It defines two property userName and message with execute method where success is
returned.

1. package mypack;  
2. public class Login {  
3. private String userName,message;  
4.   
5. public String getMessage() {  
6.     return message;  
7. }  
8. public void setMessage(String message) {  
9.     this.message = message;  
10. }  
11. public String getUserName() {  
12.     return userName;  
13. }  
14. public void setUserName(String userName) {  
15.     this.userName = userName;  
16. }  
17. public String execute(){  
18.     return "success";  
19. }  
20. }  

6) welcome.jsp

It prints values of userName and message properties.

1. <%@ taglib uri="/struts-tags" prefix="s"%>  
2.   
3. Welcome, <s:property value="userName"/><br/>  
4. ${message}   

7) error.jsp

It is the error page. But it is not required in this example because we are not defining
any logic in the execute method of action class.

1. Sorry!   
download this example (developed using Myeclipse IDE)
Output

Struts with JDBC

You might also like