Struts 2 Tiles Plugin Tutorial with Example in Eclipse

Welcome to Part-4 of the 7-part series where we will go through different aspects for Struts2 Framework with some useful examples. In previous part we went through Struts2 Validation Framework. We saw how easy it is to integrate validation in your struts2 application.

In this part we will discuss about Tiles Framework and its Integration with Struts2. We will add Tiles support to our HelloWorld Struts application that we created in previous parts. I strongly recommend you to go through previous articles and download the source code of our sample application.

Struts 2 Tutorial List

Introduction to Tiles 2

Nowadays, website are generally divided into pieces of reusable template that are being rendered among different web pages. For example a site containing header, footer, menu etc. This items remains same through out the website and give it a common look and feel. It is very difficult to hard code this in each and every webpage and if later a change is needed than all the pages needs to be modified. Hence we use templatization mechanism. We create a common Header, Footer, Menu page and include this in each page.

Tiles Plugin allow both templating and componentization. In fact, both mechanisms are similar: you
define parts of page (a “Tile”) that you assemble to build another part or a full page. A part can
take parameters, allowing dynamic content, and can be seen as a method in JAVA language. Tiles is a templating system used to maintain a consistent look and feel across all the web pages of a web application. It increase the reusability of template and reduce code duplication.

A common layout of website is defined in a central configuration file and this layout can be extended across all the webpages of the web application.

Our Application Layout

Our goal is to add Header, Footer and Menu to our StrutsHelloWorld application. Following will be the layout of the same.
struts2-tiles-layout

Required JAR files

In order to add Tiles support to our Struts2 application, we will need few jar files. Following is the list of JARs in our example. Add these JARs in WEB-INF/lib folder.
struts2-tiles-jar-files

Configuring Tiles in web.xml

To configure Tiles, an entry for listener has to be made in web.xml. Open the web.xml from WEB-INF folder and add following code into it.

01.<listener>
02.<listener-class>
03.
04.org.apache.struts2.tiles.StrutsTilesListener
05.</listener-class>
06.</listener>
07.<context-param>
08.<param-name>tilesDefinitions</param-name>
09.<param-value>/WEB-INF/tiles.xml</param-value>
10.
11.</context-param>

The above code configure Tiles listener in web.xml. An input configuration file /WEB-INF/tiles.xml is passed as argument. This file contains the Tiles definition for our web application.

Create a file tiles.xml in WEB-INF folder and copy following code into it.
struts2-tiles-xml

01.<?xml version="1.0" encoding="UTF-8" ?>
02.
03.<!--DOCTYPE tiles-definitions PUBLIC-->
04."-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
06.<tiles-definitions>
07.<definition name="baseLayout" template="/BaseLayout.jsp">
08.<put-attribute name="title" value="" />
09.
10.<put-attribute name="header" value="/Header.jsp" />
11.<put-attribute name="menu" value="/Menu.jsp" />
12.
13.<put-attribute name="body" value="" />
14.<put-attribute name="footer" value="/Footer.jsp" />
15.
16.</definition>
17.<definition name="/welcome.tiles" extends="baseLayout">
18.<put-attribute name="title" value="Welcome" />
19.
20.<put-attribute name="body" value="/Welcome.jsp" />
21.</definition>
22.<definition name="/customer.tiles" extends="baseLayout">
23.
24.<put-attribute name="title" value="Customer Form" />
25.<put-attribute name="body" value="/Customer.jsp" />
26.
27.</definition>
28.<definition name="/customer.success.tiles" extends="baseLayout">
29.<put-attribute name="title" value="Customer Added" />
30.
31.<put-attribute name="body" value="/SuccessCustomer.jsp" />
32.</definition>
33.</tiles-definitions>

Here in tiles.xml we have define a template baseLayout. This layout contains attributes such as Header, Title, Body, Menu and Footer. The layout is then extended and new definitions for Welcome page and Customer page is defined. We have override the default layout and changed the content for Body and Title.

Creating JSPs

struts-2-tiles-layout-jspWe will define the template for our webapplication in a JSP file called BaseLayout.jsp. This template will contain different segments of web page (Header, Footer, Menu etc). Create 4 new JSP files BaseLayout.jsp, Header.jsp, Menu.jsp and Footer.jsp and copy following content in each of them.
BaseLayout.jsp

01.<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
02.<!--DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"-->
03.
05.<html>
06.<head>
07.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
08.<title><tiles:insertAttribute name="title" ignore="true" /></title>
09.
10.</head>
11.<body>
12.<table border="1" cellpadding="2" cellspacing="2" align="center">
13.<tr>
14.
15.<td height="30" colspan="2"><tiles:insertAttribute name="header" />
16.</td>
17.</tr>
18.
19.<tr>
20.<td height="250"><tiles:insertAttribute name="menu" /></td>
21.<td width="350"><tiles:insertAttribute name="body" /></td>
22.
23.</tr>
24.<tr>
25.<td height="30" colspan="2"><tiles:insertAttribute name="footer" />
26.
27.</td>
28.</tr>
29.</table>
30.</body>
31.</html>

Header.jsp

1.<%@ page contentType="text/html; charset=UTF-8"%>
2.
3.<%@ taglib prefix="s" uri="/struts-tags"%>
4.<h2>Struts2 Example - ViralPatel.net</h2>

Menu.jsp

1.<%@ page contentType="text/html; charset=UTF-8"%>
2.
3.<%@ taglib prefix="s" uri="/struts-tags"%>
4.<s:a href="customer-form">Customer</s:a>

Footer.jsp

1.<%@ page contentType="text/html; charset=UTF-8"%>
2.
3.<%@ taglib prefix="s" uri="/struts-tags"%>
4.Copyright &copy; ViralPatel.net

Modifications in Struts.xml

In struts.xml we defined result tag which maps a particular action with a JSP page. Now we will modify it and map the result with Tiles. Following will be the content of struts.xml file.

01.<?xml version="1.0" encoding="UTF-8" ?>
02.
03.<!--DOCTYPE struts PUBLIC-->
04."-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
06.
07.<struts>
08.<constant name="struts.enable.DynamicMethodInvocation"
09.value="false" />
10.
11.<constant name="struts.devMode" value="false" />
12.<constant name="struts.custom.i18n.resources"
13.value="ApplicationResources" />
14.
15.<package name="default" extends="struts-default" namespace="/">
16.<result-types>
17.<result-type name="tiles"
18.
19.class="org.apache.struts2.views.tiles.TilesResult" />
20.</result-types>
21.<action name="login"
22.class="net.viralpatel.struts2.LoginAction">
23.
24.<result name="success" type="tiles">/welcome.tiles</result>
25.<result name="error">Login.jsp</result>
26.</action>
27.
28.<action name="customer"
29.class="net.viralpatel.struts2.CustomerAction">
30.<result name="success" type="tiles">/customer.success.tiles</result>
31.
32.<result name="input" type="tiles">/customer.tiles</result>
33.</action>
34.<action name="customer-form">
35.<result name="success" type="tiles">/customer.tiles</result>
36.
37.</action>
38.</package>
39.</struts>

The struts.xml now defines a new Result type for Tiles. This result type is used in tag for different actions. Also note that we have define a new action customer-form. This is just an empty declaration to redirect user to Customer form page when she clicks Customer link from menu.

That’s All Folks

Compile and Execute the application in Eclipse and see that the header, menu and footer are properly applied.
Welcome Page with Tiles
struts3
Customer Page with Tiles
struts2
Customer Success Page with Tiles
struts1

Download Source Code

Click here to download Source Code without JAR files (11KB)

Struts2 + FreeMarker Tempalte (FTL) Integration example

English: Logo of Apache Struts

Today we will create a Struts2 based application that uses Freemarker FTL as view instead of JSP. This would give you a good insight in Struts2 + Freemarker integration. The application is similar to previous tutorial’s User app, where a list of users will be displayed and we can add new user.

The application is very simple:

  1. There is a table that displays user info like firstname, lastname.
  2. New user can be added via Add User form.

Below is the wireframe of our final Freemarker based Struts2 app.
freemarker-servlet-wireframe

So lets get started.

Things We Need

Before we starts with our Struts2 + FreeMarker example, we will need few tools.

  1. JDK 1.6 or above (download)
  2. Tomcat 6.x or above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (download)
  3. Eclipse 3.4.x or above (download)
  4. Struts 2.3.4.1 or above (download)
  5. Freemarker JAR v2.3.15 or above(download)

Let us start with our Struts2 based Freemarker application.

Step 1: Getting Started

Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.

dynamic web project in eclipse

After selecting Dynamic Web Project, press Next.

eclipse dynamic web project

Write the name of the project. For example Freemarker_Struts2_example. Once this is done, select the target runtime environment (e.g. Apache Tomcat v6.0). This is to run the project inside Eclipse environment. After this press Finish.

Once the project is created, you can see its structure in Project Explorer. This is how the project structure would look like when we finish the tutorial and add all source code.

struts2-ftl-eclipse-project-structure

Till this step, our basic Eclipse web project is ready. We will now add Struts2 and Freemarker support to this project.

Step 2: Adding Struts2 Support

First copy all required Struts2 JAR and supporting JAR files in WebContent > WEB-INF > lib folder. Create this folder if it does not exists. Don’t worry if you do not have these JARs. You can download the complete source code with JAR files at the end of this tutorial.

Next we change web.xml (deployment descriptor) and add Struts2 support to it. If you do not know why we do this, I strongly recommends you to go through Struts2 Tutorial series.

Related: Struts2 hello world example

Update the web.xml with following code:

File: /WebContent/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">
  <display-name>Freemarker Struts2 example - viralpatel.net</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

The above code in web.xml will define a filter StrutsPrepareAndExecuteFilter and maps it with url pattern /*. Thus each request will go through the Struts2 framework and it will decide wheather an action is available which can be called or default action should be allowed. Also struts framework will try to load its configuration from struts.xml file. It expects this file to be present in classpath (WEB-INF/classes) of the application when the source is compiled. Thus we will create a source folder called Resources and put the struts.xml file in it.

To create a source folder, right click on your project in Project Explorer view of Eclipse and select New > Source Folder.

struts2-resource-folder

Specify folder name Resources and press Finish.

Create a new file struts.xml under Resources folder. Copy following code into it.

File: /Resources/struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

 <struts>
    <constant name="struts.enable.DynamicMethodInvocation"
        value="false" />
    <constant name="struts.devMode" value="false" />

    <package name="default" extends="struts-default" namespace="/">
        <action name="list" method="list"
           >
            <result type="freemarker" name="success">/WEB-INF/ftl/index.ftl</result>
        </action>
        <action name="add" method="add"
           >
            <result type="freemarker" name="success">/WEB-INF/ftl/index.ftl</result>
        </action>
    </package>
</struts>

Note that in above configuration file, we have defined User action of our application. We defined two actions using <action> tag. One to list the users and another to add new user. Note how we used attribute method="add" and method="list" to let Struts2 know which particular method needs to be called within the Action class. Also the result type mapped here is type="freemarker". Struts2 provides a first-class support to Freemarker template. All one has to do is to define result type freemarker. Struts automatically manage the view forwards in this case. On success of the action we forward the request to /WEB-INF/ftl/index.ftl freemarker view.

Step 3: Create Struts2 Action

Create new Struts2 action class UserAction under /src/net/viralpatel/struts2/ folder and copy following source code into it.

File: /src/net/viralpatel/struts2/UserAction.java

package net.viralpatel.struts2;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private static final long serialVersionUID = -8366209797454396351L;

	private static List<User> userList = new ArrayList<User>();

	private User user;

	static {
		userList.add(new User("Bill", "Gates"));
		userList.add(new User("Steve", "Jobs"));
		userList.add(new User("Larry", "Page"));
		userList.add(new User("Sergey", "Brin"));
		userList.add(new User("Larry", "Ellison"));
	}

    /**
     * Action method to display user list. Uses <code>userList</code> array 
     * object defined as class level attribute to display list of users.
     * @return SUCCESS 
     */
    public String list() {
    	return SUCCESS;
    }

    /**
     * Action method to add new user. Read the user information
     * via <code>user</code> object defined as class level attribute.
     * @return SUCCESS if user is added successfully
     */
    public String add() {

    	System.out.println("User:"+user);

        userList.add(user);

    	return SUCCESS;
    }

    public List<User> getUserList() {
	return userList;
    }

    public void setUserList(List<User> userList) {
	UserAction.userList = userList;
    }

    public User getUser() {
	return user;
    }

    public void setUser(User user) {
	this.user = user;
    }
}

In above Struts2 action class, we defined two methods: add() and list() for our application. Also note that we have created a static ArrayList userList, which holds the user information. This is a dummy list just to mimic database values. In an ideal application, you may have to call Database and fetch list of users from a table. But for sake of simplicity we stores users in an arraylist.

Other than userList, we also defined User user object as class attribute. This object will store the user information when a new user is added. It act as a form bean object for action class.

Apart from the above UserController class, we will also need a bean class User which holds the user information like firstname, lastname etc.

File: /src/net/viralpatel/struts2/User.java

package net.viralpatel;

public class User {
	private String firstname;
	private String lastname;

	public User() {
	}

	public User(String firstname, String lastname) {
		this.firstname = firstname;
		this.lastname = lastname;

	}

	//Add Getter and Setter methods

}

Now add Freemarker view in your project.

Step 4: Create Freemarker Template File

Create a new file index.ftl under folder /WebContent/WEB-INF/ftl/. Copy following content into it.

File: /WebContent/WEB-INF/ftl/index.ftl

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 11 February 2007), see www.w3.org" />

  <title>ViralPatel.net - FreeMarker Spring MVC Hello World</title>
</head>

<body>
  <div id="header">
    <h2><a href="http://viralpatel.net"><img height="37" width="236" border="0px" src=
    "http://viralpatel.net/blogs/wp-content/themes/vp/images/logo.png" align=
    "left" /></a> FreeMarker Struts2 Hello World</h2>
  </div>

  <div id="content">
    <fieldset>
      <legend>Add User</legend>

	<@s.form action="add" method="post">
	    <@s.textfield label="First name" name="user.firstname"/>
	    <@s.textfield label="Last name" name="user.lastname"/>
	    <@s.submit value="Save"/>
	</@s.form>    </fieldset><br />    <table>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
      </tr>
	<#list userList as user>
  	<tr>
  		<td>${user.firstname}</td> <td>${user.lastname}</td>
  	</tr>
    </#list>	  
    </table>
  </div>
</body>
</html>

This is a simple FTL template file. We just iterate model userList list in a loop and prints user’s firstname and lastname in table.

One last thing, the default index.jsp is opened when you tries to execute your application in eclipse. So just update it and add link to our User page. Modify the /WebContent/index.jsp file and add following code into it.

File: /WebContent/index.jsp

<html>
<head>
    <title>Welcome</title>
</head>
<body>
    <a href="list">Freemarker Struts2 example</a>
</body>
</html>

That’s All Folks

You may want to run the application see the result. I assume you have already configured Tomcat in eclipse. All you need to do:
Open Server view from Windows > Show View > Server. Right click in this view and select New > Server and add your server details.

To run the project, right click on Project name from Project Explorer and select Run as > Run on Server (Shortcut: Alt+Shift+X, R)

URL: http://localhost:8080/Freemarker_Struts2_example/list
struts2-freemarker-ftl-example

Download Source Code

Freemarker_Struts2_example.zip (3.3 MB)

Reference : viralpatel.net

JSF 2.0 + Spring integration example

In this tutorial, we show you how to integrate JSF 2.0 with Spring.

Directory structure of this example

jsf2-spring-example-folder

1. Project Dependencies

Use Maven to grab all the JSF 2.0 and Spring library dependencies.

File : pom.xml


  4.0.0
  com.mkyong.common
  JavaServerFaces
  war
  1.0-SNAPSHOT
  JavaServerFaces Maven Webapp
  http://maven.apache.org

       java.net.m2
       java.net m2 repo
       http://download.java.net/maven/2

  	 

		org.springframework
		spring
		2.5.6

		org.springframework
		spring-web
		2.5.6

    <!-- For Java EE Application Server, uncomment this library 
  	and comment the rest of the libraries
    
      javax.faces
      jsf-api
      2.0
      provided
    
     -->

  	
  	

	  com.sun.faces
	  jsf-api
	  2.1.0-b03

	  com.sun.faces
	  jsf-impl
	  2.1.0-b03

	

	  org.glassfish.web
	  el-impl
	  2.2

        

	  javax.servlet
	  jstl
	  1.2

	  javax.servlet
	  servlet-api
	  2.5

	  javax.servlet.jsp
	  jsp-api
	  2.1

    JavaServerFaces

           org.apache.maven.plugins
           maven-compiler-plugin
           2.3.1

               1.6
               1.6

2. Spring Bean

A Spring bean declaration, later uses Spring to DI this “userBo” into JSF 2.0 managed bean property.

File : UserBo.java

package com.mkyong.user.bo;

public interface UserBo{

	public String getMessage();

}

File : UserBoImpl.java

package com.mkyong.user.bo.impl;

import com.mkyong.user.bo.UserBo;

public class UserBoImpl implements UserBo{

	String name;

	public void setName(String name) {
		this.name = name;
	}

	public String getMessage() {

		return "JSF 2 + Spring Integration saying : " + name;

	}

}

File : applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="userBo" class="com.mkyong.user.bo.impl.UserBoImpl">
		<property name="name" value="mkyong.com" />
	</bean>

</beans>

3. JSF 2.0

A simple JSF 2.0 web application.

File : UserBean.java

package com.mkyong;

import java.io.Serializable;

import com.mkyong.user.bo.UserBo;

//DI via JSF managed bean
public class UserBean implements Serializable{

	//DI via Spring
	UserBo userBo;

	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}

	public String printMsgFromSpring() {

		return userBo.getMessage();

	}

}

File : default.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      >

    <h:body>

    	<h1>JSF 2.0 + Spring Example</h1>

 		#{user.printMsgFromSpring()}

    </h:body>

</html>

JSF 2.0 + Spring Integration

The main concern is how to inject Spring “UserBo” bean into JSF managed bean “UserBean“.

File : UserBean.java

//DI via JSF managed bean
public class UserBean implements Serializable{

	//DI via Spring
	UserBo userBo;

1. Spring Listeners

Add following two Spring’s listeners into web.xml.

  <listener>
	<listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
  </listener>
  <listener>
	<listener-class>
		org.springframework.web.context.request.RequestContextListener
	</listener-class>
  </listener>

See full example of web.xml.

File : web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
	id="WebApp_ID" version="2.5">

  <display-name>JavaServerFaces</display-name>

  <!-- Add Support for Spring -->
  <listener>
	<listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
  </listener>
  <listener>
	<listener-class>
		org.springframework.web.context.request.RequestContextListener
	</listener-class>
  </listener>

  <!-- Change to "Production" when you are ready to deploy -->
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>

  <!-- Welcome page -->
  <welcome-file-list>
    <welcome-file>faces/default.xhtml</welcome-file>
  </welcome-file-list>

  <!-- JSF mapping -->
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Map these files with JSF -->
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>

</web-app>

2. SpringBeanFacesELResolver

Add SpringBeanFacesELResolver as el-resolver in faces-config.xml, and declare JSF managed bean like normal. Now, whenever JSF sees a bean name, it try JSF rules first, then Spring rules next. In this case, when JSF see #{userBo}, it will match it with Spring’s “userBo” bean.

File : faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    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-facesconfig_2_0.xsd"
    version="2.0">

	<application>
    	        <el-resolver>
    		    org.springframework.web.jsf.el.SpringBeanFacesELResolver
    	        </el-resolver>
  	</application>

	<managed-bean>
		<managed-bean-name>user</managed-bean-name>
		<managed-bean-class>com.mkyong.UserBean</managed-bean-class>
		<managed-bean-scope>session</managed-bean-scope>
		<managed-property>
			<property-name>userBo</property-name>
			<value>#{userBo}</value>
		</managed-property>
	</managed-bean>

</faces-config>
Note
Alternatively, you can add DelegatingVariableResolver in variable-resolver, but API variable-resolver is deprecated after JSF 1.1. You should use el-resolverinstead.

<application>
	<variable-resolver>
		org.springframework.web.jsf.DelegatingVariableResolver
	</variable-resolver>
</application>

3. Demo

Done, run it and see output below : http://localhost:8080/JavaServerFaces/faces/

jsf2-spring-example-output

Download Source Code

How do I pass JavaScript values to Scriptlet in JSP?

I can provide two ways,

a.jsp:

<html>
<script language="javascript" type="text/javascript">
function call(){
var name = "xyz";
window.location.replace("a.jsp?name="+name);
}
</script>
<input type="button" value="Get" onclick='call()'>
<%
String name=request.getParameter("name");
if(name!=null){
out.println(name);
}
%>
</html>

2)b.jsp:

<script>
var v="xyz";
</script>
<% String st="<script>document.writeln(v)</script>";
out.println("value="+st); %>