需求:
web内容系统,需要静态化页面,管理系统仍然基于InternalResourceViewResolver渲染jsp页面,而非采用FreeMarkerViewResolver。也就是说只利用freemarker静态化页面
环境:
<!-- apache common start --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <!-- apache common end --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-lgpl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.36</version> </dependency> <!-- dao start --> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.5</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>20030825.184428</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.28</version> </dependency> <!-- dao end --> <!-- FreeMarker start --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency>
web.xml的配置:
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-*.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.util.IntrospectorCleanupListener </listener-class> </listener> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
spring-ui.xml配置:
<mvc:resources mapping="/static/**" location="/static/" /> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> <bean class="org.springframework.http.converter.ResourceHttpMessageConverter" /> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <context:component-scan base-package="com.someabcd.csr" /> <bean id="propertyConfigure" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" /> <!--配置Freemarker --> <bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/ftl/" /> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">10</prop> <prop key="defaultEncoding">UTF-8</prop> </props> </property> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
此时freemarkerConfigurer已经指定,并指定了templateLoaderPath,同时指定了编码
/WEB-INF/ftl/file.ftl:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>${article.title}</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <h2>${article.title}</h2> <hr/> <pre> ${article.content} </pre> </body> </html>
我们构建vo类:
public class Article implements Serializable{ private static final long serialVersionUID = 554206256994693476L; private String title; private String content; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
构建实际生成html的操作类:HtmlService.java 为测试类
@Service("com.someabcd.csr.service.htmlService") public class HtmlService { @Autowired private FreeMarkerConfigurer freeMarkerConfigurer; public void all(String msg) throws Exception { Map<String, Article> rootMap = new HashMap<String, Article>(); process("file.ftl", rootMap); } public void process(String templateName, Map<String, Article> rootMap) throws Exception { Configuration configuration = freeMarkerConfigurer.getConfiguration(); Template template = configuration.getTemplate(templateName); File file = new File("d:/file.html"); Article article = new Article(); article.setTitle("关于小网客"); article.setContent("解决方案咨询<br>大数据处理<br>系统架构<br>企业信息化咨询<br>Email:[email protected]<br>来自北京"); rootMap.put("article", article); Writer out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); template.process(rootMap, out); IOUtils.closeQuietly(out); } }
构建控制层的访问:HtmlController.java
@Controller("com.someabcd.csr.controller.htmlController") @RequestMapping("html") public class HtmlController { @Autowired private HtmlService htmlService; @RequestMapping(value = "/create") @ResponseBody public ReturnBase all(HttpServletRequest request, HttpServletResponse response) { ReturnBase returnStatus = new ReturnBase(); String msg = request.getParameter("msg"); System.out.println("*********msg***" + msg); try { this.htmlService.all(msg); returnStatus.setSuccess(true); returnStatus.setMsg("成功!"); } catch (Exception e) { e.printStackTrace(); returnStatus.setSuccess(false); returnStatus.setMsg(e.getMessage()); } return returnStatus; } }
这个时候我们访问html/create即可生成。
其他类:ReturnBase
public class ReturnBase { private boolean success = false; private String msg; public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
最终直接通过url即可访问生成页面的,结果保存为d:/file.html,实际应用中根据数据id等规则生成相应的文件目录结构即可