Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
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;"http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">index.jspstruts2 struts2/*org.apache.struts2.tiles.StrutsTilesListener3) 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 setterspublic 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.xmllogin-successlogin-error6) Create the tiles.xml file and define all the tiles definitions tiles.xml
  1. xmlversion="1.0" encoding="UTF-8" ?>
  2. "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
  3. "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
7) Create the LayoutManager pagelayoutmanager.jsp<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>><tiles:getAsString name="title" /><%@  include file="header.jsp" %><%@ include file="footer.jsp" %>8) Create View componentsheader.jspIt is header tile
footer.jsp
It is footer tilelogin-success.jsp<%@ taglib uri="/struts-tags" prefix="s" %>Welcome, 

login-error.jspSorry, username or password error!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"%>2) web.xml:index.jspstruts2class
>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterclass>class>org.springframework.web.context.ContextLoaderListenerclass>struts2/*3) struts.xml:<package name="abc" extends="struts-default">class="login">welcome.jsppackage>4) applicationContext.xml:class="mypack.Login">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, 
${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.