?? localestatemanagerimpl.java
字號:
// Copyright 2005-2007 onetsoft.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.onetsoft.fastjsp;
import com.onetsoft.fastjsp.util.CookieUtils;
import com.onetsoft.fastjsp.util.LocaleUtils;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Locale;
/**
* Locale管理
*
* @author <a href="mailto:hgw@onetsoft.com">hgw</a>
*/
public class LocaleStateManagerImpl implements LocaleStateManager {
private static String ATTRIBUTE_NAME = "fastjsp_locale";
private static int MAX_COOKIE_AGE = 60 * 60 * 24 * 3;
private Locale locale = null;
private boolean changed = false;
public LocaleStateManagerImpl() {
}
public Locale getLocale() {
return locale;
}
public void setLocale(Locale locale) {
if (locale == null) throw new NullPointerException("Null parameter \"locale\".");
if (!this.locale.equals(locale)) {
changed = true;
this.locale = locale;
}
}
public void init(PageCycle cycle) {
HttpSession s = cycle.getRequest().getSession(false);
if (s != null)
locale = (Locale) s.getAttribute(ATTRIBUTE_NAME);
if (locale == null) {
locale = loadFromCookie(cycle.getRequest());
if (locale == null) {
locale = ((AbstractPageCycle) cycle).service.servicer.module.locale;
saveCookie(cycle.getRequest(), cycle.getResponse());
}
if (s != null)
saveSession(cycle.getRequest());
}
}
public void commitChange(PageCycle cycle) {
if (changed) {
saveSession(cycle.getRequest());
saveCookie(cycle.getRequest(), cycle.getResponse());
changed = false;
}
}
private Locale loadFromCookie(HttpServletRequest request) {
Cookie cookie = CookieUtils.getCookie(request, ATTRIBUTE_NAME);
if (cookie == null)
return null;
return LocaleUtils.parseLocale(cookie.getValue(), null);
}
private void saveSession(HttpServletRequest request) {
HttpSession s = request.getSession(false);
if (s != null)
s.setAttribute(ATTRIBUTE_NAME, locale);
}
private void saveCookie(HttpServletRequest request, HttpServletResponse response) {
CookieUtils.deleteCookie(request, response, CookieUtils.getCookie(request, ATTRIBUTE_NAME));
CookieUtils.setCookie(request, response, ATTRIBUTE_NAME, locale.toString(), MAX_COOKIE_AGE);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -