Nearby lessons

30 of 35

Spring - Web MVC with Tiles

📌 What You Will Learn
  • Understand Spring - Web MVC with Tiles
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Spring - Web MVC with Tiles step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

Spring WEB MVC with Tiles Integration

In general, in web applications, we have to organize the web pages in a standard mode inorder to improve Look and Feel.

To manage all the web pages in standard manner we have to use templates.

In general, we will use the following templates to prepare web pages in web applications. To manage all the web pages in the above provided templates and to manage flow of execution between all the web pages we have to use a Apache provided Tiles framework.

Apache Software Foundations has provided the complete Tiles Framework in the form of the following JAR files.

 tiles-api-2.2.2.jar  tiles-core-2.2.2.jar  tiles-jsp-2.2.2.jar  tiles-servlet-2.2.2.jar  tiles-template-2.2.2.jar

If we want to use Tiles Framework in our web applications then we have to provide the above Jar files in web application lib folder.

The web frameworks like Struts, JSF, Xwork2 .... are using already Tiles Framework to prepare web applications.

Spring web MVC framework is also providing in built support for Tiles Framework Integration inorder to prepare web applications.

If we want to use Tiles Framework in Spring web MVC applications then we have to use the following steps.

  • Prepare Template page by using Tiles tags.
  • Prepare Tiles pages.
  • Prepare Tiles definitions.
  • Prepare Controller class.

Prepare Template page by using Tiles tags

The main intention of Template page is to define a standard template for web pages.

To prepare Template pages we have to use the following Tiles tag library which is available with the URI "http://tiles.apache.org/tags-tiles".

<tiles:insertAttribute name="--"/>

This tag can be used to define tiles logical names in Template. Where "name" attribute will take logical name of the tile in Template.

Example02
Loading editor…

Prepare Tiles pages — header.jsp

In Tiles based web applications, Tile is a JSP page it able to represent Header, Footer, Menu, Body,.....

EX:

Example03
Loading editor…

Prepare Tiles pages — menu.jsp

Example04
Loading editor…

Prepare Tiles definitions — tiles-defs.xml

The main intention of Tiles Definitions is to define the cobination of tiles pages in the form of Definitions.

In Tiles based web applications, we have to define all tiles definitions in an xml file by using the following xml tags.

<!DOCTYPE ----- > <tiles-definitions>
<definition name="--" template="--"> <put-attribute name="--" value="--"/> ------
</definition> ------ </tiles-definitions>

 Where <tiles-definitions> is root tags, it will include no of definitions.  Where <definition> tag is able to provide single definition which includes tiles pages.  Where "name" attribute in <definition> tag will take definition name.  Where "template" attribute in <definition> tag will take the name and location of the layout

page  Where <put-attribute> tag will assign tile JSP page to the respective logical name of the tile

in JSP page.  Where "name" in <put-attribute> will take logical name of the tile.  Where "value" attribute will take the name and location of the tile JSP page.

In Tiles definitions file, we are able to extend one definition to another definition by using "extends" attribute inorder to reuse tiles configurations and we are able to override one tile configuration to another tiles configuration just like normal inheritance and method overridding. Note: We are able to get DOCTYPE definition in tiles definitions file by using the following dtd file from tiles-core-2.2.2.jar.

org\apache\tiles\resources\tiles-config_2_1.dtd

EX:

Example05
Loading editor…

Prepare Tiles definitions — StudentController.java

  • Prepare Controller class.

In Tiles based applications, we will prepare Controller classes as like normal Controller classes , but, the respective Business methods must return tiles definitions logical name only instead of jsp pages names.

EX:

Example06
Loading editor…

Prepare Spring Configuration File — ds-servlet.xml

In Spring configuration file we have to provide the following configuration details.

  • UrlBasedViewResolver with TilesView class.
  • TilesConfigurer with "definitions" property of list type with tiles definitions xml file.

EX:

Example07
Loading editor…

Prepare Spring Configuration File — index.jsp

Example:

Example08
Loading editor…

Prepare Spring Configuration File — layout.jsp

Example09
Loading editor…

Prepare Spring Configuration File — header.jsp

Example10
Loading editor…

Prepare Spring Configuration File — menu.jsp

Example11
Loading editor…

Prepare Spring Configuration File — welcome.jsp

Example12
Loading editor…

Prepare Spring Configuration File — footer.jsp

Example13
Loading editor…

Prepare Spring Configuration File — addstudent.jsp

Example14
Loading editor…

Prepare Spring Configuration File — searchstudent.jsp

Example15
Loading editor…

Prepare Spring Configuration File — deletestudent.jsp

Example16
Loading editor…

Prepare Spring Configuration File — studentdetails.jsp

Example17
Loading editor…

Prepare Spring Configuration File — status.jsp

Example18
Loading editor…

Prepare Spring Configuration File — StudentController.java

Example19
Loading editor…

Prepare Spring Configuration File — StudentService.java

Example20
Loading editor…

Prepare Spring Configuration File — StudentServiceImpl.java

Example21
Loading editor…

Prepare Spring Configuration File — StudentDao.java

Example22
Loading editor…

Prepare Spring Configuration File — StudentDaoImpl.java

Example23
Loading editor…

Prepare Spring Configuration File — Student.java

Example24
Loading editor…

Prepare Spring Configuration File — StudentEntity.java

Example25
Loading editor…

Prepare Spring Configuration File — tiles-defs.xml

Example26
Loading editor…

Prepare Spring Configuration File — ds-servlet.xml

Example27
Loading editor…

Prepare Spring Configuration File — web.xml

Example28
Loading editor…

Prepare Spring Configuration File

To run this application we have to use the following JARs in web application lib folder.

1) Spring MVC + Spring ORM + Spring Tx + Spriong JDBC + Spring AOP 2) Tiles Jars and its supportig jars 3) jstl jars If we use JstlView class. 4) Hibernate Jars 5) ojdbc6.jar 6) Commons bean utils+commons digester + log4j + slf4j-log4j12 + slf4j

Spring JARS

spring-aop-4.3.9.RELEASE.jar spring-aspects-4.3.9.RELEASE.jar spring-beans-4.3.9.RELEASE.jar spring-context-4.3.9.RELEASE.jar spring-context-support-4.3.9.RELEASE.jar spring-core-4.3.9.RELEASE.jar spring-expression-4.3.9.RELEASE.jar spring-jdbc-4.3.9.RELEASE.jar spring-orm-4.3.9.RELEASE.jar spring-tx-4.3.9.RELEASE.jar spring-web-4.3.9.RELEASE.jar spring-webmvc-4.3.9.RELEASE.jar

Tiles and dependent JARS

commons-beanutils-1.8.3.jar commons-digester-2.1.jar log4j.jar slf4j-log4j12.jar slf4j.jar tiles-api-2.2.2.jar tiles-core-2.2.2.jar tiles-jsp-2.2.2.jar tiles-servlet-2.2.2.jar tiles-template-2.2.2.jar

Jstl Jars taglibs-standard-impl-1.2.5.jar taglibs-standard-spec-1.2.5.jar

Hibernate Jars

antlr-2.7.7.jar commons-logging-1.2.jar dom4j-1.6.1.jar hibernate-commons-annotations-4.0.5.Final.jar hibernate-core-4.3.11.Final.jar hibernate-entitymanager-4.3.11.Final.jar hibernate-jpa-2.1-api-1.0.0.Final.jar jandex-1.1.0.Final.jar javassist-3.18.1-GA.jar jboss-logging-3.1.3.GA.jar jboss-logging-annotations-1.2.0.Beta1.jar jboss-transaction-api_1.2_spec-1.0.0.Final.jar ojdbc6.jar

ALL Jars Together antlr-2.7.7.jar commons-beanutils-1.8.3.jar commons-digester-2.1.jar commons-logging-1.2.jar dom4j-1.6.1.jar hibernate-commons-annotations-4.0.5.Final.jar hibernate-core-4.3.11.Final.jar hibernate-entitymanager-4.3.11.Final.jar hibernate-jpa-2.1-api-1.0.0.Final.jar jandex-1.1.0.Final.jar javassist-3.18.1-GA.jar jboss-logging-3.1.3.GA.jar jboss-logging-annotations-1.2.0.Beta1.jar jboss-transaction-api_1.2_spec-1.0.0.Final.jar log4j.jar ojdbc6.jar slf4j-log4j12.jar slf4j.jar spring-aop-4.3.9.RELEASE.jar spring-aspects-4.3.9.RELEASE.jar spring-beans-4.3.9.RELEASE.jar spring-context-4.3.9.RELEASE.jar spring-context-support-4.3.9.RELEASE.jar spring-core-4.3.9.RELEASE.jar spring-expression-4.3.9.RELEASE.jar spring-jdbc-4.3.9.RELEASE.jar spring-orm-4.3.9.RELEASE.jar spring-tx-4.3.9.RELEASE.jar spring-web-4.3.9.RELEASE.jar spring-webmvc-4.3.9.RELEASE.jar taglibs-standard-impl-1.2.5.jar taglibs-standard-spec-1.2.5.jar tiles-api-2.2.2.jar tiles-core-2.2.2.jar tiles-jsp-2.2.2.jar tiles-servlet-2.2.2.jar tiles-template-2.2.2.jar

Struts Overview

 Framework is a semi implemented application, it will be used to design applications in simplified manner.

 Framework is prefabricated software units that programmer can share, customize, reuse in order to simplify application development.

 Framework is the collection of predefined classes and interfaces to simplify application development.

In general, in Applications development, Frameworks will provide the following advantages.

  • Frameworks will provide common implementation in all the projects as predefined implementation

EX: Controller Servlets and the services like I18N, Exception Handling, Validations ,.....

  • Frameworks will provide standard template to design applications.
  • Frameworks will provide standard flow of execution between the components.
  • Frameworks will reduce application development time.
  • Frameworks will reduce application development cost.
  • Frameworks will improve productivity.
  • Frameworks will provide modularity in application development.

There are two types of Frameworks.

  • Web Frameworks
  • Application Frameworks

What is the difference between web frameworks and Application Frameworks?

Ans

Web Frameworks will provide very good environment to prepare and execute web applications only. EX: Struts, JSF.

Application Frameworks will provide very good environment to prepare and execute any type of JAVA/J2EE Applications including Standalone Applications, Web applications, Distributed Applications, EX: Spring

Struts is MVC based web framework provided by Apache Software Foundations.

Struts is existed in two versions.

  • Struts1.x
  • Struts2.x

If we want to prepare Struts applications in Struts1.x version then we have to use the following components

  • View
  • Deployment Descriptor
  • Controller [ActionServlet]
  • Action class
  • Action Form
  • Struts Configuration File

View

In Struts applications, View part is representing presentation part. In web applications, there are two types of presentation part.

  • Informational Presentation part
  • Form Based Presentation part

Informational Presentation part

This type view part is able to provide only information to the user, which includes status of the server side actions like Success, failure, ... and display a particular database table data,.....

EXAMPLE: success.html

<h1> Login Success </h1>

Form Based Presentation part

This type of view part is able to provide a form to collect data from users and to submit data to the server side applications.

In Struts applications, to prepare Presentation part we are able to use plain HTML tags, but, which are not suggestible. In Struts applications it is suggestible to use Struts provided tag library.

EX: loginform.html

  • <html>
  • <body>
  • <form method="POSt" action="login.*">
  • <table>
  • <tr>
  • <td>User Name</td>
  • <td><input type="text" name="uname"/></td>
  • </tr>
  • <tr>
  • <td>Password</td>
  • <td><input type="password" name="upwd"/></td>
  • </tr>
  • <tr>
  • <td><ipnut type="submit" value="Login"/></td>
  • </tr>
  • </table></form></body></html>

The above login form with Struts provided html tag library EX: loginform.jsp

  • <%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
  • <html:html>
  • <body>
  • <html:form method="POST" action="login.do">
  • <table>
  • <tr>
  • <td>User Name</td>
  • <td><html:text property="uname"/></td>
  • </tr>
  • <tr>
  • <td>Password</td>
  • <td><html:password property="upwd"/></td>
  • </tr>
  • <tr>
  • <td><html:submit>Login</html:submit></td>
  • </tr>
  • </table></html:form></body></html>

Deployment Descriptor

Deployment descriptor is web.xml file, it will provide description about our web application which is required by the container in order to perform server side actions. In general, in web applications, web.xml file will provide the following configuration details.

  • Display names configurations.
  • Welcome Files Configurations.
  • Servlets configurations
  • Filters Configurations
  • Listeners Configurations.
  • Session time out configurations
  • Error Pages Configurations
  • Taglib configurations

In Struts based web applications, the main intention of web.xml file is to configure controller Servlet that is ActionServlet.

Example38
Loading editor…

Controller [ActionServlet]

In Struts Framework, ActionServlet is predefined Controller, it was provided in the form of org.apache.struts.action.ActionServlet .

In Struts applications, ActionServlet will perform the following actions.

  • ActionServlet will take request from Client.
  • ActionServlet will identify the names and locations of the ActionForm and Action class

through Struts configuration file.

  • ActionServlet will load , instantiate ActionForm class.
  • ActionServlet will store form data in ActionForm object.
  • ActionServlet will load and instantiation Action class.
  • ActionServlet will execute execute(--) method in Action class.
  • ActionServlet will identify view page through Struts configuration file.
  • ActionServlet will forward request to view page inorder to generate response.

Note: In Struts applications, ActionServlet is performing all the above actions by using "RequestProcessor" internally.

Action Class or Controller Component — LoginActionForm.java

In Struts based web applications, the main intention of ActionForm or FormBean component is to manage a particular form data at Server side inorder to perform Server side data validations, to transfer data from Controller layer to model layer or Vie layer,.....

To prepare Form Bean components in Struts applications we have to use the following rules and regulations.

  • Declare an user defined class, it must be extended from org.apache.struts.action.ActionForm .
  • Form Bean class must be public, it must not be abstract and final.
  • In Form Bean class, we must declare properties with the same names of the form

properties.

  • In Form Bean class we must declare all properties as private and all bahaviours as public.
  • In FormBean class , we must provide a seperate set of setXXX() and getXXX() methods

for each and every property.

  • In FormBean class, if we want to provide any constructor then we can provide constructor

but that constructor must be public and 0-arg constructor.

  • In FormBean class, we can override equals(-) method and hashCode() method as per

the requirement.

EXAMPLE:

Example40
Loading editor…

Action Class or Controller Component

In Struts based web applications, the main intention of Action class is to manage application logic which we want to execute by getting request from client.

In Struts based web applications, to prepare Action class we have to use the following steps.

  • Declare an user defined class and it must be extended from org.apache.struts.action.Action class.
  • Action class must be public class and it must not be abstract class.
  • In Action class we must override either of the following methods.

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest reques, HttpServletResponse response)throws Exception

public ActionForward execute(ActionMapping mappinig, ActionForm form, ServletRequest request, ServletResponse response)throws Exception.

Note:In execute() method we must return ActionForward object with a particular Forward key inorder to identify target view page, for this, we have to use the following method from ActionMapping.

public ActionForward findForward(String key)

EX: LoginAction.java

  • public class LoginAction extends org.apache.struts.action.Action{
  • public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequ

est request, HttpServletResponse response)throws Exception{

  • ----Appl logic-----
  • String status = "success"/"failure";
  • return mapping.findForward(status);

struts-config.xml

 In Struts based web applications, the main intention of struts configuration file is to provide mappings between user forms amd the respective ActionForm classes and Action classes ,...... which are required by the ActionServlet inorder to perform Server side actions.

 In Struts based web applications, the default name and location of configuration file is "struts-config.xml" and "WEB-INF" location, but, it is possible to change this default name and location but we must give that new name and location to the Struts Framework.

 In Struts based web applications, configuration file is able to provide the following configurations.

  • Datasource configurations.
  • Global Forwards configurations.
  • Form Beans Configurations.
  • Action classes configurations.
  • Message Resources configurations.
  • Controller configurations.
  • Plugin configurations
  • Global Exceptions configurations.

To prepare basic Struts based web applications we need to provide ActionForm class configuration and action class configuration in struts connfiguration file.

  • <!DOCTYPE ---- >
  • <struts-config>
  • <form-beans>
  • <form-bean name="--" type="--"/>
  • </form-beans>
  • <action-mappings>
  • <action path="/---" name="--" type="--">
  • <forward name="--" path="/---"/>
  • </action>
  • </action-mappings>
  • </struts-config>

 Where <struts-config> is a root tag, it will include struts application configuration details.  Where <form-beans> tag is able to include no of form beans configurations.  Where <form-bean> tag is able to provide single form bean class configuration.  Where "name" attribute in <form-bean> tag is able to provide logical name to Form Bean

component.  Where "type" attribute in <form-bean> tag will take fully qualified name of the respective

form bean class.  Where <action-mappings> tag is able to include no of actions configurations.  Where <action> tag is able to provide mapping between user form, ActionForm class and

the respective Action class.  Where "path" attribute in <action> tag is able to take url pattern which we specified in User

form.  Where "name" attribute in <action> tag will take logical name of the form bean class which

we specified in struts configuration file under form beans configurations.  Where "type" attribute in <action> tag is able to take fully qualified name of the Action

class.  Where <forward> tag is able to provide mapping between forward key which is returned

from execute() method in Action class and the target view page.  Where "name" attribute in <forward> tag is able to take forward key.  Where "path" attribute in <forward> tag is able to take the name and location of target view

page.

Example42
Loading editor…

Steps — loginform.html

  • Download Struts JARs from internet.
  • Create Dynamic Project in Eclipse.
  • Copy all Struts related JARs in lib folder in dynamic web project.
  • Configure ActionServlet in web.xml file.
  • Prepare User forms
  • Prepare Form Bean class.
  • Create Action class.
  • Create Struts configuration file.
  • Run dynamic web application.

Example (struts Application)

Example43
Loading editor…

Steps — success.html

Example44
Loading editor…

Steps — Failure.html

Example45
Loading editor…

Steps — LoginActionForm.java

Example46
Loading editor…

Steps — LoginAction.java

Example47
Loading editor…

Steps — struts-config.xml

Example48
Loading editor…

Steps — web.xml

Example49
Loading editor…

Steps

If we want to integrate Struts application with Spring Framework then we have to use the following steps.

  • Prepare User Forms and View part.
  • Prepare ActionForm class.
  • Prepare Action class.
  • Prepare Business class.
  • Prepare Struts configuration File.
  • Prepare Spring Configuration File.
  • Prepare web.xml file.

Prepare User Forms and View part

In Struts and Spring Integration applications, we have to prepare presentation part as per Struts rules and regulations only. To prepare User Interface in Struts applications we will use either normal html tags or we will use Struts provided tag library.

EX:

<form method="POST" action="login.do"> User Name<input type="text" name="uname"/><br> Password<input type="password" name="upwd"/><br> <input type="submit" value="Login"/> </form>

success.jsp

<h1> User Login Success</h1>

failure.jsp

<h1> User Login Failure </h1>
  • Prepare ActionForm class.

In Struts based web applications, we have to prepare ActionForm class inorder to manage user form data at Server side .

In Struts applications, to prepare ActionForm class we will use the following steps.

  • Declare an USer defined class.
  • Extend org.apache.struts.action.ActionForm class to user defined class.
  • Declare properties as per user form in ActionForm class and provide setXXX() and getXXX() methods for each and every property.

EX:

public class LoginActionForm extends ActionForm{ private String uname; private String upwd; setXXX() and getXXX() }

  • Prepare Action class.

In Struts based web applications, the main intention of Action class is to include business logic or to provide Business Components provided business method calls. In Struts with Spring integration applications we will provide application business logic in Spring Bean components and it must be accessed from Struts Action classes.

To prepare Action class in Struts with Spring integration applications we have to use the following steps.

a) Declare an user defined class. b) Extend org.springframework.web.struts.ActionSupport class to user defined class. c) Provide application logic by overriding execute() method. d) In execute() method get ApplicationContext object by accessing

getWebApplicationContext() method and get Spring Bean objcts by using getBean() method. e) Access Business methods which we provided in Spring bean objects.

Note: The main intention of org.springframework.web.struts.ActionSupport class is to provide getWebApplicationContext() method inorder to get ApplicationContext object inorder to get Spring provided Bean objects.

public ApplicationContext getWebApplicationcontext()

EX:

public class LoginAction extends ActionSupport { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {

LoginActionForm laf = (LoginActionForm)form; String uname = laf.getUname(); String upwd = laf.getUpwd(); ApplicationContext context = getWebApplicatinContext(); UserService us =(UserService)context.getBean(―userService‖); String status = us.checkLogin(uname, upwd); return mapping.findForward(status); } }

Prepare Business class

In Struts with Spring Integration application, we will provide Business component as per Spring rules and regulation.

EX:

public class UserService{ String status = ""; public String checkLogin(String uname, String upwd){ if(uname.equals("durga") && upwd.equals("durga")){ status = "success"; }else{ status = "failure"; }

Prepare Struts configuration File — loginform.jsp

In Struts with Spring Integration applications, we have to provide provide all Struts configurations like FormBeans, Action classes,... and we must provide plug-in coinfiguration with the "org.springframework.web.struts.ContextLoaderPlugIn" with the property "contextConfigLocation" inorder to provide name and location of the Spring configuration.

<struts-config>
<form-beans>
<form-bean name="loginActionForm"

type="com.durgasoft.formbeans.LoginActionForm"/>

</form-beans>
<action-mappings>
<action path="/login" name="loginActionForm"

type="com.durgasoft.action.LoginAction">

<forward name="success" path="/success.jsp"/>
<forward name="failure" path="/failure.jsp"/>
</action>
</action-mappings>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-

INF/applicationContext.xml" />

</plug-in>
</struts-config>
  • Prepare Spring Configuration File.

In Struts with Spring Integration application we will prepare Spring configuration file with all the beans configuration.

EX:

<beans> <bean id="userService" class="com.durgasoft.service.UserService"/>
</beans>
  • Prepare web.xml file.

In Struts with Spriong integration application , we will provide ActionServlet Configuration in web.xml file as per Struts rules and regulations.

EX:

<web-app> <display-name>struts_spring_app</display-name> <welcome-file-list> <welcome-file>loginform.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>actionServlet</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>actionServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
</web-app>

Example:

Example55
Loading editor…

Prepare Struts configuration File — success.jsp

Example56
Loading editor…

Prepare Struts configuration File — failure.jsp

Example57
Loading editor…

Prepare Struts configuration File — LoginActionForm.java

Example58
Loading editor…

Prepare Struts configuration File — LoginAction.java

Example59
Loading editor…

Prepare Struts configuration File — UserService.java

Example60
Loading editor…

Prepare Struts configuration File — struts-config.xml

Example61
Loading editor…

Prepare Struts configuration File — applicationContext.xml

Example62
Loading editor…

Prepare Struts configuration File — web.xml

Example63
Loading editor…

Prepare Struts configuration File

To run this application we have to use the following JARs. o Struts1.3.10 Jars + Spring2.5 jars

antlr-2.7.2.jar bsf-2.3.0.jar commons-beanutils-1.8.0.jar commons-chain-1.2.jar commons-digester-1.8.jar commons-fileupload-1.1.1.jar commons-io-1.1.jar commons-logging-1.0.4.jar commons-validator-1.3.1.jar jstl-1.0.2.jar oro-2.0.8.jar spring-aop.jar

📝 Key Takeaways
  • Key ideas of Spring - Web MVC with Tiles explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.