Advertisement
Home > Struts Struts 2 Example Tutorial > Struts Struts2 Validation > ResourceBundle Message IssuesPlease log in to add or reply to any matter<- requires login or
RMI Example

Home > Struts Struts 2 Example Tutorial > Struts Struts2 Validation > ResourceBundle Message Issues
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 :
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...


Written By : ISHTEK
Title :
Struts2 questions
Description : open for discussion
More...


Written By : ISHTEK
Title :
Struts2 Tiles Example
Description : with Code and article
More...

Struts Struts2 Validation, Struts 1.x, Validation, ResourceBundle, Message, Issues
Author : guddu
Date (Year/Month/Date): 2009-12-05 Struts-Struts2-Validation - ResourceBundle-Message-Issues
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");
        }

}
Advertisement
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>
Advertisement
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.
Please write your Comment on this Matter
(This will be visible if found suitable):
Name: *
Email (will not be displayed): *
Matter: *
11,10
Enter bigger number from above :*
Home > Struts Struts 2 Example Tutorial > Struts Struts2 Validation > ResourceBundle Message Issues
Visitor/User submitted related resources:
(Visible upon review and approved by this site Administrator)
Referred By Name *:
Resource URL *: (e.g, URL should be starting with http://www.-----.---)
 
Resource Short Description *:
14,31
Enter bigger number from above : *

Please log in to add or reply to any matter<- requires login
Log in or Register
This List is generated as on 2009-07-12 (YYYY-MM-DD)
#Discuss-these : questions-and-answer : Interview-Questions-on-Java #Some-facts-about-MVC-Framework-and-Struts : MVC-Struts : Struts-Struts-2-Example-Tutorial #My-First-example-using-Apache-Struts2 : Struts2-example-tutorial : Struts-Struts-2-Example-Tutorial This article discusses steps of creating a Struts2 based example web application With lots of easy to understand explaination and source code for reference. #Tags-such-as-IF-Iterator-Tags : Struts2 : Struts-Struts-2-Example-Tutorial #Step-by-step-demonstration : Struts-Eclipse-MVC : Struts-Struts-2-Example-Tutorial #Code-with-Sample-Action : Struts-Tiles-I18N-Example : Struts-Struts-2-Example-Tutorial #open-for-discussion : Struts2-questions : Struts-Struts-2-Example-Tutorial #with-Code-and-article : Struts2-Tiles-Example : Struts-Struts-2-Example-Tutorial #with-code-explained-article : Struts2-Tiles-I18N-Example : Struts-Struts-2-Example-Tutorial #Tutorial : Struts2-Tag-Library-Example : Struts-Struts-2-Example-Tutorial #Code-and-Tutorial : Struts2-Tags-Example : Struts-Struts-2-Example-Tutorial #Step-By-Step-Code : Radio-Tag-Example : Struts-Struts-2-Example-Tutorial #Struts-2-Code : Struts2-Validation-Example : Struts-Struts-2-Example-Tutorial #Struts-2-Example-with-Code : Struts2-HibernateSession-Plugin : Struts-Struts-2-Example-Tutorial
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved.
CONTACT    PRIVACY POLICY    DISCLAIMER

This web site provides some of the information about various technologies, example 
code, tips, tutorials etc. Like any printed matterials, 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.