在网站根目录下,静态缓存首页为index.html。
1、用httpclient拉取index.action,然后用httpclient拉取内容,把内容合并成一行,用common-io中的fileutils保存文件到网站根目录下的index.html。
2、定时更新index.html,用quartz和spring。
项目下载地址:
http://download.csdn.net/detail/yx511500623/8457471
代码如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_app" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>gradle-web</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</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>
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>static/index.html</welcome-file>
</welcome-file-list>
</web-app>
IndexController.java
package net.zkbc.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/index")
public class IndexController {
@RequestMapping(value = "/", method = { RequestMethod.POST,
RequestMethod.GET })
@ResponseBody
public String index () throws Exception {
return "我是测试页面";
}
}
PageUtil.java
package net.zkbc.demo.webstatic;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.http.client.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@Lazy(value=false)
public class PageUtil {
private final static Logger log = LoggerFactory.getLogger(PageUtil.class);
// static final String INDEX_URL = "http://www.baidu.com";
static final String INDEX_URL = "http://localhost:9080/index/";
private static String webRootPath = "";
private static String sysEnv="dev";
static {
if("dev".equals(sysEnv)){
webRootPath=StringUtils.join(new Object[]{SystemUtils.getUserDir(),"src","main","webapp","static"}, SystemUtils.FILE_SEPARATOR);
}else{
webRootPath=StringUtils.join(new Object[]{SystemUtils.getUserDir().getParent(),"webapps","ROOT","static"}, SystemUtils.FILE_SEPARATOR);
}
log.info("webRootPath=[{}]",webRootPath);
}
@Scheduled(cron="*/5 * * * * ?")
public void indexPage() throws Exception {
log.debug("静态化首页time=[{}]",DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
URL indexUrl = new URL(INDEX_URL);
File dest = new File(webRootPath,
"index.html");
FileUtils.copyURLToFile(indexUrl, dest);
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"
default-lazy-init="true">
<task:annotation-driven />
<context:component-scan base-package="net.zkbc" >
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
<context:property-placeholder
ignore-unresolvable="true" file-encoding="UTF-8"
location="classpath*:application.properties" />
</beans>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<context:component-scan base-package="net.zkbc" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<mvc:annotation-driven>
<!--<mvc:async-support default-timeout="300000"/>-->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<mvc:default-servlet-handler/>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="${app.locale:en_US}" />
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000000" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/themes/${app.theme}/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- <bean class="org.springframework.web.servlet.view.XmlViewResolver"> -->
<!-- <property name="order" value="1"/> -->
<!-- <property name="location" value="/WEB-INF/views.xml"/> -->
<!-- </bean> -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Throwable">500</prop>
</props>
</property>
</bean>
<context:property-placeholder ignore-unresolvable="true" file-encoding="UTF-8"
location="classpath*:application.properties" />
</beans>
build.gradle
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'
apply plugin: 'jetty'
[compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8'
ext.springVersion = '3.2.8.RELEASE'
ext.commonsLangVersion = '2.6'
ext.jacksonVersion = '2.3.3'
ext.log4jVersion = '1.2.17'
task createJavaProject << {
sourceSets*.java.srcDirs*.each { it.mkdirs() }
sourceSets*.resources.srcDirs*.each { it.mkdirs()}
}
task createWebProject(dependsOn: 'createJavaProject') << {
def webAppDir = file('src/main/webapp')
webAppDir.mkdirs()
}
eclipse {
classpath {
defaultOutputDir = file("build/eclipse")
}
}
dependencies {
compile(
'javax.servlet:javax.servlet-api:3.1-b07',
'org.slf4j:slf4j-log4j12:1.7.5',
'org.slf4j:slf4j-jdk14:1.7.5',
'log4j:log4j:1.2.17',
'org.springframework:spring-aop:3.2.8.RELEASE',
'org.springframework:spring-beans:3.2.8.RELEASE',
'org.springframework:spring-context:3.2.8.RELEASE',
'org.springframework:spring-context-support:3.2.8.RELEASE',
'org.springframework:spring-core:3.2.8.RELEASE',
'org.springframework:spring-expression:3.2.8.RELEASE',
'org.springframework:spring-jdbc:3.2.8.RELEASE',
'org.springframework:spring-oxm:3.2.8.RELEASE',
'org.springframework:spring-test:3.2.8.RELEASE',
'org.springframework:spring-tx:3.2.8.RELEASE',
'org.springframework:spring-web:3.2.8.RELEASE',
'org.springframework:spring-webmvc:3.2.8.RELEASE',
'commons-httpclient:commons-httpclient:3.0',
'commons-io:commons-io:2.4',
'commons-lang:commons-lang:2.6',
'commons-collections:commons-collections:3.2',
'commons-logging:commons-logging:1.1',
'commons-fileupload:commons-fileupload:1.3.1',
'org.quartz-scheduler:quartz:1.8.5',
'org.apache.httpcomponents:httpclient:4.3.2',
'com.fasterxml.jackson.core:jackson-annotations:2.3.3',
'com.fasterxml.jackson.core:jackson-core:2.3.3',
'com.fasterxml.jackson.core:jackson-databind:2.3.3'
)
testCompile(
'junit:junit:4.11'
)
}
jettyRun {
httpPort = 9080
contextPath = ''
reload = 'manual'
classpath = files(eclipse.classpath.defaultOutputDir) + classpath
}
jettyRun.doFirst {
jettyRun.webDefaultXml = new File("$projectDir.path\\conf\\jetty", 'jetty-webdefault.xml')
}
repositories {
maven {
url 'http://localhost:8081/nexus/content/groups/public/'
}
mavenCentral name:'codehaus', artifactUrls: ['http://repository.codehaus.org/']
mavenCentral name:'ibiblio', artifactUrls: ['http://mirrors.ibiblio.org/pub/mirrors/maven2/']
mavenCentral name:'apache', artifactUrls: ['http://people.apache.org/repo/m2-ibiblio-rsync-repository/']
mavenCentral name:'maven', artifactUrls: ['http://repo2.maven.org/maven2/']
mavenCentral name:'restlet', artifactUrls: ['http://maven.restlet.org/']
mavenCentral name:'lds', artifactUrls: ['https://code.lds.org/nexus/content/repositories/thirdparty/']
mavenCentral name:'m2m', artifactUrls: ['http://repository.m2m.io:8081/nexus/content/groups/public/']
}
setenv.bat
set JAVA_HOME=E:\tools\JDK7-64\JDK
set GROOVY_HOME=E:\tools\groovy-2.1.9
set GRADLE_HOME=E:\tools\gradle-2.2.1
set ECLIPSE_HOME=E:\tools\ECLIPSE-JEE-64BIT
set PATH=%JAVA_HOME%\bin;%GRADLE_HOME%\bin;%PATH%
gradle-eclipse.bat
@echo off
call "%~dp0\setenv"
cd /d "%~dp0.."
gradle -Dfile.encoding=UTF-8 createWebProject eclipse 1>error.log 2>>stdio.log 1>>con 2>>con & pause
gradle-jettyRun.bat
@echo off
call "%~dp0\setenv"
cd /d "%~dp0.."
set GRADLE_OPTS=-Xmx512m -XX:MaxPermSize=512m -Xdebug -Xrunjdwp:transport=dt_socket,address=9000,server=y,suspend=n
gradle -Dfile.encoding=UTF-8 jettyRun 1>error.log 2>>stdio.log 1>>con 2>>con & pause