| |
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 : Amit
Title :
MVC Struts
Description :
Some facts about MVC Framework and Struts More...
|
Written By : Amit
Title :
Struts2 example tutorial
Description :
My First example using Apache Struts2 More...
|
Written By : ISHTEK
Title :
Struts2
Description :
Tags such as IF Iterator Tags More...
|
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...
|
| Tags/Keywords : Struts, Struts2 Tutorial, Struts 2 Example, Struts2 Author : guddu Date (Year/Month/Date): 2009-12-05
Struts-Struts2-Validation - ResourceBundle-Message-Issues | |
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 example we shall how to reuse Action and Action Form from Struts 1.x
based web application.
Looking back to the already posted example on using Struts 1.x action and action form
In order to use the validation along with the action and actionform.
First we shall see how user input validation is carried out in case of
a web application that is using Struts 1.x as MVC Architecture.
We have the Action form and Action class from an existing example are 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");
}
}
|
And Action Form as
action.ActionForm.java
package action;
import org.apache.struts.validator.ValidatorForm;
import java.util.Date;
public class ExampleForm extends ValidatorForm
{
private String name;
public ExampleForm() {
}
public void setName(String argName) {
name = argName;
}
public String getName() {
return name;
}
}
|
One quick question:
Can I re-use these two class files for another web application
that is going to use Struts2 as MVC Framework
Or if an already existing web application using Struts 1.x
MVC Framework to be upgraded to use Struts2 at some
point of time in the evolving of any web application.
One thing to remember here is that we may not be able to re-use
existing JSP and UI tier files as Tag Library for Struts2 is
different from Struts 1.x Tag Library.
So let us create an example web application by put together various
parts like root folder, lib, classes folders with various JAR file
in lib folder, and struts.xml configuration file in
"ROOT_FOLDER/WEB-INF/classes" folder, following is the folder structure
<<ROOT_FOLDER>>/WEB-INF/web.xml
<<ROOT_FOLDER>>/WEB-INF/lib/antlr.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-beanutils.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-digester.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-fileupload-1.2.1.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-fileupload.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-io-1.3.2.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-logging-1.1.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-logging.jar
<<ROOT_FOLDER>>/WEB-INF/lib/commons-validator-1.3.1.jar
<<ROOT_FOLDER>>/WEB-INF/lib/freemarker-2.3.13.jar
<<ROOT_FOLDER>>/WEB-INF/lib/jakarta-oro.jar
<<ROOT_FOLDER>>/WEB-INF/lib/ognl-2.6.11.jar
<<ROOT_FOLDER>>/WEB-INF/lib/struts2-core-2.1.6.jar
<<ROOT_FOLDER>>/WEB-INF/lib/struts2-struts1-plugin-2.1.8.1.jar
<<ROOT_FOLDER>>/WEB-INF/lib/struts.jar
<<ROOT_FOLDER>>/WEB-INF/lib/xwork-2.1.2.jar
(* these are the files I have used for upgrading
an existing Struts 1.x based web application to Struts2 based MVC Architecture.
Please don't consider this as standard way of upgrading to Struts2
You may have to look at many aspects of any application before upgrade)
You might have noted that there is struts.jar file also present along
with Struts2 core and Struts2 Plugin related JAR file.
This is to make existing Action and ActionForm to work or compile properly
as we are using some of the Java Class Files those are using some of the
class files from struts.jar file.
<<ROOT_FOLDER>>/WEB-INF/classes/struts.xml
<<ROOT_FOLDER>>/WEB-INF/classes/action/ExampleAction.java
<<ROOT_FOLDER>>/WEB-INF/classes/action/ExampleForm.java
<<ROOT_FOLDER>>/WEB-INF/validation.xml
<<ROOT_FOLDER>>/WEB-INF/validation-rules.xml
|
In order to provide ways for including Validation from the Action Form
we have to provide appropriate Interceptor like "ActionFormValidationInterceptor"
This interceptor requires pathnames as param with value as the validation
and validation-rules XML configuration files.
Let us step through the struts.xml file as follows:
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="sample-example" namespace="/sample1"
extends="struts1-default">
<interceptors>
<!-- Defining struts 1.x base action form with scope -->
<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>
<!-- Example Form definition for the validation with
validator framework -->
<interceptor name="exampleValidation" class="org.apache.struts2.s1.ActionFormValidationInterceptor">
<param name="pathnames">
/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml
</param>
</interceptor>
<!-- Defining required interceptor stack with valious interceptor references -->
<interceptor-stack name="sample-example">
<interceptor-ref name="staticParams"/>
<interceptor-ref name="exampleForm"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="basicStack"/>
<interceptor-ref name="exampleValidation"/>
<interceptor-ref name="workflow"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="sample-example"/>
<!-- Example Action wrapper class from Struts2 plugin -->
<action name="example1"
class="org.apache.struts2.s1.Struts1Action">
<param name="className">action.ExampleAction</param>
<param name="validate">true</param>
<result name="success">/sample-preview.jsp</result>
<result name="input">/sample.jsp</result>
</action>
</package>
</struts>
|
Defining Web Application Archive configuration file is having configuration
for both Struts 1.x ActionServlet and Struts2 "StrutsPrepareAndExecuteFilter"
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>
Struts 1 with Struts 2 and Plugin
</display-name>
<!-- Struts 1.x Actionservlet Definition -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- Struts 1.x Tag Library -->
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>
/WEB-INF/struts-bean.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>
/WEB-INF/struts-html.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>
/WEB-INF/struts-logic.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>
/WEB-INF/struts-nested.tld
</taglib-location>
</taglib>
<!-- Tiles related Tag Library Definition -->
<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>
/WEB-INF/struts-tiles.tld
</taglib-location>
</taglib>
<!-- Struts2 Filter for Prepare and Execute Appropriately -->
<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>
</web-app>
|
Now let us look at the validation and validation-rules XML configuration
files,
validation.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>
<global>
</global>
<formset>
<form name="exampleForm">
<field property="name" depends="required">
<arg0 key="exampleForm.name.text"/>
</field>
</form>
</formset>
</form-validation>
|
Now as per the validation entry shown above, name firld in action form
should be a mandatory field to be entered from UI.
When this example is executed using Tomcat Web server and and from browser
the sample.jsp file is accessed, UI as shown below is seen on screen.
And after clicking submit button, following is the error message
OOPs..... why is this errors.required is showing on screen, i think
it is from the Struts 1.x action error default key, that is getting
shwon on screen, then where should i define the messages so that
Struts2 Plugin will read read appropriate error message.
I can recall from my memory, is that while using Struts 1.x
based web application I defined appropriate error message in the
/WEB-INF/classes/MessageResources.properties file.
I tried putting the same resource bundle properties file in this
example, but no success.
If anyone faced this issue/problem, please write your suggestions
or recomendations below form/section.
Thanks in advance.
|
| Are you interested in solving a very interesting Technology Stack while Playing this Game 
|
|
| Home >>> Struts Struts 2 Example Tutorial >>> Struts Struts2 Validation >>> ResourceBundle Message Issues |
|
|
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.
|  |
|
|
|
|
|