How Struts Works
How Struts Works
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.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.
In this example, we going to use struts 2 framework with hibernate. You need to have
jar files for struts 2 and hibernate.
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>
1. index.jsp
2. web.xml
3. struts.xml
4. applicationContext.xml
5. Login.java
6. welcome.jsp
7. error.jsp
1) index.jsp
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
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.
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
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