五步实现企业换肤
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
做事之前先理清思路,自己需要實(shí)現(xiàn)怎樣的功能,有哪幾種實(shí)現(xiàn)方案,寫(xiě)例子查看幾種方案的實(shí)現(xiàn)效果。擇優(yōu)選取最佳方案。
1、設(shè)想
? 我的設(shè)想是三種方式:
? ?三種方式的優(yōu)缺點(diǎn):設(shè)想一的優(yōu)點(diǎn):不需要企業(yè)提供域名,不需要配置域名參數(shù),一個(gè)站點(diǎn)ICP備案即可,缺點(diǎn):后綴面的方式不能讓企業(yè)用戶(hù)高度辨識(shí)企業(yè)站點(diǎn),企業(yè)用戶(hù)對(duì)站點(diǎn)的信任度較低,動(dòng)態(tài)后綴配置繁瑣。設(shè)想二的優(yōu)點(diǎn):各企業(yè)用戶(hù)對(duì)站點(diǎn)的辨識(shí)度高,信任度高,配置一個(gè)域名對(duì)應(yīng)一個(gè)企業(yè)來(lái)實(shí)現(xiàn)動(dòng)態(tài)css和js配置,簡(jiǎn)單方便。缺點(diǎn):需要企業(yè)提供域名,ICP備案信息多樣。設(shè)想三優(yōu)點(diǎn):以企業(yè)名作為地址實(shí)現(xiàn)方便,配置少。缺點(diǎn)同設(shè)想一。
2、實(shí)現(xiàn)
? ? 經(jīng)過(guò)項(xiàng)目組篩選方案,選擇方案二,以域名來(lái)辨識(shí)企業(yè),一來(lái)多個(gè)域名可以對(duì)應(yīng)一個(gè)IP,二來(lái)有利于推廣,三來(lái)用戶(hù)信任度高。
? 篩選過(guò)后實(shí)現(xiàn)就是技術(shù)問(wèn)題了,我看了springmvc的動(dòng)態(tài)css加載有一個(gè)類(lèi),ResourceBundleThemeSource類(lèi)可以實(shí)現(xiàn)動(dòng)態(tài)加載css,完全滿(mǎn)足我們的不同企業(yè)的換膚需求,使用方便。
? ? ? 第一步:配置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文件夾,存放不同站點(diǎn)的對(duì)應(yīng)的css文件配置。
文件內(nèi)容為:
#ThemeSource配置code <link rel="stylesheet" type="text/css" href="<spring:theme code='login_style'/>" /> login_style=css/github/login_style.css? ? ?第三步:配置站點(diǎn)對(duì)應(yīng)的企業(yè)名:通過(guò)企業(yè)名查找對(duì)應(yīng)的properties加載指向的css
github.oschina.com=github? ? ?為了方便讀取properties文件,寫(xiě)個(gè)工具類(lèi)來(lái)獲取資源文件的配置,需要在spring配置文件配置此類(lèi)。
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判斷域名并在頁(yè)面加載動(dòng)態(tài)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"; }? ? ? 第五步:也就是最后一步,實(shí)現(xiàn)頁(yè)面的動(dòng)態(tài)加載css,先要有對(duì)應(yīng)的css文件?
? ? ??
? ? ? ?頁(yè)面代碼:
<%@ 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'/>" />? ? ? 驗(yàn)證后,完美實(shí)現(xiàn)了所要求的換膚需求。特此記錄
轉(zhuǎn)載于:https://my.oschina.net/githubhty/blog/916337
總結(jié)
- 上一篇: CentOS 7 防火墙开启了哪些服务和
- 下一篇: JavaScript Select和Op