五步实现企业换肤
2019獨角獸企業重金招聘Python工程師標準>>>
做事之前先理清思路,自己需要實現怎樣的功能,有哪幾種實現方案,寫例子查看幾種方案的實現效果。擇優選取最佳方案。
1、設想
? 我的設想是三種方式:
? ?三種方式的優缺點:設想一的優點:不需要企業提供域名,不需要配置域名參數,一個站點ICP備案即可,缺點:后綴面的方式不能讓企業用戶高度辨識企業站點,企業用戶對站點的信任度較低,動態后綴配置繁瑣。設想二的優點:各企業用戶對站點的辨識度高,信任度高,配置一個域名對應一個企業來實現動態css和js配置,簡單方便。缺點:需要企業提供域名,ICP備案信息多樣。設想三優點:以企業名作為地址實現方便,配置少。缺點同設想一。
2、實現
? ? 經過項目組篩選方案,選擇方案二,以域名來辨識企業,一來多個域名可以對應一個IP,二來有利于推廣,三來用戶信任度高。
? 篩選過后實現就是技術問題了,我看了springmvc的動態css加載有一個類,ResourceBundleThemeSource類可以實現動態加載css,完全滿足我們的不同企業的換膚需求,使用方便。
? ? ? 第一步:配置spring.xml文件,加上:
<!-- 樣式--> <bean class="org.springframework.ui.context.support.ResourceBundleThemeSource" id="themeSource"> <property name="basenamePrefix" value="theme."></property> </bean> <bean id="themeResolver" class="org.springframework.web.servlet.theme.SessionThemeResolver"> <property name="defaultThemeName" value="github" /> </bean>? ? ?第二步:增加theme配置文件,在resource文件中增加theme文件夾,存放不同站點的對應的css文件配置。
文件內容為:
#ThemeSource配置code <link rel="stylesheet" type="text/css" href="<spring:theme code='login_style'/>" /> login_style=css/github/login_style.css? ? ?第三步:配置站點對應的企業名:通過企業名查找對應的properties加載指向的css
github.oschina.com=github? ? ?為了方便讀取properties文件,寫個工具類來獲取資源文件的配置,需要在spring配置文件配置此類。
public class PropertyPlaceholder extends PropertyPlaceholderConfigurer {private static Map<String,String> propertyMap;@Overrideprotected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {super.processProperties(beanFactoryToProcess, props);propertyMap = new HashMap<String, String>();for (Object key : props.keySet()) {String keyStr = key.toString();String value = props.getProperty(keyStr);propertyMap.put(keyStr, value);}}//static method for accessing context propertiespublic static String getProperty(String name) {return propertyMap.get(name);} }? ? ?第四步:controller判斷域名并在頁面加載動態css,代碼如下:
@Autowiredprivate ThemeResolver themeResolver;@RequestMapping(value = "/changeTheme", method = RequestMethod.GET)public String theme(HttpServletRequest request,HttpServletResponse response, String bankName) {String domain = request.getServerName(); logger.info("domain:"+domain);String domainName = PropertyPlaceholder.getProperty(domain);logger.info("domainName :"+domainName );logger.info("current theme is "+ themeResolver.resolveThemeName(request));themeResolver.setThemeName(request, response, domainName);logger.info("current theme change to "+ themeResolver.resolveThemeName(request));model.addAttribute("domainName ", domainName );return "theme"; }? ? ? 第五步:也就是最后一步,實現頁面的動態加載css,先要有對應的css文件?
? ? ??
? ? ? ?頁面代碼:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> <link rel="stylesheet" type="text/css" href="<spring:theme code='login_style'/>" />? ? ? 驗證后,完美實現了所要求的換膚需求。特此記錄
轉載于:https://my.oschina.net/githubhty/blog/916337
總結
- 上一篇: CentOS 7 防火墙开启了哪些服务和
- 下一篇: 【BZOJ】3779 重组病毒