Struts 2 Validation Form With I18N

Step 1 – Create New Maven-based Dynamic Web Project

Step 1.1 Create new dynamic web project

Go to File->New->Others… Select Dynamic Web Project under Web category then click Next. Type Struts2Demo for the project name (you can choose your own name). Click Finish.

Step 1.2 Convert the project to Maven Project

Right click to the project; select Configure->Convert to Maven Project. Eclipse will generate Project Object Model (pom.xml) file. Open the file by Eclipse text editor then paste the content below.

Note: you can create normal java project and add jar files to your project build path. But it’s easier & faster using maven as maven is integrated with Eclipse IDE; so you don’t need to manually install it. 

Step 2 Create Web Deployment Descriptor (web.xml)

You can create web.xml when creating project or do it later. Right click to the project; select Java EE Tools -> Generate Deployment Descriptor Stub to generate web.xml.

Step 3 Create Struts Configuration file

Create an empty xml file named struts.xml under package WEB-INF\classes folder and paste the content below.

Step 4 Message Resource Properties Files

Create 2 properties files under WEB-INF/classes folder. MessageBundle.properties is by default for English; and MessageBundle_es.properties for Spanish.

Step 4.1 MessageBundle.properties (default)

Step 4.2 MessageBundle_es.properties

Step 4 Java Bean Class

Step 5 Create Struts Action Class

Right click to the project or src folder; select New->Class. Class name & package must be the same as we declared in struts.xml.

Step 6 Create JSP Pages

In this example we need 2 JSP files. Right click to the project or WebContent folder; select New -> JSP File

Step 6.1 register.jsp

Step 6.2 thankyou.jsp

Step 7 Configure Tomcat Server

  • In Servers window, add new tomcat server; select Tomcat v7.0 Server then click Next. In case you don’t see the Servers window, you can go to Windows->Show View->Others… Select Servers under Server category.
  • In the next window, click Browse button to select Tomcat installation directory. Select the folder then click Ok.
  • Click Installed JREs, to configure configure runtime environment. Remove existing JRE then click Add button to add new one. Select Standard VM then click Next. Click Directory button to select JDK home folder; select the folder then click Ok.
  • Change the JRE to the JDK then click Finish.

Step 8 Deploy & Run

  • Go to Project -> Clean to clean & build the project. Eclipse will download & add maven dependencies to your project build path; then compile your classes.
  • Right click to the tomcat server; select Add & Remove. Move the application to the right to deploy it to the server.
  • Start the tomcat server
  • Open web browser and run the below URL. We assume that you install tomcat on port 8080 and context path is Struts2Demo. By default project name is same as context path:

http://localhost:8080/Struts2Demo/register.jsp

Output

Enter your personal information then submit. There’re some validations rule need to be satisfied:

  • First name is required
  • Email is required
  • Age must be greater or equal 18

Toggle languages by clicking locale links English | Español

formValidI18n formValidI18nES

Enter valid information then submit.

In case Spanish: