有两种方法来实现该功能:间接法、直接法
第一种:间接法,使用el表达式与struts2的tag来写,这种方法比较繁琐,比如:
<s:set name="username" value="getText('user.username')" scope="page" />
<ec:table items="users" var="user" tableId="user" form="userForm"
view="multiCompact"
action="${ctx}/system/listUser.action" locale="${i18n_locale}">
<ec:row>
<ec:column property="username" title="${username}" />
</ec:row>
</ec:table>
第二种:直接法,该方法是利用ec提供的扩展点来实现,这主要得益于ec的良好设计,该方法有两个步骤:
1.定义个实现org.extremecomponents.table.core.Messages结构的类
import
java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.servlet.jsp.PageContext;
import org.apache.struts2.views.jsp.TagUtils;
import org.extremecomponents.table.context.Context;
import org.extremecomponents.table.core.Messages;
import org.extremecomponents.table.resource.TableResourceBundle;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.util.ValueStack;
public class MessagesStruts2Adapter implements Messages ... {
private TextProvider textProvider = null;
private Messages defaultMessages;
public MessagesStruts2Adapter() ...{
super();
this.defaultMessages = new TableResourceBundle();
}
public void init(Context context, Locale locale) ...{
// 先读取extremecomponents自带的国际化文件
this.defaultMessages.init(context, locale);
if (context.getContextObject() instanceof PageContext) ...{
PageContext pageContext = (PageContext) context.getContextObject();
ValueStack stack = TagUtils.getStack(pageContext);
for (Object o : stack.getRoot()) ...{
if (o instanceof TextProvider) ...{
this.textProvider = (TextProvider) o;
break;
}
}
}
}
public String getMessage(String code) ...{
return getMessage(code, null);
}
public String getMessage(String code, Object[] args) ...{
// 优先查找struts2的配置文件,然后再查找ec自带的配置文件
List<Object> theArgs = null;
if (args != null) ...{
theArgs = new ArrayList<Object>();
for (Object arg : args) ...{
theArgs.add(arg);
}
}
String message = null;
if (this.textProvider != null) ...{
message = this.textProvider.getText(code, null, theArgs);
}
if (message == null) ...{
message = defaultMessages.getMessage(code, args);
}
return message;
}
}
import java.util.List;
import java.util.Locale;
import javax.servlet.jsp.PageContext;
import org.apache.struts2.views.jsp.TagUtils;
import org.extremecomponents.table.context.Context;
import org.extremecomponents.table.core.Messages;
import org.extremecomponents.table.resource.TableResourceBundle;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.util.ValueStack;
public class MessagesStruts2Adapter implements Messages ... {
private TextProvider textProvider = null;
private Messages defaultMessages;
public MessagesStruts2Adapter() ...{
super();
this.defaultMessages = new TableResourceBundle();
}
public void init(Context context, Locale locale) ...{
// 先读取extremecomponents自带的国际化文件
this.defaultMessages.init(context, locale);
if (context.getContextObject() instanceof PageContext) ...{
PageContext pageContext = (PageContext) context.getContextObject();
ValueStack stack = TagUtils.getStack(pageContext);
for (Object o : stack.getRoot()) ...{
if (o instanceof TextProvider) ...{
this.textProvider = (TextProvider) o;
break;
}
}
}
}
public String getMessage(String code) ...{
return getMessage(code, null);
}
public String getMessage(String code, Object[] args) ...{
// 优先查找struts2的配置文件,然后再查找ec自带的配置文件
List<Object> theArgs = null;
if (args != null) ...{
theArgs = new ArrayList<Object>();
for (Object arg : args) ...{
theArgs.add(arg);
}
}
String message = null;
if (this.textProvider != null) ...{
message = this.textProvider.getText(code, null, theArgs);
}
if (message == null) ...{
message = defaultMessages.getMessage(code, args);
}
return message;
}
}
2.在extremecomponents的配置文件中配置该实现类