1. Add displaytag-1.1.1.jar in your libraries and classpath of your project.
2. Add displaytag-11.tld under /WEB-INF directory.
3. Add a java file I18nStruts2Adapter.java under any package (i.e. com.shaan.common)
package com.shaan.common;
import java.util.Iterator;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.views.jsp.TagUtils;
import org.displaytag.Messages;
import org.displaytag.localization.I18nResourceProvider;
import org.displaytag.localization.LocaleResolver;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.util.ValueStack;
// Struts2 implementation of a resource provider and locale resolver
public class I18nStruts2Adapter implements LocaleResolver,
I18nResourceProvider {
// prefix/suffix for missing entries.
public static final String UNDEFINED_KEY = "???"; //$NON-NLS-1$
// logger
private static Log log =
LogFactory.getLog(I18nStruts2Adapter.class);
public Locale resolveLocale(HttpServletRequest request) {
Locale result = null;
ValueStack stack = ActionContext.getContext().getValueStack();
Iterator iterator = stack.getRoot().iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
if (o instanceof LocaleProvider) {
LocaleProvider lp = (LocaleProvider) o;
result = lp.getLocale();
break;
}
}
if (result == null) {
log.debug("Missing LocalProvider actions, init locale to default");
result = Locale.getDefault();
}
return result;
}
public String getResource(String resourceKey, String defaultValue,
Tag tag, PageContext pageContext) {
// if resourceKey isn't defined either, use defaultValue
String key = (resourceKey != null) ? resourceKey : defaultValue;
String message = null;
ValueStack stack = TagUtils.getStack(pageContext);
Iterator iterator = stack.getRoot().iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
if (o instanceof TextProvider) {
TextProvider tp = (TextProvider) o;
message = tp.getText(key);
break;
}
}
// if user explicitely added a titleKey
if (message == null && resourceKey != null) {
log.debug(Messages.getString("Localization.missingkey",
resourceKey)); // $NON-NLS-1$
message = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY;
}
return message;
}
}
4. Add displaytag_en.properties under your source folder directly without any package.
You should define your I18nStruts2Adapter Java file as the locale resolver
and provider in this file.
# Display tag library 1.1.1
# The display tag library is an open source suite of custom tags that provide high-level web
# presentation patterns which will work in an MVC model.
# The library provides a significant amount of functionality while still being easy to use.
sort.behavior=list
sort.amount=list
basic.msg.empty_list=<div class="textError">There is no result in your search</div><ul><li CLASS="triangleOrange"><a href="javascript:history.go(-1)" mce_href="javascript:history.go(-1)" mce_href="javascript:history.go(-1)" class="smallLinkOrangeBold">New search</a></li></ul>
paging.banner.no_items_found=<div class="textError">There is no result in your search.</div><ul><li CLASS="triangleOrange"><a href="javascript:history.go(-1)" mce_href="javascript:history.go(-1)" mce_href="javascript:history.go(-1)" class="smallLinkOrangeBold">New search</a></li></ul>
paging.banner.all_items_found=<table width="100%"><tr><td><div class='note'>Result: {0} answer(s)</div></td>
paging.banner.some_items_found=<table width="100%"><tr><td><div class='note'>Result: {0} answer(s)</div></td>
paging.banner.full=<td align='right'><span class="note" >pages : <a href="{2}" mce_href="{2}" mce_href="{2}" class="smallLink">[previous]</a> {0} <a href="{3}" mce_href="{3}" mce_href="{3}" class="smallLink">[next]</a></span></td></tr></table>
paging.banner.first=<td align='right'><span class="note ">pages : {0} <a href="{3}" mce_href="{3}" mce_href="{3}" class="smallLink">[next]</a> </span></td></tr></table>
paging.banner.last=<td align='right'><span class="note" >pages : <a href="{2}" mce_href="{2}" mce_href="{2}" class="smallLink">[previous]</a> {0}</span></td></tr></table>
paging.banner.onepage=
paging.banner.one_item_found=
paging.banner.page.selected=<span class="textBoldOrange">{0}</span>
paging.banner.page.link=<a href="{1}" mce_href="{1}" mce_href="{1}" title="Go to the page {0}" CLASS="inkDeco">{0}</a>
paging.banner.page.separator=
paging.banner.group_size=10
# ====================== StyleSheet CSS
css.tr.even=rowEven
css.tr.odd=rowOdd
css.tr.even=rowEven
css.tr.odd=rowOdd
css.table=osuit-Table
# ====================== I18 resolver and provider
locale.resolver=com.shaan.common.I18nStruts2Adapter
locale.provider=com.shaan.common.I18nStruts2Adapter
|
5. You can add other locale specific properties files to have messages in different langugages.
For example : displaytag_fr.properties
6. Make data structures in Struts 2 Actions and use display tag in your JSP
Action class
ArrayList myList;
public ArrayList getMyList() {
return myList;
}
public void setMyList(ArrayList myList) {
this.myList = myList;
}
public String input() {
myList = new ArrayList();
HashMap map = new HashMap();
map.put("number", "2");
map.put("description", "hello");
map.put("standard", "A1");
myList.add(map);
map = new HashMap();
map.put("number", "5");
map.put("description", "bye");
map.put("standard", "A2");
myList.add(map);
map = new HashMap();
map.put("number", "10");
map.put("description", "shree");
map.put("standard", "B2");
myList.add(map);
return "input";
}
JSP page
<!-- Add display tag TLD -->
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<display:table name="myList" pagesize="2" uid="row" requestURI=""
sort="list" class="osuit-Table">
<display:column property="number" headerClass="osuit-Headers"
sortable="true" />
<display:column property="description" headerClass="osuit-Headers" />
<display:column property="standard" headerClass="osuit-Headers" />
</display:table>
|
No comments:
Post a Comment
Note: only a member of this blog may post a comment.