Home >> Struts
Integrating JSF with Struts2 example code:
In this example I shall try to demonstrate a simple way
and may be one out of many ways of integrating JSF with
Struts2, using Plugin available with Struts2 Framework.
In this example, I shall be working on ways to achive
page and action navigation flow using Struts2 and
displaying target JSF page using JSF UI component.
So the index JSP page will have form and action Tags from
Struts2 Tag Library, and the next preview page will be
using JSF Tags from TagLibrary.
index.jsp
<%@ taglib uri="/struts-tags" prefix="struts2"%>
<html>
<head>
</head>
<body>
<struts2:form method="post" action="jsf-view" namespace="/sample-jsf">
<table>
<tr><td><struts2:textfield key="userName" label="Name :"/></td></tr>
<tr><td><struts2:textfield key="roll" label="Roll :"/></td></tr>
<tr><td colspan="2"><struts2:submit value="Register"/></td></tr>
</table>
</struts2:form>
</body>
</html>
|
Looking at this index.jsp file, I can say the Register button submit action
points to "jsf-view" with namespace as "/sample-jsf" and the two text fields
as userName and roll.
Now the struts.xml file will be having the mapping for this action and the
Action class along with the configuration for defining JSF related interceptors
for using JSF mapping as the result for success from the Action class file
action method.
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>
<package name="jsf-example" namespace="/sample-jsf"
extends="jsf-default">
<action name="jsf-view" class="sample.ExampleJSFAction"
method="preview">
<interceptor-ref name="basicStack"/>
<interceptor-ref name="jsfStack"/>
<result name="success" type="jsf">
<param name="location">/preview.jsf</param>
</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
|
In order to use JSF related Tags in the preview.jsp file, we have to
define these interceptor-ref tags as mentioned in bold as shown above.
The Action class has two variables with same name as those defined
in the index.jsp file (userName, roll).
sample.ExampleJSFAction.java
package sample;
public class ExampleJSFAction
{
private String userName;
private String roll;
public ExampleJSFAction() {
}
public void setUserName(String arg) {
userName = arg;
}
public String getUserName() {
return userName;
}
public void setRoll(String arg) {
roll = arg;
}
public String getRoll() {
return roll;
}
public String preview() {
System.out.println(userName+" "+roll);
return "success";
}
}
|
the finsl preview JSP file is having JSF tags defined and the dynamic
values to this Page is comming from the Struts2 Action Object.
preview.jsp
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="sampleHtml" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="sampleCore" %>
<html>
<body>
<h4>Preview</h4>
<sampleCore:view>
<sampleHtml:form>
<table>
<tr><td>Name :</td><td><sampleHtml:outputText
value="#{action.userName}"/></td></tr>
<tr><td>Roll :</td><td><sampleHtml:outputText
value="#{action.roll}"/></td></tr>
</table>
</sampleHtml:form>
</sampleCore:view>
</body>
</html>
|

Software environment I have used to test this example is as follows:
1. JDK 1.5.x
2. Eclipse 3.2
3. Struts 2.1.6
4. JSF 1.0 specification API and Impl.
5. Jakarta Tomcat 6.0.18
And I have used following set of Jar files in the WEB-INF/lib folder,
in irder to run and test this example, and this works well in my local
software environment.

If you like to share your comment/suggestions/feedback relating to this Page,
you can do so by droping us an email at
usingframeworks @ gmail . com
with the subject line mentioning URL for this Page (i.e,
/struts-jsf-integrate.php) or use this
LINK.
As per this website's privacy policy, we never disclose your email id,
though we shall post your comments/suggestions/feedback with
your name (optional) and date on this Page. If you don't want your
comments/suggestions/feedback to be shared in this Page, please
mention so in your email to us. Thank you very much.....
If anything missed out , please let me know at
techienjoy at yahoo . com