src/main/webapp |- tfw/ (页面文件) `- WEB-INF/ |- web.xml (站点主配置文件) |- pages/tfw/ (受保护的页面文件) |- conf/ (各框架的顶层配置文件) |- data/ (数据库之外的数据) `- lib/ (Maven 管制之外的库)
src/ |- main/ | |- java/tfw/integration_demo/ | `- resources/tfw/integration_demo/ `- test/ |- java/tfw/integration_demo/ `- resources/tfw/integration_demo/
echo "========================================"; echo "Project variable define:"; echo "========================================"; export APP_NAME=tfw-web-integration; export APP_VERSION=2016-12-2X_XX-XX; export PROJ_BASE=$(pwd); export SRC_DIR_IN_PROJ=src/main/java; export CLASSES_DIR_IN_PROJ=target/classes; export OUT_DIR=$PROJ_BASE/export.$APP_VERSION; export TMP_DIR=$OUT_DIR/tmp; echo "========================================"; echo "Web project variable define:"; echo "========================================"; export WAR_NAME=$APP_NAME; export WEBROOT_DIR=$PROJ_BASE/src/main/webapp; export PAGE_DIR_IN_WEBROOT_DIR="tfw WEB-INF/pages/tfw"; export LIB_DIR=$WEBROOT_DIR/WEB-INF/lib; export DEPENDENCE_IN_LIB_DIR="tfw-*.jar"; # echo "========================================"; # echo "Javadoc variable define:"; # echo "========================================"; # # export JAVADOC_BASE=$PROJ_BASE/doc/java_doc; # export LANG=en; # export CLASSPATH=$CLASSPATH; # # echo "========================================"; # echo "Javadoc generating:"; # echo "========================================"; # # rm -rf $JAVADOC_BASE/$LANG; # mkdir -p $JAVADOC_BASE/$LANG; # cd $PROJ_BASE/$SRC_DIR_IN_PROJ; # javadoc -author -private -subpackages * -d $JAVADOC_BASE/$LANG; # # javadoc -private -subpackages * -d $JAVADOC_BASE/$LANG; echo "========================================"; echo "Javadoc, source(main and test), classes packing up:"; echo "========================================"; rm -Rf $OUT_DIR; mkdir -p $OUT_DIR; # cd $JAVADOC_BASE; # jar Mcvf $OUT_DIR/$APP_NAME.$APP_VERSION.doc.zip * # cd $OUT_DIR; # ln -s $APP_NAME.$APP_VERSION.doc.zip $APP_NAME.doc.zip; cd $PROJ_BASE/$SRC_DIR_IN_PROJ; jar Mcvf $OUT_DIR/$APP_NAME.$APP_VERSION.src.zip * cd $OUT_DIR; ln -s $APP_NAME.$APP_VERSION.src.zip $APP_NAME.src.zip; cd $PROJ_BASE/$CLASSES_DIR_IN_PROJ; jar Mcvf $OUT_DIR/$APP_NAME.$APP_VERSION.raw.jar * cd $OUT_DIR; ln -s $APP_NAME.$APP_VERSION.raw.jar $APP_NAME.raw.jar; echo "========================================"; echo "All-in-one jar packing up:"; echo "========================================"; rm -Rf $TMP_DIR; mkdir -p $TMP_DIR/classes; cd $TMP_DIR/classes; jar xvf $OUT_DIR/$APP_NAME.$APP_VERSION.raw.jar; jar xvf $OUT_DIR/$APP_NAME.$APP_VERSION.src.zip; jar Mcvf $OUT_DIR/$APP_NAME.$APP_VERSION.aij.jar *; cd $OUT_DIR; ln -s $APP_NAME.$APP_VERSION.aij.jar $APP_NAME.aij.jar; echo "========================================"; echo "Web project preparing:"; echo "========================================"; rm -Rf $TMP_DIR/WebRoot; mkdir -p $TMP_DIR/WebRoot; cd $WEBROOT_DIR; jar Mcvf $OUT_DIR/$APP_NAME.$APP_VERSION.pages.zip $PAGE_DIR_IN_WEBROOT_DIR; cd $OUT_DIR; ln -s $APP_NAME.$APP_VERSION.pages.zip $APP_NAME.pages.zip; cd $TMP_DIR/WebRoot; jar xvf $OUT_DIR/$APP_NAME.$APP_VERSION.pages.zip; echo "========================================"; echo "CUSTOMIZED preparing:"; echo "========================================"; cd $PROJ_BASE; cp -a export.sh $OUT_DIR; cp -a export.sh $TMP_DIR/WebRoot; mkdir -p $OUT_DIR/eclipse_project_files; cp -a .classpath .project pom.xml $OUT_DIR/eclipse_project_files; cd $WEBROOT_DIR/WEB-INF; mkdir -p $TMP_DIR/WebRoot/WEB-INF; cp -a web.xml conf data $TMP_DIR/WebRoot/WEB-INF; mkdir -p $TMP_DIR/WebRoot/WEB-INF/classes; cp -a $PROJ_BASE/src/main/resources/* $TMP_DIR/WebRoot/WEB-INF/classes; cd $PROJ_BASE/src/main/resources; jar Mcvf $OUT_DIR/$APP_NAME.$APP_VERSION.resources.zip * cd $OUT_DIR; ln -s $APP_NAME.$APP_VERSION.resources.zip $APP_NAME.resources.zip; cd $PROJ_BASE/src/test/java; jar Mcvf $OUT_DIR/$APP_NAME.$APP_VERSION.test-src.zip * cd $OUT_DIR; ln -s $APP_NAME.$APP_VERSION.test-src.zip $APP_NAME.test-src.zip; cd $PROJ_BASE/src/test/resources; jar Mcvf $OUT_DIR/$APP_NAME.$APP_VERSION.test-resources.zip * cd $OUT_DIR; ln -s $APP_NAME.$APP_VERSION.test-resources.zip $APP_NAME.test-resources.zip; cd $PROJ_BASE; jar Mcvf $OUT_DIR/$APP_NAME.$APP_VERSION.doc.zip doc cd $OUT_DIR; ln -s $APP_NAME.$APP_VERSION.doc.zip $APP_NAME.doc.zip; echo "========================================"; echo "Web project packing up:"; echo "========================================"; mkdir -p $TMP_DIR/WebRoot/WEB-INF/lib; cd $LIB_DIR; cp -a $OUT_DIR/$APP_NAME.$APP_VERSION.raw.jar $DEPENDENCE_IN_LIB_DIR $TMP_DIR/WebRoot/WEB-INF/lib; cd $TMP_DIR/WebRoot; jar Mcvf $OUT_DIR/$WAR_NAME.$APP_VERSION.raw.war *; cd $OUT_DIR; ln -s $WAR_NAME.$APP_VERSION.raw.war $WAR_NAME.raw.war; ln -s $WAR_NAME.raw.war $WAR_NAME.raw.war.zip; rm -f $TMP_DIR/WebRoot/WEB-INF/lib/$APP_NAME.$APP_VERSION.raw.jar; cd $LIB_DIR; cp -a $OUT_DIR/$APP_NAME.$APP_VERSION.aij.jar $TMP_DIR/WebRoot/WEB-INF/lib; cd $TMP_DIR/WebRoot; jar Mcvf $OUT_DIR/$WAR_NAME.$APP_VERSION.aij.war *; cd $OUT_DIR; ln -s $WAR_NAME.$APP_VERSION.aij.war $WAR_NAME.aij.war; ln -s $WAR_NAME.aij.war $WAR_NAME.aij.war.zip; echo "========================================"; echo "Cleaning:"; echo "========================================"; rm -rf $TMP_DIR;
……
public class _01_DataSourceConfigServlet extends HttpServlet
{
……
private String strFileName =
"WEB-INF/conf/tfw/integration_demo/datasource.properties";
private String strRealPath;
……
{
strRealPath = getServletContext().getRealPath(strFileName);
}
……
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
……
}
}
<servlet> <servlet-name>ds_config_servlet</servlet-name> <servlet-class>tfw.integration_demo._03_servlet_and_jsp._01_DataSourceConfigServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ds_config_servlet</servlet-name> <url-pattern>/tfw/integration_demo/03_servlet_and_jsp/01_ds_config/action.jsp</url-pattern> </servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app [ <!-- Define new entity. --> <!ENTITY _03_servlet_and_jsp SYSTEM "conf/tfw/integration_demo/03_servlet_and_jsp/web_frg.xml"> ]> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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>jweb.tfw.2016_integration_demo_spring_web_mvc</display-name> &_03_servlet_and_jsp; <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> …… <dependencies> <dependency> <!-- [Spring 3] Web MVC. --> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.8.RELEASE</version> </dependency> </dependencies> </project>
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd">
</beans>
<servlet>
<servlet-name>spring_web_mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/tfw/integration_demo/04_spring_web_mvc/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring_web_mvc</servlet-name>
<url-pattern>*.spr</url-pattern>
</servlet-mapping>
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.spr</url-pattern>
</filter-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app [ <!-- Define new entity. --> <!ENTITY _03_servlet_and_jsp SYSTEM "conf/tfw/integration_demo/03_servlet_and_jsp/web_frg.xml"> <!ENTITY _04_spring_web_mvc SYSTEM "conf/tfw/integration_demo/04_spring_web_mvc/web_frg.xml"> ]> <web-app …… ……> …… &_03_servlet_and_jsp; &_04_spring_web_mvc; <welcome-file-list> …… </welcome-file-list> </web-app>
……
public class InternalRealNameResourceView extends InternalResourceView
{
/**
* @see org.springframework.web.servlet.view.AbstractUrlBasedView#checkResource(java.util.Locale)
* @author Typhoon.Free.Wolf
* @version ……
* @param locale
* @return {@code true} if the resource exists (or is assumed to exist);
* {@code false} if we know that it does not exist
* @throws Exception .<br />
*/
@Override
public boolean checkResource(Locale locale) throws Exception
{
File file = new File(getServletContext().getRealPath("/") + getUrl());
return file.exists();
}
}
<?xml version="1.0" encoding="UTF-8"?> <beans ……> <bean id="internalRealNameResourceView" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="tfw.integration_demo._04_spring_web_mvc.view.InternalRealNameResourceView"/> <property name="order" value="999"/> </bean> </beans>
……
public class _01_DataSourceConfigController implements Controller
{
……
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception
{
……
String strTarget =
"/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/01_basic/01_datasource.jsp";
return new ModelAndView(strTarget);
}
}
<?xml version="1.0" encoding="UTF-8"?> <beans ……> <bean id="internalRealNameResourceView" ……> …… </bean> <!-- [########################################] --> <bean id="basic.dsCfgCtrl" class="tfw.integration_demo._04_spring_web_mvc._01_basic.controller._01_DataSourceConfigController"/> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans ……> …… <bean id="basic.dsCfgCtrl" ……/> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/tfw/integration_demo/04_spring_web_mvc/01_basic/01_ds_config/action.spr">basic.dsCfgCtrl</prop> </props> </property> </bean> </beans>
<project ……> …… <dependencies> …… <dependency> <!-- For DAO and service (and more) test. --> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <!-- Oracle JDBC driver for JDK 1.5. --> <groupId>cn.easyproject</groupId> <artifactId>ojdbc5</artifactId> <version>11.2.0.4</version> </dependency> <dependency> <!-- DataSource. --> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <!-- For MD5 encryption. --> <groupId>org.lucee</groupId> <artifactId>commons-codec</artifactId> <version>1.10.0</version> </dependency> </dependencies> </project>
<servlet> <servlet-name>spring_web_mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- <param-value>/WEB-INF/conf/tfw/integration_demo/04_spring_web_mvc/applicationContext.xml</param-value> --> <param-value>classpath:tfw/integration_demo/_04_spring_web_mvc/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring_web_mvc</servlet-name> <url-pattern>*.spr</url-pattern> </servlet-mapping> <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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.spr</url-pattern> </filter-mapping>
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <bean id="internalRealNameResourceView" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="tfw.integration_demo._04_spring_web_mvc.view.InternalRealNameResourceView"/> <property name="order" value="999"/> </bean> <bean id="basic.dsCfgCtrl" class="tfw.integration_demo._04_spring_web_mvc._01_basic.controller._01_DataSourceConfigController"/> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/tfw/integration_demo/04_spring_web_mvc/01_basic/01_ds_config/action.spr">basic.dsCfgCtrl</prop> </props> </property> </bean> </beans>
ds_cfg.jdbc_url=jdbc:oracle:thin:@127.0.0.1:1521:demo01 ds_cfg.jdbc_driver=oracle.jdbc.driver.OracleDriver ds_cfg.jdbc_user=scott ds_cfg.jdbc_passwd=tiger ds_cfg.pool_max_size=5 ds_cfg.pool_max_wait=600
……
public class _01_DataSourceConfigServlet extends HttpServlet
{
……
private String strFileName =
"WEB-INF/classes/tfw/integration_demo/datasource.properties";
……
}
……
public class _01_DataSourceConfigController implements Controller
{
……
private String strFileName =
"WEB-INF/classes/tfw/integration_demo/datasource.properties";
……
}
<?xml version="1.0" encoding="UTF-8"?> <beans ……> …… <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" …… </bean> <!-- [########################################] --> <util:properties id="dsProps" location="classpath:tfw/integration_demo/datasource.properties"/> </beans>
……
public class UserMgrTestCase
{
private String strClasspathConfFile =
"tfw/integration_demo/_04_spring_web_mvc/applicationContext.xml";
@Test
public void testObjects()
throws FileNotFoundException, SQLException
{
System.out.println("Spring Configuration File:\n\t" + strClasspathConfFile);
AbstractApplicationContext actxt =
new ClassPathXmlApplicationContext(strClasspathConfFile);
System.out.println("Application Context:\n\t" + actxt);
Properties props = actxt.getBean("dsProps", Properties.class);
System.out.println("Connection Configuration:\n\t" + props);
actxt.close();
System.out.println("Application Context Closed:\n\t" + actxt);
}
}