Skip to main content

Posts

Showing posts with the label Struts 2

Struts2 Resource Handling - Using Hindi language in internationalization (i18n)

Struts2 provides us different ways for handling message resources. Struts2 supports internationalization for different locales and we can provide message resources for different locales. Struts2 will use resource file naming convention through which it identifies that which message resource file to be used within the particular session. See the following configuration. <? xml   version = "1.0"   encoding = "UTF-8" ?> <! DOCTYPE   struts   PUB      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"      "http://struts.apache.org/dtds/struts-2.0.dtd" > < struts >      < constant   name = "struts.custom.i18n.resources"   value = "global"   / >      < package   name = "default"   namespace = "/"   extends = "struts-default" >          < ac...

Struts2 - Enabling Client Side validation

Struts2 provides the declarative option to define the validation rules for the data fields in form. You can use the default implementation of validation framework of the struts2 to validate various kind of data. When you use the validation feature of the struts2, it is done server side by default. But setting a single attribute of the form tag, it can be applied at client side. Struts2 validation process can work on server side as well as client side. When it works at server side, server return input form containing validation error along with  the text fields. when you use client side validation feature, the javascript will process the validate without sending the form data to server for validation. it means data is validated before going to server. this javascript is generated by struts frame work into the rendered webpage having the form. this validation works when you set the validation attribute to true in the form tag. And you have the add the head tag of the strut...

Struts2 - Writing custom validator

Sometimes it is needed that the existing validation rules those are implemented by struts framework do not satisfy project validation criteria. That you have to write your logic according to your requirements. Struts2 provides the facility to write your own validation class in which you can implement your own validation logic. Step-1: Write down a class that extends inbuilt validator support class. In this example I am extending the FieldValidatorSupport class that the validate () method that has to be overridden by your custom validator class. Here we can create the variable (i.e. property) e.g. domainName (or other) and its getter-setter in which the values will be passed from validation (xml) file as parameters. Method validate () is overridden to implement your validation logic. This method takes the Object to be validated at run time that is passed by interceptor. The method  getFieldValue () which is inherited from super class will help you to retrieve the actual va...

Struts 2 | ModelDriven to handle Form Data

In struts2, values of request parameters(HTML form's fields) are mapped to matching properties of the action class . Struts populates the values come in Http Request to the variables having same name of input fields of the HTML form. When we want to persist the data received form request, it needs to be in model object that can be persisted by ORM framework like JPA or Hibernate. So if we are using the Action class to handle the values from form, we have to mark it as Entity for JPA/Hibernate to persist its data. Therefore using the action class as Entity class is not a best practice. Struts2 provides another option to handle to form data. We can use separate Entity bean/Object to receive the form data rather than Action class. In terms of struts, it is called Model object. This is the same thing as Entity object in terms of JPA/Hibernate. In struts, we have to implement the ModelDriven interface that has the getModel method. This method returns the model object an...

How to handle values from dynamically generated elements in web page using struts2

Some time you will see the form containing the button " Add More " . This facility is provided for the user to get the values for unknown number of repeating for some information. for example when you are asking to get the projects details from user, you need to put the option to add the more project for the user since you don't known how many projects user have. In the HTML form, you repeat the particular section to get the multiple values for those elements. In Html page , you can put the option to add new row of elements or text fields by writing the java script or using JQuery API. Now, the question is that how to capture the values of dynamically generated text fields on the server. Using the servlet programming you can get the values by using getParameters() method that resultants the array of the parameter having the same name. But this limit you to naming the text fields in the HTML form. To ally this approach, you have to take the same name for t...