Posted In: Struts

Struts2 validation and internationalization i18n example

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Struts2App</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
	<filter>
		<filter-name>struts-prepare</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter
		</filter-class>
	</filter>
	<filter>
		<filter-name>struts-execute</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts-prepare</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>struts-execute</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>
<?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>
	<constant name="struts.devMode" value="true" />
	<include file="employee.xml" />
</struts>
<?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="employee" namespace="/employee" extends="struts-default">
    <interceptors>
      <interceptor-stack name="defaultStack">
        <interceptor-ref name="exception" />
        <interceptor-ref name="alias" />
        <interceptor-ref name="servletConfig" />
        <interceptor-ref name="prepare" />
        <interceptor-ref name="i18n" />
        <interceptor-ref name="chain" />
        <interceptor-ref name="debugging" />
        <interceptor-ref name="profiling" />
        <interceptor-ref name="scopedModelDriven" />
        <interceptor-ref name="modelDriven" />
        <interceptor-ref name="fileUpload">
          <param name="maximumSize">10240</param>
          <param name="allowedTypes">image/jpeg,image/gif,image/png</param>
        </interceptor-ref>
        <interceptor-ref name="checkbox" />
        <interceptor-ref name="staticParams" />
        <interceptor-ref name="actionMappingParams" />
        <interceptor-ref name="params">
          <param name="excludeParams">dojo\..*,^struts\..*</param>
        </interceptor-ref>
        <interceptor-ref name="conversionError" />
        <interceptor-ref name="validation">
          <param name="excludeMethods">input,back,cancel,browse</param>
        </interceptor-ref>
        <interceptor-ref name="workflow">
          <param name="excludeMethods">input,back,cancel,browse</param>
        </interceptor-ref>
      </interceptor-stack>
    </interceptors>
    <action name="*" method="{1}" class="com.company.strutsapp.EmployeeAction">
      <interceptor-ref name="defaultStack" />
      <result name="input">/WEB-INF/jsp/insert.jsp</result>
      <result name="success">/WEB-INF/jsp/success.jsp</result>
    </action>
	</package>
</struts>
package com.company.strutsapp;

import java.io.Serializable;
import java.util.Date;

public class EmployeeBean implements Serializable{
  private String fname;
  private String lname;
  private String salary;
  private String idnumber;
  public String getFname() {
    return fname;
  }
  public void setFname(String fname) {
    this.fname = fname;
  }
  public String getLname() {
    return lname;
  }
  public void setLname(String lname) {
    this.lname = lname;
  }
  public String getSalary() {
    return salary;
  }
  public void setSalary(String salary) {
    this.salary = salary;
  }
  public String getIdnumber() {
    return idnumber;
  }
  public void setIdnumber(String idnumber) {
    this.idnumber = idnumber;
  }
 
}
package com.company.strutsapp;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends ActionSupport {
  private final Logger logger = Logger.getLogger(EmployeeAction.class);
  private EmployeeBean employeeBean = new EmployeeBean();

  public EmployeeBean getEmployeeBean() {
    return employeeBean;
  }

  public void setEmployeeBean(EmployeeBean employeeBean) {
    this.employeeBean = employeeBean;
  }

  public String input() throws Exception {
    logger.info("called");
    return INPUT;
  }

  public String insert() throws Exception {
    logger.info("called");
    return SUCCESS;
  }

  public void validate() {
    if (StringUtils.isEmpty(employeeBean.getFname())) {
      addFieldError("employeeBean.fname",
          getText("employeeBean.fname.required"));
    }
    if (StringUtils.isEmpty(employeeBean.getLname())) {
      addFieldError("employeeBean.lname",
          getText("employeeBean.lname.required"));
    }
    if (NumberUtils.toInt(employeeBean.getIdnumber()) == NumberUtils.INTEGER_ZERO) {
      addFieldError("employeeBean.idnumber",
          getText("employeeBean.idnumber.required"));
    }
    if (NumberUtils.toDouble(employeeBean.getSalary()) == NumberUtils.DOUBLE_ZERO) {
      addFieldError("employeeBean.salary",
          getText("employeeBean.salary.required"));
    }
  }
}
employeeBean.fname=\u092a\u0939\u0932\u093e  \u0928\u093e\u092e
employeeBean.lname=\u0905\u0902\u0924\u093f\u092e \u0928\u093e\u092e
employeeBean.idnumber=\u0915\u094d\u0930\u092e\u093e\u0902\u0915 \u0938\u0902\u0916\u094d\u092f\u093e 
employeeBean.salary=\u0935\u0947\u0924\u0928
employeeBean.fname.required=\u092a\u0939\u0932\u093e \u0928\u093e\u092e \u0906\u0935\u0936\u094d\u092f\u0915
employeeBean.lname.required=\u0905\u0902\u0924\u093f\u092e \u0928\u093e\u092e  \u0906\u0935\u0936\u094d\u092f\u0915      
employeeBean.idnumber.required=\u0915\u094d\u0930\u092e\u093e\u0902\u0915 \u0938\u0902\u0916\u094d\u092f\u093e \u0930\u093f\u0915\u094d\u0924 \u0928\u0939\u0940\u0902 \u0939\u094b \u0938\u0915\u0924\u093e \u0939\u0948, \u0936\u0942\u0928\u094d\u092f
employeeBean.salary.required=\u0935\u0947\u0924\u0928 \u0930\u093f\u0915\u094d\u0924 \u0928\u0939\u0940\u0902 \u0939\u094b \u0938\u0915\u0924\u093e \u0939\u0948, \u0936\u0942\u0928\u094d\u092f 

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>


Struts2 Validation and i18n











Tags: , ,

by , on January 14th, 2017

  • Categories