| |
Struts Tutorials:
Struts2 Tag CheckBoxList , Checkbox, Iterator, IF
Struts2 Tag Library Example
Struts2 Tiles Example
Struts2 Tiles I18N Example
Struts2 Questions
Struts Tiles I18N Example
Struts Eclipse MVC
Struts2 Tags
Struts2 Example and Tutorial
Struts MVC
Struts2 Validation
Hibernate Tutorials:
Hibernate Case Study
Class Hierarchy Persist Example
Using Hibernate Interceptor
Hibernate Questions with Answer
Hibernate Many-to-Many Mapping Example
Hibernate one-to-many Mapping Example
Hibernate and ORM tools
Spring Hibernate Example
Hibernate SessionFactory Example
Hibernate Mapping Class Hierarchy
Hibernate Questions
Hibernate SessionFactory Questions
Spring Hibernate Example:
Spring Hibernate Case Study
Written By : guddu
Title :
Struts Eclipse MVC
Description :
Step by step demonstration More...
|
Written By : Girish
Title :
Struts Tiles I18N Example
Description :
Code with Sample Action More...
|
Written By : ISHTEK
Title :
Struts2 questions
Description :
open for discussion More...
|
Written By : ISHTEK
Title :
Struts2 Tiles Example
Description :
with Code and article More...
|
Written By : ISHTEK
Title :
Struts2 Tiles I18N Example
Description :
with code explained article More...
|
Written By : Amit
Title :
Struts2 Tag Library Example
Description :
Tutorial More...
|
Written By : Amit
Title :
Struts2 Tags Example
Description :
Code and Tutorial More...
|
Written By : Amit
Title :
Radio Tag Example
Description :
Step By Step Code More...
|
| Tags/Keywords : Struts, Struts2 Tutorial, Struts 2 Example, Struts2 Author : ISHTEK Date (Year/Month/Date): 2009-11-28
Struts 1 Plugin with Struts2 Framework | |
Please be informed that NONE of the design/code from this
page is claiming to be some sort of best practices and we DO NOT expect
any of our visitor/reader of this page to assume this as some sort of
best practice for any context and should not be using this
as it is without appropriate evaluation to their, so to say,
specific programming context.
This page intends only to provide bit and piece of known ways for
doing some sort of example and may not be fit for any other purpose.
In this writing I shall be showing all the steps I have followed
in order to make a Struts1 based Action class from a web application,
those cane be used in Struts2 Environment.
Let us setup a simple web application where I have designed a
very simple form with a single textfield to accept user input.
This user inputted value goes to a Action class, and then the
Action Forward to another JSP file happens. Simple isn't it!!
For this I have to create a Action class as follows:
action.ExampleAction.java
package action;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import javax.servlet.ServletException;
public class ExampleAction extends Action
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
ExampleForm exampleForm = (ExampleForm) form;
System.out.println("Value of Name:"+exampleForm.getName());
return mapping.findForward("success");
}
}
|
So the action form is ExampleForm as follows:
action.ExampleForm.java
package action;
import org.apache.struts.action.ActionForm;
import java.util.Date;
public class ExampleForm extends ActionForm
{
private String name;
private int age;
private Date dob;
public void setName(String argName) {
name = argName;
}
public String getName() {
return name;
}
public void setAge(int argAge) {
age = argAge;
}
public int getAge() {
return age;
}
public void setDob(Date argDob) {
dob = argDob;
}
public Date getDob() {
return dob;
}
}
|
I remember what i said earlier, there will be a single input textfield
then why are these three fields in Action form. Out of these three
fields I shall be using field "name" only, and the rest two fields
are there as place holder for some further examples on validations.
First screen/page of this example is index.jsp file.
As this JSP file is Struts1 based, so you may see Struts1 related
tag libraries are used here in this file as follows:
index.jsp
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html locale="true">
<head>
<title><bean:message key="index.title.label"/></title>
<html:base/>
</head>
<body bgcolor="white">
<html:errors/>
<table>
<html:form method="post" action="example">
<tr><td><bean:message key="index.name.label"/></td>
<td><html:text property="name"/></td></tr>
<tr><td><html:submit property="butSubmit" value="Submit"/></td>
<td><html:button property="butReset" value="Reset"/></td></tr>
</html:form>
</table>
</body>
</html:html>
|
As you can see from the above JSP file we have a action mapping
as "example" is to be defined in struts-config.xml file.
And the action form as "exampleForm" :
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- Action Form definition goes here -->
<form-beans>
<form-bean name="exampleForm" type="action.ExampleForm"/>
</form-beans>
<!-- global exception handling if any -->
<global-exceptions>
</global-exceptions>
<!-- global forward if any -->
<global-forwards>
</global-forwards>
<!-- Action definition goes here -->
<action-mappings>
<action path="/example"
type="action.ExampleAction"
name="exampleForm"
scope="request">
<forward name="success" path="/preview.jsp"/>
</action>
</action-mappings>
<!-- Tiles related request processor (not required in this example) -->
<controller
processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<!-- MessageResources.properties file for all messages used in UI JSP files. -->
<message-resources parameter="MessageResources" />
<!-- TilesPlugin definitions goes here -->
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config"
value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>
<!-- Validator Plugin definitions goes here -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
|
This configuration file has the next page as preview.jsp , when the
action forward is "success".
After setting up all the required components for this example web application,
such as all JAR files, web.xml file with configuration for Actionservlet,
tag library files, validation-rules and validation XML files etc.
when this index JSP file is accessed from the Browser, it shows the input
textfield to be entered by user and the on click of submit button, this
value from the textfield goes to the ExampleAction action class, and it
prints out on server console as
"Value of Name: <<Value as entered in text field on screen>>"
This shows that existing Struts 1.x example is all setup properly and functional.
Now is the time to move on to explore Struts1 Plugin with Struts2 framework.
As we might be knowing that we can reuse only the example Action and ActionForm
class (by the time i have written this example)and not all the other files,
such as Struts1 Tag Libraries in JSP files, and related configuration.
If any version of Struts1 Plugin has support for reusing
existing JSP files with Struts 1.x Tag Libraries, then please make us
aware by writing comment on this page.
So we have to move towards creating a web application with Struts2 Framework,
but reuse existing Action and ActionForm from Struts 1.x example as already seen
here.
So we have to write appropriate Struts2 related configuration file, and use
Struts 2 specific Tag Libraries in JSP files.
And we have to define related configuration file for the prepare and excute
filter that is comming with truts2 filter, as shown below:
web.xml
....
....
<filter>
<filter-name>sample-filter</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>sample-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
....
....
|
It is required to provide appropriate Struts2 specific JAR files, including
dependencies. In this example following are the JAR files I have used
to make a single web application using both Strus 1.x version and Struts2
but same set of Action and Action Form.
One may or may not require all these JARs, but for this example I have tested
using following set of JAR files in LIB folder of the web application.
One can observe that corresponding Plugin JAR file "struts2-struts1-plugin-2.1.8.1.jar"
is required to make this example work.
For this example, of course after so many trial and error and fact finding
I could able to put together following Struts Configuration XML file, that
is working for this example.
struts.xml (to be placed in WEB-INF/classes folder only)
<?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="sample-example" namespace="/sample1"
extends="struts1-default">
<interceptors>
<interceptor name="exampleForm"
class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor">
<param name="className">action.ExampleForm</param>
<param name="name">exampleForm</param>
<param name="scope">request</param>
</interceptor>
<interceptor-stack name="sample-example">
<interceptor-ref name="staticParams"/>
<interceptor-ref name="exampleForm"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="basicStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="sample-example"/>
<action name="example1" class="org.apache.struts2.s1.Struts1Action">
<param name="className">action.ExampleAction</param>
<result name="success">/sample-preview.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
|
In order to start with the struts.xml file, we have the Struts 1.x
Action form is defined as parameter with the name as className
for the scope model driven interceptor.
And the org.apache.struts2.s1.Struts1Action acts like a wrapper for the
Struts1.x Action class (for this example it is action.ExampleAction),
which is defined as param with name className.
In order to create a UI screen, we may have to create another JSP
file using Struts2 based Tag Library, in which appropriate namespace
and action mapping should be referred, as shown below:
sample.jsp
<%@ taglib uri="/struts-tags" prefix="struts2"%>
<html>
<head>
<link rel="stylesheet" type="text/css" href="sample.css" />
</head>
<body>
<struts2:actionerror cssClass="ss"/>
<struts2:form method="post" action="sample1/example1">
<table>
<tr><td><struts2:textfield key="name" label="Name "/></td></tr>
<tr><td colspan="2"><struts2:submit value="Register"/></td></tr>
</table>
</struts2:form>
</body>
</html>
|
Here "sample1" is the namespace and "example1" is the corresponding action
mapping in struts.xml file.
 | Author of this article/writeup has expressed his/her willingness |
to help or guide users with any technical difficulties he/she faces
while working with the example code environment setting up, running
and resolving any such exception raised during compile or at runtime.
You may ask for any technical doubt or seek technical help related to
this article by using following form to reach for technical help
from the Author for FREE.
This article's Author shall be reading your request and responding
within reasonable time (no resolution timeframe defined as such).
|
| Are you interested in solving a very interesting Technology Stack while Playing this Game 
|
|
| Home >>> Struts Struts 2 Example Tutorial >>> Struts Struts2 Plugin >>> example using Struts and Struts2 |
|
|
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
|
|
|
|
|
<- requires login | Log in or Register | |
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved. | CONTACT PRIVACY POLICY DISCLAIMER |
 |
Terms of Use and Disclaimer :
This web site provides some of the information about various technologies, example
code, tips, tutorials etc. Like any printed materials, content of these pages may
become out of date over a period of time. Therefore all visitor/users of this web
site are requested/advised to refer to the originating parties/sources for the
latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating
parties/sources.
By accessing and using this website in any ways, including, without
limitation, browsing the website pages, using any information, using any content and/or
downloading any materials, you agree to and are bound by the terms of use
described in this page and Usage Terms and Conditions.
If you do not agree to all of
the terms and conditions contained in the terms of use described in this
page and Usage Terms and Conditions, do not use this
website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use
on your employer's behalf.
All Trademarks are property of their respective owner. Appropriate measure is being
taken for providing accurate and up-to-date information but like any printed materials,
these blog(s)/contents may eventually be outdated one day, so if you are using any
of these information, please refer original content/documentation from respective sources.
And under no circumstances shall the Author of these contents and/or this web site
be liable for any loss, damage, expense incurred or suffered which is claimed to have
occurred because of usage of the contents of this web site.
If you have any questions/queries/feedback/suggestions then please write to this web
site owner at contact.
|  |
|
|
|
|
|