Tiles-config.xml
<definition name=".coreLayout" path="/WEB-INF/pages/layouts/core/coreLayout.jsp">
<put name="titleKey"/>
<put name="content" />
<putList name="leftLayoutBlocksList">
<add value=".loginForm" />//左侧的登陆页面瓦片标志
<add value=".core.menu" />//左侧的菜单瓦片标志
</putList>
<putList name="rightLayoutBlocksList">
<add value=".searchForm" />//右侧全文搜索瓦片标志
<add value=".switchLocaleForm" />//右侧多国语言支持瓦片标志
<add value=".editModeForm" />//右侧的可编辑模式切换瓦片标志
</putList>
。。。。
<!-- Switch locale form definition -->
<definition name=".switchLocaleForm" extends=".blockLayout"
//此处的name对应于以上的<add value=".switchLocaleForm" />
controllerClass="com.blandware.atleap.webapp.action.core.contentLocale.LocaleListController">
//此处的class是用于处理switchLocaleForm所代表的页面请求的
<put name="blockBody" value="/WEB-INF/pages/core/contentLocale/switchLocaleForm.jsp" />
// switchLocaleForm真正所代表的JSP页面
</definition>
此时,我们知道了页面中语言切换所对应页面是:
/WEB-INF/pages/core/contentLocale/switchLocaleForm.jsp
另外,该页面对应的处理页面是:
"com.blandware.atleap.webapp.action.core.contentLocale.LocaleListController"
Ø switchLocaleForm.jsp定义
<%@ include file="/WEB-INF/pages/common/taglibs.jsp"%>
<atleap:constants className="org.apache.struts.Globals" var="LOCALE_KEY" scope="page" />
<atleap:constants className="com.blandware.atleap.webapp.struts.ContentTilesRequestProcessor" var="PROCESSED_URL" />
<atleap:constants className="com.blandware.atleap.webapp.util.core.WebappConstants" var="LANGUAGES_COLLECTION_KEY" scope="page" />
<div id="switchLocaleForm" align="center">
<html:form action="switchLocale" method="POST">
<input type="hidden" name="redirectUrl" value="${requestScope[pageScope.PROCESSED_URL]}" />
<span><atleap:label property="localeIdentifier"/></span><br />
<span>
<html:select property="localeIdentifier" value="${sessionScope[pageScope.LOCALE_KEY].language}">
<html:options collection="${pageScope.LANGUAGES_COLLECTION_KEY}" property="value" labelProperty="label" />
</html:select>
</span>
<div align="center">
<html:submit styleClass="button"><bean:message key="button.change" /></html:submit>
</div>
</html:form>
</div>
Ø LocaleListController.java定义
public final class LocaleListController extends ControllerSupport {
public void execute(ComponentContext tilesContext, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext)
throws Exception {
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
LookupManager lookupManager = (LookupManager) ctx.getBean(Constants.LOOKUP_MANAGER_BEAN);
List localesList = lookupManager.listActiveContentLocales();
request.setAttribute(WebappConstants.LANGUAGES_COLLECTION_KEY, localesList);
}
}
其中在苏丹放松
<html:form action="switchLocale" method="POST">
switchLocale可以到structs-config.Xml中找到相关配置
<action
path="/switchLocale"
type="com.blandware.atleap.webapp.action.core.contentLocale.SwitchLocaleAction"
name="switchLocaleForm"
scope="request"
unknown="false"
validate="false"
>
path="/switchLocale"所影射到的action 和 actionForm 是SwitchLocaleAction和switchLocaleForm。
SwitchLocaleForm。Java
public class SwitchLocaleForm extends BaseForm {
protected String localeIdentifier;
protected String redirectUrl;
……
public void reset(ActionMapping mapping, HttpServletRequest request) {
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
return errors;
}
}
SwitchLocaleAction。Java
public final class SwitchLocaleAction extends BaseAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
SwitchLocaleForm switchLocaleForm = (SwitchLocaleForm) form;
Locale newLocale = new Locale(switchLocaleForm.getLocaleIdentifier());
String redirectUrl = switchLocaleForm.getRedirectUrl();
request.getSession().setAttribute(Globals.LOCALE_KEY, newLocale);
ActionForward redirect = new ActionForward();
redirect.setRedirect(true);
redirect.setPath(redirectUrl);
return redirect;
}
}
到此为止,我们发现在这个action中,只做了将newLocal放到Session中,另外,redirect到自身去了,那么我们到底如何实现跳转页面采用用户选择的语言呢。
在atleap中,采用filter来截获每个request和response,通过配置文件web.xml中,
<filter-name>actionFilter</filter-name>
<filter-class>com.blandware.atleap.webapp.filter.ActionFilter</filter-class>
<init-param>
<param-name>isSecure</param-name>
<param-value>false</param-value>
</init-param>
</filter>
其源码是:
public class ActionFilter implements Filter {
private static Boolean secure = Boolean.FALSE;
private final transient Log log = LogFactory.getLog(ActionFilter.class);
private FilterConfig config = null;
………
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain)
throws IOException, ServletException {
// cast to the types I want to use
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
HttpSession session = request.getSession(true);
// notify the LocaleContextHolder what locale is being used so
// service and data layer classes can get the locale
LocaleContextHolder.setLocale(request.getLocale());
……
}
}
这样我们通过使用filter来处理各个请求和相应,从中设置国家语言,以方便service 和 Dao 层类调用。