Struts - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Struts 2 Template

Struts 2 Template

shape Description

Struts 2 Template act as MVC (model and view controller). Configuration of all the information about views and controllers is done in Struts.XML file. One can modify any data without changing the entire application by changing the XML file. As a general all know that template means that effective assert to the users for example while using any public web site user will add the template to look better likewise in every technology may have have predefined templates. To design graphical user interface applications Java provides the strategy of layout managers, components and containers. Localization concept is the best example to go with templates. Struts 2 Template with Internalization or Localization means, it is the “process of planning and implementing products and services. So that, they can be converted to any languages and any culture” and the process known as  localization.

shape Example

Let's take one example using localization, then one need to define the name as request_locale type. 1. index.jsp [java] <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title><s:property value="getText('global.form')" /> - Struts2 Demo |splesson.com</title> </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:a href="%{localeEN}" >English</s:a> <s:a href="%{localeHN}" >Hindi</s:a> </body> </html> [/java] One can use the s:textfield to create a HTML input textbox.The URL tag is used to create an URL and output it as a text format. It’s never work by itself, but it can provides URL to other tags like to create a hyperlink or to render an image. success.jsp [java]<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title><s:property value="getText('global.form')" /> - Struts2 Demo | splesson.com</title> </head> <body> <h2><s:property value="getText('global.success')" />.</h2> </body> </html> [/java] The getText function is used to get the data from other resources. 2. To Create the properties files. global.properties [java]global.name = Name global.age = Age global.telephone = Telephone global.email = Email global.submit = Submit global.success = Employee Successfully Added global.form = Employee Form [/java] Configure the global resource bundle in struts.properties file, here you defined a properties file named global.properties as the global resource bundle. global_hn.properties [java]global.name = \u0928\u093E\u092E global.age = \u0909\u092E\u094D\u0930 global.telephone = \u091F\u0947\u0932\u0940\u092B\u094B\u0928 global.email = \u0908\u092E\u0947\u0932 global.submit = \u092A\u094D\u0930\u0938\u094D\u0924\u0941\u0924 \u0915\u0930\u0928\u093E global.success = \u0915\u0930\u094D\u092E\u091A\u093E\u0930\u0940 \u0938\u092B\u0932\u0924\u093E\u092A\u0942\u0930\u094D\u0935\u0915 \u091C\u094B\u0921\u093C\u093E \u0917\u092F\u093E global.form = \u0915\u0930\u094D\u092E\u091A\u093E\u0930\u0940 \u092A\u0930\u094D\u091A\u093E [/java] The global_hn.properties will be used for Hindi locale. 3. Create the Action class, in this example it is created with the two Action classes. One Action class performs the localization operation and another class performs the submit operation, but both action classes returns success. EmployeeAction.java [java]package com.splessons; import com.opensymphony.xwork2.ActionSupport; public class EmployeeAction extends ActionSupport { private String name,email; private int age,telephone; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getTelephone() { return telephone; } public void setTelephone(int telephone) { this.telephone = telephone; } public String execute() { return "success"; } } [/java] EmployeeLocalAction.java [java]package com.splessons.local; import com.opensymphony.xwork2.ActionSupport; public class EmployeeLocalAction extends ActionSupport { public String execute() { return "success"; } } [/java] Struts 2 comes with an optional action interface (com.opensymphony.xwork2.Action). By implements this interface, it bring some convenient benefits. 4. Create the xml files. struts.xml [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.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <constant name="struts.custom.i18n.resources" value="global" /> <package name="default" extends="struts-default" namespace="/"> <action name="employee" class="com.splessons.EmployeeAction"> <result name="success">/success.jsp</result> <result name="input">/index.jsp</result> </action> <action name="locale" class="com.splessons.local.EmployeeLocalAction"> <result name="success">/employee.jsp</result> </action> </package> </struts> [/xml] Where success is a predefined result type. action element is the sub component of package. It speaks to an activity to be conjured for the approaching request. It has name, class and method attributes. In the event that you don’t determine name property as a matter of course execute() technique will be summoned for the predetermined actiion class. web.xml [xml] <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app></pre> [/xml] Output When compile the program following output will be displayed. When user click on the submit button alert message will be as follows depends on the selected language.

Summary

shape Key Points

  • Struts 2 Template - Internalization interceptor provides multi-lingual support for an application.
  • Struts 2 Template - Internalization can be supported as an interceptor also.