Tech I Enjoy Logo

Custom Search




  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
Apache Struts 2 Validation Example :
Validation Example Steps of using Struts 2
and Example code explained.
Struts 2 Tiles and I18N Example :
Example using Tiles and I18N with Struts 2 and 
code explained.
Apache Struts 2 Tiles and I18N Example :
Example Steps of using Struts 2 With Tiles
and I18N code explained.
Apache Struts 2 Tags Example :
Example Steps of using Struts 2 Tags
and code explained.
Apache Struts 2 PDF Result :
Example Steps of using Struts 2 with PDF Result
and code explained.
Apache Struts with DOJO Example Part-1 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts 2 Radio Tag Example :
Example Steps of using Struts 2 Radio Tags
and code explained.
Apache Struts 2 Tags Example :
Example of Struts 2 Tags and code explained.
Apache Struts 2 and XSLT With Example :
XSLT Example using Struts 2 and Example 
code explained.
Apache Struts 2 upload Example :
Uploading Example Steps of using Struts 2
and Example code explained.
Apache Struts 2 Result PlainText Example :
Example Steps of using Struts 2 PlainText Result
and code explained.
Apache Struts 2 Custom Validator :
Example Steps of using Struts 2 with Custom Validator
and code explained.
Apache Struts 2 Validation With Example :
Validation Example using Struts 2
and Example code explained.
Apache Struts with DOJO Example Part-3 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts with DOJO Example Part-4 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts Validation with Example :
Example of Validation using Struts and 
code explained.
Struts 2 with JSF Example :
Example using JSF with Struts 2 and 
code explained.
Apache Struts 2 Validation With Expression :
Validation with Expression using Struts 2
and Example code explained.
Struts 2 Result Chain Example :
Example using Result Chain with Struts 2 and 
code explained.
Apache Struts Date Validation with Example :
Example of Date Validation using Struts and 
code explained.
Struts 2 Plugin Example :
Example using Struts 2 Plugin
List of Struts Example :
Examples List using Struts 2 and 
code explained.
Apache Struts with DOJO Example Part-2 :
Example of DOJO Toolkit with Struts and 
code explained.
Struts Validation Example :
Example using Validation with Struts 2 and 
code explained.
Apache Struts 2 Example Steps :
Example Steps of using Struts 2 and code explained.


References :
Tags: Struts 1 Struts2 plugin
Tags: struts jsf integrate
Tags: struts result chain
Tags: struts tiles i18n
Tags: Struts validation struggle
Tags: Struts
Tags: struts1 date validation
Tags: struts1 struts2 validation
Tags: struts2 dojo 1
Tags: struts2 dojo 2
Tags: struts2 dojo 3
Tags: struts2 dojo 4
Tags: Struts2 example steps Tag usage
Tags: Struts2 example steps
Tags: struts2 own validator
Tags: struts2 pdf result
Tags: struts2 radio tag example
Tags: struts2 result plaintext
Tags: struts2 tags example
Tags: struts2 tiles i18n
Tags: struts2 upload example
Tags: struts2 validation 1
Tags: struts2 validation expression
Tags: struts2 validation
Tags: struts2 xslt article



DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
techienjoy.com is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.

In spite of all precautions taken to avoid any typo in these pages, there might be some 
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.



Android Examples || Android Examples

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions