OfferTransform Your Career with Expert-Led IT Training. Flat discounts active!Explore Now
OnlineITGuru Logo
WEEKEND SPECIAL - UPTO 60% OFF
AI & Machine Learning

Struts Zero Configuration

Last updated on Jun 15, 2020

Copy Link:
Struts Zero Configuration
Struts Zero Configuration: We can make struts 2 application without the arrangement record struts.xml. There are two approaches to make Struts zero Configuration.
  • By convention
  • By annotation
It means to make Struts Zero Configuration arrangement record utilizing tradition is as per the following
  1. Create input page (optional)
  2. Create the action class
  3. Create view components
Annotations

Struts 2 give you advantageous approach to make struts application utilizing comments. Thus, there is no need struts.xml document. As we have said before, there are 2 approaches to utilize zero arrangement document (no struts.xml record).

  1. By convention
  2. By annotation
 Annotations utilized as a part of struts 2 application
  1. @Action annotation is utilized to stamp the activity class.
  2. @Results annotation is utilized to characterize numerous outcomes for one activity.
  3. @Result annotation is utilized to show single outcome.
Tiles Framework Integration:

We can customize the design of the struts 2 application by incorporating with tiles structure. A site page can contain many parts (known as tile, for example, header, left sheet, right sheet, body part, footer and so on. In tiles system, we deal with all the tile by our Layout Manager page.

Advantage of tiles
  •       Customization
  •       Code reusability
  •       Easy to modify
  •       Easy to remove
Layout of webpage:

Layout of WebpageSteps to create tiles application

  1. Add tiles library in your application
  2. Define Struts2TilesListener in web.xml file
  3. Create the input page (index.jsp)
  4. Create the Action class
  5. Extend the tiles-default package in your package and define all the result type as tiles in struts.xml file
  6. Create the tiles.xml file and define all the tiles definitions
  7. Create the LayoutManager page
  8. Create the View components
1) Add tiles library in your application
By using myeclipse IDE, we can add tiles by right click on the project -> Build Path -> Add Library -> Add Myeclipse Library -> Select tiles -> then  ok.
2) Define Struts 2 Tiles Listener in web.xml file.
 web.xml; <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>   </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> </listener> </web-app> 3) Create the input page (index.jsp)  index.jsp: <%@ taglib uri="/struts-tags" prefix="s" %> <s:form action="login"> <s:textfield name="name" label="Name"></s:textfield> <s:password name="password" label="Password"></s:password> <s:submit value="login"></s:submit> </s:form> 4) Create the action class
Login.java:
package com.javaspot; public class Login { private String name,password; //getters and setters public String execute(){ if(password.equals("admin")){ return "success"; } else{ return "error"; } } } 5) Inherit the tiles-default package and define all the result type as tiles in struts.xml  struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="abc" extends="tiles-default" > <action name="login" class="com.javaspot.Login"> <result name="success" type="tiles">login-success</result> <result name="error" type="tiles">login-error</result> </action> </package> </struts> 6) Create the tiles.xml file and define all the tiles definitions  tiles.xml
  1. <?xmlversion="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE tiles-definitions PUBLIC
  3. "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
  4. "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions> <definition name="login-success" template="/layoutmanager.jsp"> <put-attribute name="title" value="Welcome Page"/> <put-attribute name="body" value="/login-success.jsp"/> </definition> <definition name="login-error" template="/layoutmanager.jsp"> <put-attribute name="title" value="Login Error"/> <put-attribute name="body" value="/login-error.jsp"/> </definition> </tiles-definitions> 7) Create the LayoutManager page layoutmanager.jsp <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title><tiles:getAsString name="title" /></title> </head> <body> <%@  include file="header.jsp" %> <tiles:insertAttribute name="body" /> <%@ include file="footer.jsp" %> </body> </html> 8) Create View components header.jsp <h2 style="background-color: pink;text-align:center;"> It is header tile</h2> <hr/> footer.jsp <hr> <h2 style="background-color: pink;text-align:center;"> It is footer tile</h2> login-success.jsp <%@ taglib uri="/struts-tags" prefix="s" %> Welcome, <s:property value="name"/> </textrea></div> <hr/> <strong>login-error.jsp</strong> <div class="codeblock"><textarea name="code" class="xml" > Sorry, username or password error! <jsp:include page="index.jsp"></jsp:include> Hibernate and Struts:

We can integrate any struts application with rest. There is no necessity of additional endeavors. In this illustration, we going to utilize struts 2 structure with sleep. You need bump records for struts 2 and sleep.

Spring and Struts 2 Integration

Spring framework gives a simple approach to deal with the dependency. It can be effectively incorporated with struts 2 framework.

The ContextLoaderListener class is utilized to impart spring application with struts 2. It must be indicated in the web.xml document.

Following Steps need to follow:
  • Make struts2 application and include spring jar records.
  • In web.xml record, characterize ContextLoaderListener class.
  • In struts.xml record, characterize bean name for the activity class.
  • In applicationContext.xml record, make the bean. Its class name ought to be activity class name e.g. com.javaspot.Login and id should coordinate with the activity class of struts.xml record (e.g. login).
  • In the activity class, characterize additional property e.g. message.

Example of Spring and Struts 2 Integration

  1. index.jsp
  2. web.xml
  3. struts.xml
  4. applicationContext.xml
  5. Login.java
  6. welcome.jsp
  7. error.jsp
1) index.jsp: <%@ taglib uri="/struts-tags" prefix="s"%> <s:form action="login"> <s:textfield name="userName" label="UserName"></s:textfield> <s:submit></s:submit> </s:form> 2) web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 3) struts.xml: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="abc" extends="struts-default"> <action name="login" class="login"> <result name="success">welcome.jsp</result> </action> </package> </struts> 4) applicationContext.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="login" class="mypack.Login"> <property name="message" value="Welcome Spring"></property> </bean> </beans> 5) Login.java: package mypack; public class Login { private String userName,message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String execute(){ return "success"; } } 6) welcome.jsp: <%@ taglib uri="/struts-tags" prefix="s"%> Welcome, <s:property value="userName"/><br/> ${message} 7) error.jsp:

It is the error page. Be that as it may, it is not required in this case since we are not characterizing any logic in the execute technique for activity class.

Why Choose Us

Master Your Future with OnlineITGuru

We don't just provide courses; we build careers. From expert-led live training to dedicated placement support, discover why thousands of professionals trust us for their digital transformation journey.

200+

Partner Companies

$120K

Highest Package

75%

Average Hike

98%

Placement Rate

Reliable Career Partners

Google
Microsoft
Amazon
Meta
Netflix
Apple