26.struts to demonstrate I18N


HelloWorldAction.java :-
package com.struts;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
public class HelloWorldAction extends ActionSupport {
private String message;
private Date nowDate;
@Override
public void validate(){
if (message==null || message.length()==0)
addActionError(getText(“error.enter.message”));
}
@Override
public String execute() throws Exception {
nowDate = new Date();

return SUCCESS;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Date getNowDate() {
return nowDate;
}
}

HelloWorld.jsp :-
<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″
pageEncoding=”ISO-8859-1″%>
<%@ taglib prefix=”s” uri=”/struts-tags” %>
<?xml version=”1.0″ encoding=”UTF-8″ ?>
<html>
<head>
<title>Hello World!</title>
</head>
<body>

<h2>Thank you for your message on <s:property value=”nowDate” /></h2>
<p>
Your Message was:<br/><br/>
<s:property value=”message” />
</p>
</body>
</html>

index.jsp :-
<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″ pageEncoding=”ISO-8859-1″%>
<%@ taglib prefix=”s” uri=”/struts-tags” %>
<?xml version=”1.0″ encoding=”UTF-8″ ?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html>
<head>
<title>Basic Struts 2 Project Setup</title>
<s:head/>
</head>
<body>
<h1><s:text name=”welcome” /></h1>
<p>
<s:url id=”localeEN” namespace=”/” action=”” >
<s:param name=”request_locale” >en</s:param>
</s:url>
<s:url id=”localeDE” namespace=”/” action=”” >
<s:param name=”request_locale” >de</s:param>
</s:url>
<s:url id=”localeFR” namespace=”/” action=”” >
<s:param name=”request_locale” >fr</s:param>
</s:url>
<s:a href=”%{localeEN}” >English</s:a>
<s:a href=”%{localeDE}” >German</s:a>
<s:a href=”%{localeFR}” >French</s:a>
</p>
<s:if test=”hasActionErrors()”>

</s:if>
<s:form action=”hello” namespace=”/” method=”post” name=”myForm” theme=”xhtml”>
<s:textfield name=”message” size=”40″ maxlength=”40″ required=”true” key=”your.message-label”/>
<s:submit key=”submit” />
</s:form>
</body>
</html>

global.properties (English) :-
submit=submit
your.message-label=Your message
welcome=Welcome to Struts 2!
error.enter.message=Please enter a Message!

global_de.properties (German) :-
submit=Absenden
your.message-label=Ihre Nachricht
welcome=Willkomen zu Struts 2!
error.enter.message=Please enter a message!

global_fr.properties (French) :-
submit=Soumettre
your.message-label=votre message
welcome=Bienvenue a Struts 2!
error.enter.message=Please enter a message!