Struts Zero Configuration
Last updated on Jun 15, 2020
- By convention
- By annotation
- Create input page (optional)
- Create the action class
- Create view components
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).
- By convention
- By annotation
- @Action annotation is utilized to stamp the activity class.
- @Results annotation is utilized to characterize numerous outcomes for one activity.
- @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
Steps to create tiles application
- Add tiles library in your application
- Define Struts2TilesListener in web.xml file
- Create the input page (index.jsp)
- Create the Action class
- Extend the tiles-default package in your package and define all the result type as tiles in struts.xml file
- Create the tiles.xml file and define all the tiles definitions
- Create the LayoutManager page
- 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 classLogin.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- <?xmlversion="1.0" encoding="UTF-8" ?>
- <!DOCTYPE tiles-definitions PUBLIC
- "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
- "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
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 IntegrationSpring 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
- index.jsp
- web.xml
- struts.xml
- applicationContext.xml
- Login.java
- welcome.jsp
- 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.
