使用cookie保存皮肤

xiaoxiao2022-12-02  180

下面是使用cookie保存皮肤的代码:

backbean:

@Scope(SESSION)@Name("skinBean")public class SkinBean {

 private String skin = "blueSky";

  public String getSkin() {        HttpServletRequest httpServletRequest = (HttpServletRequest) FacesContext    .getCurrentInstance().getExternalContext().getRequest();  Cookie[] cookies = httpServletRequest.getCookies();  if (cookies != null) {   for (int i = 0; i < cookies.length; i++) {    if (cookies[i].getName().equalsIgnoreCase("skin")) {     this.skin = cookies[i].getValue();

break;//只取得最后选择的皮肤    }   }  }   return skin; }

 

 

 public void setSkin(String skin) {  this.skin = skin;    HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext    .getCurrentInstance().getExternalContext().getResponse();  Cookie cookie = new Cookie("skin", skin);  cookie.setMaxAge(365);  cookie.setComment("Richfaces skin");  httpServletResponse.addCookie(cookie);  } public void skinChangeListener(ActionEvent event){  //HtmlSelectOneMenu ss = (HtmlSelectOneMenu) event.getComponent().findComponent("selectSkin");  //String selected=ss.getValue().toString();  //this.skin=selected;  }

 

 

skin.xhtml

 

 

 

<h:selectOneMenu id="selectSkin" value="#{skinBean.skin}">   <f:selectItem itemLabel="Wfw" itemValue="WfwSkin" />   <f:selectItem itemLabel="Plain" itemValue="plain" />   <f:selectItem itemLabel="EmeraldTown" itemValue="emeraldTown" />   <f:selectItem itemLabel="BlueSky" itemValue="blueSky" />   <f:selectItem itemLabel="Wine" itemValue="wine" />   <f:selectItem itemLabel="JapanCherry" itemValue="japanCherry" />   <f:selectItem itemLabel="Ruby" itemValue="ruby" />   <f:selectItem itemLabel="Classic" itemValue="classic" />   <f:selectItem itemLabel="DeepMarine" itemValue="deepMarine" />   <a4j:support event="onchange" limitToList="true" actionListener="#{skinBean.skinChangeListener}" oncomplete="

window.location.href=window.location.href;

//window.location.reload(true);"    reRender="selectSkin" ajaxSingle="true">   </a4j:support>  </h:selectOneMenu> 

 

注意:因为request和response不同步的原因,所以必须让页面刷新一次

相关资源:Ajax无刷新改变网页模板(风格) 保存cookie.rar
转载请注明原文地址: https://www.6miu.com/read-4979242.html

最新回复(0)