<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title><s:property value="getText('global.form')" /> - Struts2 Demo | dineshonjava.com</title> <s:head /> </head> <body> <h2><s:property value="getText('global.form')" /></h2> <s:form action="employee" method="post" validate="true" > <s:textfield name="name" key="global.name" size="20"/> <s:textfield name="age" key="global.age" size="20" /> <s:textfield name="email" key="global.email" size="20" /> <s:textfield name="telephone" key="global.telephone" size="20" /> <s:submit name="submit" key="global.submit" align="center" /> </s:form> <s:url id="localeEN" namespace="/" action="locale" > <s:param name="request_locale" >en</s:param> </s:url> <s:url id="localeHN" namespace="/" action="locale" > <s:param name="request_locale" >hn</s:param> </s:url> <s:url id="localeES" namespace="/" action="locale" > <s:param name="request_locale" >es</s:param> </s:url> <s:url id="localezhCN" namespace="/" action="locale" > <s:param name="request_locale" >zh_CN</s:param> </s:url> <s:url id="localeDE" namespace="/" action="locale" > <s:param name="request_locale" >de</s:param> </s:url> <s:url id="localeFR" namespace="/" action="locale" > <s:param name="request_locale" >fr</s:param> </s:url> <s:a href="%{localeEN}" >English</s:a> <s:a href="%{localeHN}" >Hindi</s:a> <s:a href="%{localeES}" >Spanish</s:a> <s:a href="%{localezhCN}" >Chinese</s:a> <s:a href="%{localeDE}" >German</s:a> <s:a href="%{localeFR}" >France</s:a> </body> </html>
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title><s:property value="getText('global.form')" /> - Struts2 Demo | dineshonjava.com</title> </head> <body> <h2><s:property value="getText('global.success')" />.</h2> </body> </html>Create Action:
package com.dineshonjava.struts2.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator; import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator; /** * @author Dinesh Rajput * */ @Results({ @Result(name="success", location="/success.jsp"), @Result(name="input", location="/employee.jsp") }) public class EmployeeAction extends ActionSupport { private static final long serialVersionUID = 1L; private String name; private Integer age; private String email; private String telephone; @Action(value="/employee") public String addEmployee() { return SUCCESS; } @RequiredFieldValidator( message = "The name is required" ) public String getName() { return name; } public void setName(String name) { this.name = name; } @IntRangeFieldValidator(message = "Age must be in between 28 and 65", min = "18", max = "65") public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @RequiredFieldValidator( message = "The email is required" ) public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @RequiredFieldValidator( message = "The phone is required" ) public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts2I18N</display-name> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> <init-param> <param-name>struts.devMode</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>struts.custom.i18n.resources</param-name> <param-value>global</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>employee.jsp</welcome-file> </welcome-file-list> </web-app>
Labels: struts2