Theme Templates
If you do not specify a topic, then the default XHTML theme is used in Struts2. For example, in Struts 2, select a label:
<s:textfield name= "name" label= "name"/>
To generate HTML markup:
<tr>
<td class= "Tdlabel" >
<label for= "Empinfo_name" class= "label" >Name:</label>
</td><td>
<input type= "text" name= "name" value= "" id= "Empinfo_name"/>
</td>
</tr>
This defines the action name in the Empinfo struts.xml file.
Select a topic:
You can specify the theme struts 2 based on each label or the specified theme Struts 2 used, you can use one of the following methods:
- Specific label for subject attributes
- Peripheral form labels for subject attribute labels
- Page-wide properties, named "Theme"
- The request scope property is named "Subject"
- The session scope property is named "Subject"
- The properties of an application scope are named "Subject"
In the Struts.properties Struts.ui.theme property (default is XHTML)
The following syntax specifies that they are at the label level, if they are willing to use different themes for different labels:
<s:textfield name= "name" label= "name" Theme= "xhtml"/>
Because it is not very practical, each label is based on the use of the theme, so simply we can specify the rules struts.properties file using the following tags:
# Standard UI Theme
struts.ui.theme=xhtml
# Directory where theme template resides
struts.ui.templatedir= Template
# Sets the default template type. Either FTL, VM, or JSP
STRUTS.UI.TEMPLATESUFFIX=FTL
The following result is that we picked up the struts-default.properties file from the localization section where we use the default settings Struts.ui.theme= XHTML, by default, in Struts2-core.xy.z.jar file, which is by subject.
How does the topic work?
For a given topic, each struts tag has an associated template, such as: S:textfield-> TEXT.FTL and S:password-> PASSWORD.FTL and so on, these template files to compress struts2-core.xy.z.jar files. These template files keep a predefined HTML layout for each label. So the STRUTS2 framework generates the final HTML tag code using the sturts tag and related templates.
Struts 2 tags + associated template file = Final HTML markup code.
The default templates are already written in freemarker and they have extensions. FTL. You can design a speed or JSP template and configure it to use Struts.ui.templateSuffix and Struts.ui.templateDir struts.properties.
To create a new theme:
The easiest way to create a new theme is to copy any existing subject/template files and make the necessary modifications. So let's start by creating a folder webcontent/web-inf/classes named templates and subfolders with our new subject name, such as Webcontent/web-inf/classes/template/mytheme. From here, you can build the template from scratch, or you can copy the template from the STRUTS2 distribution and modify it as needed.
We want to modify the existing default template XHTML learning purposes. So now let's copy the content from struts2-core-x.y.z.jar/template/xhtml to our topic directory and just modify the webcontent/web-inf/classes/template/mytheme/ CONTROL.FTL files. When we open CONTROL.FTL it will have the following lines:
<table class= "${parameters.cssclass?default (' wwformtable ')? HTML}" < #rt/>
< #if parameters.cssstyle ?? > style= "${parameters.cssstyle?html}" < #rt/>
</#if >
>
Let us control.ftl the above document change has the following content:
<table style= "border:1px solid black;" >
If checking to see FORM.FTL will find that control.ftl this file, FORM.FTL This file refers to the XHTML theme. So let's change the following:
< #include "/${PARAMETERS.TEMPLATEDIR}/XHTML/FORM-VALIDATE.FTL"/>
< #include "/${ PARAMETERS.TEMPLATEDIR}/SIMPLE/FORM-COMMON.FTL "/>
< #if (Parameters.validate?default (false)) >
onreset= "${parameters.onreset?default (' Clearerrormessages" (this);
Clearerrorlabels (this); ')} "
< #else >
< #if parameters.onreset?? >
onreset= "${parameters.onreset?html}"
</#if >
</#if >
>
< #include "/$ {PARAMETERS.TEMPLATEDIR}/MYTHEME/CONTROL.FTL "/>
I assume that there won't be much understanding of the Freemarker template language, still looking for FTL files what needs to be done to get a good idea. However, let us, in addition to the above changes, and return to our localized example, create the following contents of the Webcontent/web-inf/classes/struts.properties file:
# customized them
struts.ui.theme=mytheme
# Directory where theme template resides
struts.ui.templatedir= Template
# Sets the template type to FTL.
Struts.ui.templatesuffix=ftl
Now after this change, right click on the project name and click Export > War file to create a war document. This war is then deployed under Tomcat's WebApps directory. Finally, start the Tomcat server and try to access the URL http://localhost:8080/HelloWorldStruts2. This will give you the following picture:
XHTML theme After the change, we did a topic this is a result and you can see a border around the form component. Freemarker Learning, if you work hard, then you will be able to create or modify themes easily. At least for now, you must have a basic understanding of Sturts2 themes and templates.
Localization/Internationalization
internationalization (i18n) is the process of planning and implementing products and services that enable them to easily adapt to specific local languages and cultures, a process known as localization. The process of internationalization is sometimes referred to as translation or localization enablement. Internationalization is abbreviated i18n, because I and the ends are preceded by n words and have 18 characters between the first and last N.
STRUTS2 provides localization, i.e., internationalization (i18n) support through resource packs, interceptors and tag libraries in the following places:
- UI Label
- Messages and Errors
- Action class
Resource bundle:
STRUTS2 uses resource bundles to provide user multilingual and regional Options for Web applications. Don't worry about pages written in different languages. All that must be done is create a resource bundle for each desired language. The resource bundle will contain the language user for the title, message, and other text. The file for the resource bundle that contains the default language for the key/value pair for your application.
The resource files for a simple named format are:
Bundlename_language_country.properties
Here, packages can be actionclass, interfaces, superclass, model, encapsulation, global resource properties. The next part of the Language_country, en_US and so on here, can skip this is optional national part of the regional representation of Es_es and English (United States), Spanish (Spain) represents a locale's locale, for example, representing the country.
When referencing the message element, the key is to search for the corresponding message packet's struts framework in the following order:
- Actionclass.properties
- Interface.properties
- Superclass.properties
- Model.properties
- Package.properties
- Struts.properties
- Global.properties
Multi-language development application, you must maintain the corresponding to those language/locale settings for multiple property files defined in the key/value pairs of all content. For example, if you are developing an application (default), you must create three property files for U.S. English, Spanish, and French. Here, I'll use only global.properties files, and you can isolate different types of messages using different property files.
- Global.properties: By default, English (USA) will be applied
- Global_fr.properties: This will be used in a French environment.
- Global_es.properties: This will be used in the Spanish language environment.
Access message:
There are several ways to access the information resources, including gettext, text tags, UI tags, key attributes, internationalization tags. Let's take a look at them simply:
To display i18n text, use the Call property tag GetText, or any other tag, such as the UI tag as follows:
<s:property value= "GetText (' Some.key ')"/>
Text tag retrieves from the default resource bundle, that is, a message struts.properties
<s:text name= "Some.key"/>
The i18n label pushes any resource bundle on the stack. Other labels within the range of the i18n label can display messages for that resource bundle:
<s:i18n name= "Some.package.bundle" >
<s:text name= "Some.key"/>
</s:i18n>
Most UI tags have key attributes that can be used to retrieve messages from a resource bundle:
<s:textfield key= "Some.key" name= "Textfieldname"/>
Localization Example:
The index.jsp is created from the previous chapter to multiple languages. The same file will be written as follows:
<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 "pageencoding=" iso-8859-1 "%> <%@ taglib prefix=" s "uri="/struts-tags "%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
We will create the success.jsp file that will be invoked in case of the action defined by the returned success.
<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 "
pageencoding=" iso-8859-1 "%> <%@ taglib prefix="
S "uri="/struts-tags "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"
"HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
< html>
Here, we need to create two actions. (i) First action a locale and care, displaying the same index.jsp file in different languages (ii) Another action was taken to care for the submission of the form itself. The action will return success, but we will take a different action to return the value based on, because our purpose is different two actions:
Action processing locale:
Package com.yiibai.struts2;
Import Com.opensymphony.xwork2.ActionSupport;
public class Locale extends actionsupport{public
String Execute ()
{return
SUCCESS;
}
}
To submit a form processing action:
Package com.yiibai.struts2;
Import Com.opensymphony.xwork2.ActionSupport;
public class Employee extends actionsupport{
private String name;
private int age;
Public String Execute ()
{return
SUCCESS;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
public int getage () {return age
;
}
public void Setage (int age) {
this.age = age;
}
}
Right now. Let's create the following three global.properties files in classpath:
GLOBAL. PROPERTIES:
global.name = name
Global.age = Age
Global.submit = submit
global.heading = Select locale< c5/>global.success = successfully authenticated
global_fr. PROPERTIES:
global.name = Nom d ' utilisateur
global.age = L ' âge
global.submit = Soumettre des
global.heading = Sélectionnez local
global.success = Authentifiéavec succès global_es
. PROPERTIES:
global.name = Nombre de usuario
global.age = edad global.submit
=
Presentar global.heading = Seleccionar la configuracion regional
global.success = Autenticado correctamente
We will create the following two actions in the Struts.xml:
<?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 "/>
<constant name=" struts.custom.i18n.resources "value=" global "/> <package name="
HelloWorld " extends= "Struts-default" namespace= "/" >
<action name= "Empinfo" class= "Com.yiibai.struts2.Employee"
method= "Execute" >
<result name= "input" >/index.jsp</result>
<result name= " Success ">/success.jsp</result>
</action>
<action name=" locale "
class=" Com.yiibai.struts2.Locale "
method=" "Execute" >
<result name= "Success" >/index.jsp</result >
</action>
</package>
</struts>
The following are the contents of the Web.xml file:
<?xml version= "1.0" encoding= "UTF-8"?> <web-app "xmlns:xsi=" /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>struts 2</display-name> <welcome-fil e-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <fil Ter-name>struts2</filter-name> <filter-class> Org.apache.struts2.dispatcher.FilterDispatcher ;/filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <ur l-pattern>/*</url-pattern> </filter-mapping> </web-app>
Now, right click on the project name and click Export > War file to create a war document. This war is then deployed under Tomcat's WebApps directory. Finally, start the Tomcat server and try to access the URL http://localhost:8080/HelloWorldStruts2/index.jsp. This will give you the following picture:
Now choose any language, let's say, we choose Spanish, which will show the following results:
You can try the usage language. Finally, let's try clicking the "Submit" button when we are in the Spanish language and it will show the following screen:
Congratulations, there is now a multilingual web page that can launch your site worldwide.