Spring MVC靜態化解決方案(一)
阿新 • • 發佈:2019-02-08
http://fumingfu1990-gmail-com.iteye.com/blog/1541740
前段時間研究了下Spring MVC靜態化,今天整理了一下,附上實現方法。
(本文只介紹靜態化,nginx對映以及靜態化更新機制後續介紹)
實現方法:
1、對Spring MVC預設的檢視進行擴充套件,複寫FreeMarkerView,新增自己想要的邏輯。(判斷需要將請求後的response資訊落地)
- public class MyFreeMarkerView extends FreeMarkerView{
- @Override
- protected void doRender(Map model,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- exposeModelAsRequestAttributes(model, request);
- SimpleHash fmModel = buildTemplateModel(model, request, response);
- Locale locale = RequestContextUtils.getLocale(request);
- /*
- * 預設不生成靜態檔案,除非在Action中進行如下設定
- * model.addAttribute("STATIC_PAGE", true);
- */
- if(model.get("STATIC_PAGE") == null || Boolean.FALSE.equals(model.get("STATIC_PAGE"))){
- processTemplate(getTemplate(locale), fmModel, response);
- }else{
- createHTML(getTemplate(locale), fmModel, request, response);
- }
- }
- public void createHTML(Template template, SimpleHash model,HttpServletRequest request,
- HttpServletResponse response) throws IOException, TemplateException, ServletException {
- // 靜態檔案根目錄的絕對路徑
- ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(request
- .getSession().getServletContext());
- PropsUtil configHelper = (PropsUtil) context.getBean("configHelper");
- String basePath = configHelper.getProperty("static_html_path");
- // String basePath =
- // "D:\\Program Files\\Apache Software Foundation\\Tomcat 7.0\\webapps\\ROOT\\static\\";
- // 訪問的URL(根目錄以後,如xxx/113.html)
- String requestHTML = this.getRequestHTML(request);
- // 靜態頁面儲存的絕對路徑
- String htmlPath = basePath + requestHTML;
- // response路徑
- String responsePath = "/" + requestHTML;
- File htmlFile = new File(htmlPath);
- if (!htmlFile.getParentFile().exists()) {
- htmlFile.getParentFile().mkdirs();
- }
- if (!htmlFile.exists()) {
- htmlFile.createNewFile();
- }
- Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile),
- "UTF-8"));
- // 處理模版
- template.process(model, out);
- out.flush();
- out.close();
- request.getRequestDispatcher(responsePath).forward(request, response);
- }
- /**
- * 獲取要生成的靜態檔案相對路徑
- *
- * @param request HttpServletRequest
- * @return /目錄/*.html
- */
- private String getRequestHTML(HttpServletRequest request) {
- // web應用名稱,部署在ROOT目錄時為空
- String contextPath = request.getContextPath();
- // web應用/目錄/檔案,如/xxxx/1
- String requestURI = request.getRequestURI();
- // basePath裡面已經有了web應用名稱,所以直接把它replace掉,以免重複
- requestURI = requestURI.replaceFirst(contextPath, "");
- // 得到引數
- Enumeration<?> pNames = request.getParameterNames();
- while (pNames.hasMoreElements()) {
- String name = (String) pNames.nextElement();
- String value = request.getParameter(name);
- requestURI = requestURI + "_" + name + "=" + value;
- }
- // 加上.html字尾
- requestURI = requestURI + ".html";
- return requestURI;
- }
- }
2、修改web-servlet.xml
,將預設的freemarker檢視改成上面複寫FreeMarkerView 的MyFreeMarkerView
Xml程式碼
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
- <property name="viewClass" value="xxx.xxx.xxxx.util.freemarker.MyFreeMarkerView" />
- </bean>
3、在action中,進行一下設定,就可以輸入靜態html
Java程式碼
- model.addAttribute("STATIC_PAG", true);