??xml version="1.0" encoding="utf-8" standalone="yes"?> W一个tag定义的是spring的配|文件地址到环境参?context parameter) W二个tag定义一个listener?u>org.springframework.web.context.ContextLoaderListener,q里相当于j2ee容器l我们提供的main函数的切入点Q可以让我们做一些系l初始化的工作,需要实现的cLQjavax.servlet.ServletContextListener W三个tag则定义了struts2的一个filter。Filter则是Ҏơ请求(可以通过filter-mapping指定Q做qo处理Q请求首先请qfilter铄处理Q然后再到HttpServlet的initҎ。对应的cLQjavax.servlet.Filter。上面先配置了一个filter,对应的类是org.apache.struts2.dispatcher.FilterDispatcher,参数则是struts的配|文件位|?/p>
W四个参数定义了filter怎样行ؓQ显然它?do为后~的请求应用struts2q个名称的filter 查看ContextLoaderListener可知Q它正好l承?u>javax.servlet.ServletContextListenerQ用于监听javax.servlet.ServletContextEvent事g 此类implement了ServletContextListener的两个方法: 分别做context的初始化和销?/p>
另外提供了一个protectedҎQprotected ContextLoader createContextLoader() 用于创徏真正做事情的代理cCotextLoader 和一个publicҎQpublic ContextLoader getContextLoader()Q?/p>
可见Q这个Listenercȝ接将工作代理l了ContextLoadercM ____________________________________________________________________________________________________________________________ 我们按图索骥Q下面再分析org.springframework.web.context.ContextLoader ContextLoader里面有个U有成员Qprivate WebApplicationContext context 此变量提供了createҎQ?/p>
createWebApplicationContext(ServletContext servletContext, ApplicationContext parent)
因ؓSpring提供了多UWebApplicationContextc,所以需要一个方法来军_使用哪个WebApplicationContextContextc?/p>
public static final String CONTEXT_CLASS_PARAM = "contextClass"; 所以如果在web.xml中的<context-param> </context-param>中定义了参数contextClassQ那么直接就军_了用此Contextc?/p>
否则Q就应用~省{略来决定用哪个ContextcR?/p>
~省{略如下Q?/p>
实际上,~省{略从org\springframework\web\context\ContextLoader.properties 文g中取得属性org.springframework.web.context.WebApplicationContext 我们看看ContextLoader.properties文g的内容: 昄Q缺省的Contextcd?XmlWebApplicationContextc?/p>
Q!Q真累啊QSpring... OK,ȝ知道用哪个ContextcMQ那么现在到了createq个Context实例的时候了 q里很容易理解,只不q它的创Z是直接newQ而是装了一层,调用BeanUtils.instantiateClass()工具Ҏ 接下来设定WebApplicationCcontext实例的parent, servletContext, 其中配置文g位置(web.xml?的contextConfigLocation参数指定?/p>
倒数W二行调用的customizeContext()Ҏ目前Spring实现代码是空的,估计留作以后Q看来万事要留余地啊^_^Q?/p>
最后一件事是调用WebApplicationContext?font color="#ff0000" size="4">refresh()Ҏ。(q个Ҏ是个stratupҎQ很重要。他q的事情后面会着重涉及) 最后就是真正做事的initWebApplicationContext()ҎQ?/p>
它其实做得事情很单,调用loadParentContext()Ҏ获取父context,调用createWebApplicationContext()创徏WebApplicationContext实例 一切都g完成了,臛_我们明白了配|文件是如何加蝲q去的,至于IoC容器如何帮你注入配置文g中的beanQ下ơ再探烦。线索是什么呢Q你q记得在createWebApplication()的最后一步做什么了吗?refresh(), 对了Q这个就是你作ؓ职业"嘿客"要探索的下一个线?/p> 首先声明偶也不是什么高手,以下ȝ都是自己的体会。不对之处请您多指教? POQ?br>persistant object持久对象 BOQ?br>business object业务对象 VO Q?br>value object值对?br>ViewObject表现层对?br>主要对应界面昄的数据对象。对于一个WEB面Q或者SWT、SWING的一个界面,用一个VO对象对应整个界面的倹{? DTO Q?br>Data Transfer Object数据传输对象 POJO Q?br>plain ordinary java object 单java对象 DAOQ?br>data access object数据讉K对象 web.xml定义Q?br>.站台的名U和说明 要了解web.xml的设定|必须了解它的schema,从web.xml中知道它的schema是由Sum Microsystems公司定制的,如果你想更ؓ详细的了解它Q?br>可以?a >http://java.sun.com/xml/ns/j2ee/web-mapp_2_4.xsd|页Q那里有更ؓ详细的介l。这里我介绍我们q_见得最多的. <description>,<display-name>,<icon> <description>站台描述</discription> <display-name>站台名称</display-name> <icon> <large-icon>/路径/largeicon-jpg</large-icon> <distributable> <context-param> <context-param> 又如Spring framework在这里可以设|context参数 <filter> 又如Struts2可以在这里设filter <filter-mapping> ______________________________________ <servlet-name>Servlet的名U?lt;servlet-name> <servlet>
___________________________________________
<servlet-name>servlet的名U?lt;/servlet-name> <display-name>昄名称</display-name> 范例Q?/p>
<servlet-mapping> <listener> 又如Spring framework可以在这里设listener <session-cofing> <mime-mapping> <welcome-file-list> <error-page> <jsp-config> <taglib> <jsp-property-group> <display-name>Name</display-name> <url-pattern>URL</url-pattern> <el-ignored>true|false</el-ignored> <scripting-invalid>true|false</scripting-invalid> <page-encoding>encoding</page-encoding> <include-prelude>.jspf</include-prelude> <include-coda>.jspf</include-coda> <resource-ref> <rec-ref-name>资源名称</rec-ref-name> <res-type>资源U类</res-type> <res-auth>Application|Container</res-auth> <res-sharing-scope>Shareable|Unshareable</res-sharing-scope> q些都是些比较常用的,详细可以d: http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
1: <!-- spring的配|?-->
2: <context-param>
3: <param-name>contextConfigLocation</param-name>
4: <param-value>classpath:/SpringContext/applicationContext-web.xml</param-value>
5: </context-param>
7:
8: <listener>
9: <listener-class>org.springframework.web.context.ContextLoaderListener
10: </listener-class>
11: </listener>
12:
13: <filter>
14: <filter-name>struts2</filter-name>
15: <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
17: <init-param>
18: <param-name>config</param-name>
19: <param-value>struts-default.xml,struts-plugin.xml,struts/struts.xml</param-value>
20: </init-param>
21: </filter>
22:
23: <filter-mapping>
24: <filter-name>struts2</filter-name>
25: <url-pattern>*.do</url-pattern>
26: </filter-mapping>
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.context;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* Bootstrap listener to start up Spring's root {@link WebApplicationContext}.
* Simply delegates to {@link ContextLoader}.
*
* <p>This listener should be registered after
* {@link org.springframework.web.util.Log4jConfigListener}
* in <code>web.xml</code>, if the latter is used.
*
* @author Juergen Hoeller
* @since 17.02.2003
* @see ContextLoaderServlet
* @see org.springframework.web.util.Log4jConfigListener
*/
public class ContextLoaderListener implements ServletContextListener {
private ContextLoader contextLoader;
/**
* Initialize the root web application context.
*/
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
/**
* Create the ContextLoader to use. Can be overridden in subclasses.
* @return the new ContextLoader
*/
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
/**
* Return the ContextLoader used by this listener.
* @return the current ContextLoader
*/
public ContextLoader getContextLoader() {
return this.contextLoader;
}
/**
* Close the root web application context.
*/
public void contextDestroyed(ServletContextEvent event) {
if (this.contextLoader != null) {
this.contextLoader.closeWebApplicationContext(event.getServletContext());
}
}
}
public void contextInitialized(ServletContextEvent event);
public void contextDestroyed(ServletContextEvent event);
protected Class determineContextClass(ServletContext servletContext) throws ApplicationContextException {
String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
if (contextClassName != null) {
try {
return ClassUtils.forName(contextClassName);
}
catch (ClassNotFoundException ex) {
throw new ApplicationContextException(
"Failed to load custom context class [" + contextClassName + "]", ex);
}
}
else {
contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
try {
return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
}
catch (ClassNotFoundException ex) {
throw new ApplicationContextException(
"Failed to load default context class [" + contextClassName + "]", ex);
}
}
}
private static final Properties defaultStrategies;
static {
// Load default strategy implementations from properties file.
// This is currently strictly internal and not meant to be customized
// by application developers.
try {
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
}
}
# Default WebApplicationContext implementation class for ContextLoader.
# Used as fallback when no explicit context implementation has been specified as context-param.
# Not meant to be customized by application developers.
org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
protected WebApplicationContext createWebApplicationContext(
ServletContext servletContext, ApplicationContext parent) throws BeansException {
Class contextClass = determineContextClass(servletContext);
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
}
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
wac.setParent(parent);
wac.setServletContext(servletContext);
wac.setConfigLocation(servletContext.getInitParameter(CONFIG_LOCATION_PARAM));
customizeContext(servletContext, wac);
wac.refresh();
return wac;
}
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/SpringContext/applicationContext-web.xml
</param-value>
</context-param>
public WebApplicationContext initWebApplicationContext(ServletContext servletContext)
throws IllegalStateException, BeansException {
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " +
"check whether you have multiple ContextLoader* definitions in your web.xml!");
}
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();
try {
// Determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
this.context = createWebApplicationContext(servletContext, parent);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
currentContextPerThread.put(Thread.currentThread().getContextClassLoader(), this.context);
if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
return this.context;
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
}
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
throw err;
}
}
最形象的理解就是一个PO是数据库中的一条记录?br>好处是可以把一条记录作Z个对象处理,可以方便的{为其它对象?
主要作用是把业务逻辑装Z个对象。这个对象可以包括一个或多个其它的对象?br>比如一个简历,有教育经历、工作经历、社会关pȝ{?br>我们可以把教育经历对应一个POQ工作经历对应一个POQ社会关pd应一个PO?br>建立一个对应简历的BO对象处理历,每个BO包含q些PO?br>q样处理业务逻辑Ӟ我们可以针对BOd理?
主要用于q程调用{需要大量传输对象的地方?br>比如我们一张表?00个字D,那么对应的PO有100个属性?br>但是我们界面上只要显C?0个字D,
客户端用WEB service来获取数据,没有必要把整个PO对象传递到客户端,
q时我们可以用只有q?0个属性的DTO来传递结果到客户端,q样也不会暴露服务端表结?到达客户端以后,如果用这个对象来对应界面昄Q那此时它的w䆾p{为VO
个h感觉POJO是最常见最多变的对象,是一个中间对象,也是我们最常打交道的对象?br>一个POJO持久化以后就是PO
直接用它传递、传递过E中是DTO
直接用来对应表示层就是VO
q个大家最熟悉Q和上面几个O区别最大,基本没有互相转化的可能性和必要.
主要用来装Ҏ据库的访问。通过它可以把POJO持久化ؓPOQ用POl装出来VO、DTO
ȝ下我认ؓ一个对象究竟是什么O要看具体环境Q在不同的层、不同的应用场合Q对象的w䆾也不一P而且对象w䆾的{化也是很自然的。就像你对老婆来说是老公Q对父母来说是子女。设计这些概늚初衷不是Z唬h而是Z更好的理解和处理各种逻辑Q让大家能更好的ȝ面向对象的方式处理问?
大家千万不要陷入q度设计Q大可不必ؓ了设计而设计一定要在代码中区分各个对象。一句话技术是为应用服务的?br>Ƣ迎指正?
M个图Q感觉没有完全表辑և自己的意思。。。。。谁帮忙完善下,最好能体现各个O在MVC中的位置
.针对环境参数(Context)做初始化工作
.Servlet的名U和映射
.Session的设?br>.Tag library的对?br>.JSP|页讑֮
.Mime Type处理
.错误处理
.利用JDNI取得站台资源 1: <?xml version="1.0" encoding="ISO-8859-1"?>
2:
3: <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
4: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5: xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
6: version="2.4">
7: <web-app>
q是一般在写XML时所做的声明,定义了XML的版本,~码格式,q有重要的指明schema的来??a >http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd.
____________________________________________
对站台做出描q?
定义站台的名U?
icon元素包含small-icon和large-icon两个子元?用来指定web站台中小图标和大图标的\?
<small-icon>/路径/smallicon.gif</small-icon>
small-icon元素应指向web站台中某个小图标的\?大小?6 X 16 pixel,但是图象文g必须为GIF或JPEG格式,扩展名必Mؓ:.gif?jpg.
large-icon元素应指向web站台中某个大图表路径,大小?2 X 32 pixel,但是图象文g必须为GIF或JPEG的格?扩展名必Mؓ; gif
或jpg.
范例: 1: <display-name>Develop Example</display-name>
2: <description>JSP 2.0 Tech Book's Examples</description>
3: <icon>
4: <small-icon>/images/small.gif</small-icon>
5: <large-icon>/images/large.gir</large-icon>
6: </icon>
7:
8: <distributable>
______________________________________
distributable 元素为空标签,它的存在与否可以指定站台是否可分布式处理.如果web.xml中出现这个元?则代表站台在开发时已经
被设计ؓ能在多个JSP Container 之间分散执行.
范例: 1: <distributable/>
___________________________________
context-param 元素用来讑֮web站点?u>环境参数(context),它包含两个子元素:
param-name和param-value.
<param-name>参数名称</param-name>
讑֮Context名称
<param-value>?lt;/param-value>
讑֮Context名称的?br></context-param>
范例: 1: <context-param>
2: <param-name>param_name</param-name>
3: <param-value>param_value</param-value>
4: </context-param>
此所讑֮的参?在JSP|页中可以用下列方法来取得: 1: ${initParam.param_name}
若在Servlet可以使用下列Ҏ来获? 1: String param_name=getServletContext().getInitParamter("param_name");
1: <!-- spring的配|?-->
2: <context-param>
3: <param-name>contextConfigLocation</param-name>
4: <param-value>classpath:/SpringContext/applicationContext-web.xml
5: </param-value>
6: </context-param>
_________________________________
filter元素用来声明filter的相兌?filter元素除了下面介绍的的子元素之?q包?lt;servlet>介绍q的<icon>,<display-name>
,<description>,<init-param>,其用途一?
<filter-name>Filter的名U?lt;/filter-name>
定义Filter的名U?
<filter-class>Filter的类名称</filter-class>
定义Filter的类名称.例如:com.foo.hello
范例: 1: <filter>
2: <filter-name>setCharacterEncoding</filter-name>
3: <filter-class>coreservlet.javaworld.CH11.SetCharacterEncodingFilter</filter-class>
4: <init-param>
5: <param-name>encoding</param-name>
6: <param-value>GB2312</param-value>
7: </init-param>
8: </filter>
9:
10: <filter-mapping>
1: <filter>
2: <filter-name>struts2</filter-name>
3: <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
4: </filter-class>
5: <init-param>
6: <param-name>config</param-name>
7: <param-value>struts-default.xml,struts-plugin.xml,struts/struts.xml</param-value>
8: </init-param>
9: </filter>
10:
11: <filter-mapping>
12: <filter-name>struts2</filter-name>
13: <url-pattern>*.do</url-pattern>
14: </filter-mapping>
filter-mapping 元素的两个主要子元素filter-name和url-pattern.用来定义Filter所对应的URL.
<filter-name>Filter的名U?lt;/filter-name>
定义Filter的名U?
<url-pattern>URL</url-pattern>
Filter所对应的RUL.例如:<url-pattern>/Filter/Hello</url-pattern>
定义servlet的名U?
<dispatcher>REQUEST|INCLUDE|FORWARD|ERROR</disaptcher>
讑֮Filter对应的请求方?有RQUEST,INCLUDE,FORWAR,ERROR四种,默认为REQUEST.
</filter-mapping>
范例: 1: <filter-mapping>
2: <filter-name>GZIPEncoding</filter-name>
3: <url-pattern>/*</url-pattern>
4: </filter-mapping>
<servlet-class>servlet对应的class?lt;/servlet-class>
1: <servlet>
2: <servlet-name>org.apache.jsp.index_jsp</servlet-name>
3: <servlet-class>org.apache.jsp.index_jsp</servlet-class>
4: </servlet>
_____________________________________________
servlet-mapping元素包含两个子元素servlet-name和url-pattern.用来定义servlet所对应URL.
<servlet-name>Servlet的名U?lt;/servlet-name>
定义Servlet的名U?
<url-pattern>Servlet URL</url-pattern>
定义Servlet所对应的RUL.例如:<url-pattern>/Servlet/Hello</url-pattern>
</servlet-mapping>
范例: 1: <servlet-mapping>
2: <servlet-name>LoginChecker</servlet-name>
3: <url-pattern>/LoginChecker</url-pattern>
4: </servlet-mapping>
___________________________________________
<listener>
listener元素用来定义Listener接口,它的主要子元素ؓ<listener-class>
<listen-class>Listener的类名称</listener-class>
定义Listener的类名称.例如: com.foo.hello
<listener>
范例: 1: <listener>
2: <listener-class>coreservlet.javaworld.CH11.ContenxtListener</listener-class>
3: </listener>
1: <listener>
2: <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
3: </listener>
__________________________________
<session-config>
session-config包含一个子元素session-timeout.定义web站台中的session参数.
<session-timeout>分钟</session-timeout>
定义q个web站台所有session的有效期?单位为分?
</session-config>
范例: 1: <session-config>
2: <session-timeout>20</session-timeout>
3: </session-config>
___________________________________________________
<mima-mapping>
mime-mapping包含两个子元素extension和mime-type.定义某一个扩展名和某一MIME Type做对?
<extension>扩展名名U?lt;/extension>
扩展名称
<mime-type>MIME格式</mime-type>
MIME格式.
</mime-mapping>
范例: 1: <mime-mapping>
2: <extension>doc</extension>
3: <mime-type>application/vnd.ms-word</mime-type>
4: </mime-mapping>
5: <mime-mapping>
6: <extension>xls</extension>
7: <mime-type>application/vnd.ms-excel</mime-type>
8: </mime-mapping>
9: <mime-mapping>
10: <extension>ppt</extesnion>
11: <mime-type>application/vnd.ms-powerpoint</mime-type>
12: </mime-mapping>
_____________________________________________
<welcome-file-list>
welcome-file-list包含一个子元素welcome-file.用来定义首页列单.
<welcome-file>用来指定首页文g名称</welcome-flie>
welcome-file用来指定首页文g名称.我们可以?lt;welcome-file>指定几个首页,而服务器会依照设定的序来找首页.
范例: 1: <welcome-file-list>
2: <welcome-file>index.jsp</welcome-file>
3: <welcome-file>index.htm</welcome-file>
4: </welcome-file-list>
_________________________
<error-page>
error-page元素包含三个子元素error-code,exception-type和location.错误代?Error Code)或异?Exception)的种cd?br>到web站台资源路径.
<error-code>错误代码</error-code>
HTTP Error code,例如: 404
<exception-type>Exception</exception-type>
一个完整名U的Java异常cd
<location>/路径</location>
在web站台内的相关资源路径
</error-page>
范例: 1: <error-page>
2: <error-code>404</error-code>
3: <location>/error404.jsp</location>
4: </error-page>
5: <error-page>
6: <exception-type>java.lang.Exception</exception-type>
7: <location>/except.jsp</location>
8: </error-page>
_______________________________________________
<jsp-config>
jsp-config元素主要用来讑֮JSP的相关配|?<jsp:config>包括<taglib>?lt;jsp-property-group>两个子元?其中<taglib>元素
在JSP 1.2时就已经存在??lt;jsp-property-group>是JSP 2.0新增的元?
taglib元素包含两个子元素taglib-uri和taglib-location.用来讑֮JSP|页用到的Tag Library路径.
<taglib-uri>URI</taglib-uri>
taglib-uri定义TLD文g的URI,JSP|页的taglib指o可以l由q个URI存取到TLD文g.
<taglib-location>/WEB-INF/lib/xxx.tld</taglib-laction>
TLD文g对应Web站台的存放位|?
</taglib>
jsp-property-group元素包含8个元?分别?
<description>Description</descrition>
此设定的说明
此设定的名称
讑֮值所影响的范??/CH2 或?*.jsp
若ؓtrue,表示不支持EL语法.
若ؓtrue表示不支?lt;%scription%>语法.
讑֮JSP|页的编?
讄JSP|页的抬?扩展名ؓ.jspf
讄JSP|页的结?扩展名ؓ.jspf
</jsp-property-group>
</jsp-config>
范例: 1: <jsp-config>
2: <taglib>
3: <taglib-uri>Taglib</taglib-uri>
4: <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
5: </taglib>
6: <jsp-property-group>
7: <description>
8: Special property group for JSP Configuration JSP example.
9: </description>
10: <display-name>JSPConfiguration</display-name>
11: <uri-pattern>/*</uri-pattern>
12: <el-ignored>true</el-ignored>
13: <page-encoding>GB2312</page-encoding>
14: <scripting-inivalid>true</scripting-inivalid>
15: ............
16: </jsp-property-group>
17: </jsp-config>
________________________________________________
<resource-ref>
resource-ref元素包括五个子元素description,res-ref-name,res-type,res-auth,res-sharing-scope.利用JNDI取得站点?br>利用资源.
<description>说明</description>
资源说明
资源名称
资源U类
资源由Application或Container来许?
资源是否可以׃n.默认gؓ Shareable
范例: 1: <resource-ref>
2: <description>JNDI JDBC DataSource of JSPBook</description>
3: <res-ref-name>jdbc/sample_db</res-ref-name>
4: <res-type>javax.sql.DataSoruce</res-type>
5: <res-auth>Container</res-auth>
6: </resource-ref>
只需要指定需要生成的目录Q就可以扚w?BO.java转换?VO.as
代码如下Q?/p>
1: /**
2: *
3: */
4: package com.suprasoft.utils.flex;
5:
6: /**
7: * @作?Charles <chio.yang@gmail.com> Suprasoft Inc.,
8: *
9: * 2008-12-25
10: */
11:
12: import java.io.BufferedWriter;
13: import java.io.File;
14: import java.io.FileWriter;
15: import java.io.IOException;
16: import java.lang.reflect.Field;
17:
18: import org.apache.commons.lang.StringUtils;
19:
20: public class POJO2VO {
21: public POJO2VO() {
22: }
23:
24: public static String getClassType(Class c) {
25: String typeName = c.getSimpleName();
26: // System.out.println("typename " + typeName);
27: // 数值型的都转成Number
28: if (typeName.equals("BigDecimal") || typeName.equals("Decimal")
29: || typeName.equals("Double") || typeName.equals("int")
30: || typeName.equals("Integer") || typeName.equals("Long")
31: || typeName.equals("long")) {
32: return "Number";
33: } else if (typeName.equals("Boolean")) {
34: return "boolean";
35: } else if (typeName.equals("Set")) {
36: return "Array";
37: } else {
38: if (isBasicType(typeName)) {
39: return typeName;// 其它cd的设|ؓ未知cd
40: } else {
41: return typeName + "VO";// 自定义类型的Q加上VOl尾
42: }
43: }
44:
45: }
46:
47: // 是否基本的数据类?/span>
48: public static boolean isBasicType(String type) {
49: return type.equals("String") || type.equals("BigDecimal")
50: || type.equals("Decimal") || type.equals("Double")
51: || type.equals("int") || type.equals("Integer")
52: || type.equals("Boolean") || type.equals("Set")
53: || type.equals("Long") || type.equals("long")
54: || type.equals("Date");
55: }
56:
57: public static String repeat(String c, int count) {
58: String temp = "";
59: for (int i = 0; i < count; i++) {
60: temp += c;
61: }
62:
63: return temp;
64: }
65:
66: public static void makeDirByPackageName(String rootFolder, String packageName) {
67:
68: File folder = new File(rootFolder + "\\" + packageName.replace(".", "\\"));
69: if(!folder.exists()) {
70: folder.mkdirs();
71: }
72: }
73:
74: public static void generateAsFiles(String pojoQualifiedName ){
75:
76: }
77:
78: /**
79: * 生成as文g
80: *
81: * @param pojoQualifiedName
82: * @param voPackageName
83: * @param folders
84: * @throws ClassNotFoundException
85: * @throws IOException
86: */
87: public static void generateAsFile(String pojoQualifiedName, String voPackageRootFolder, String voPackageName) throws ClassNotFoundException, IOException {
88: //pojoName必须带包名的Qualified Name
89: Class c = Class.forName(pojoQualifiedName);
90: Field[] fields = c.getDeclaredFields();
91:
92: POJO2VO.makeDirByPackageName(voPackageRootFolder, voPackageName);
93: // as的vo对象名称l尾加上VO标志
94: File f = new File(voPackageRootFolder + "\\"
95: + voPackageName.replace(".", "\\") + "\\"
96: + c.getSimpleName().replace("BO", "VO") + ".as");
97:
98: BufferedWriter bw = new BufferedWriter(new FileWriter(f));
99: // 包名Q没有设|包名就取和java pojo一L包名
100: if (StringUtils.isEmpty(voPackageName)) {
101: bw.write("package " + c.getPackage().getName() + "\n{\n");
102: } else {
103: bw.write("package " + voPackageName + "\n{\n");
104: }
105:
106: // 写类
107: bw.write(repeat(" ", 4) + "public class " + c.getSimpleName().replace("BO", "VO") + "\n");
108: bw.write(repeat(" ", 4) + "{\n");
109:
110: // 写属?/pre>111: for (int i = 0; i < fields.length; i++) {
112: Class fieldType = fields[i].getType();
113: String typeName = getClassType(fieldType);
114: bw.write(repeat(" ", 8) + "private var _" + fields[i].getName()115: + ":" + typeName + ";\n");116: }
117:
118: bw.write("\n\n\n");119: // 写空的构造函?/pre>120: bw.write(repeat(" ", 8) + "public function " + c.getSimpleName().replace("BO", "VO")121: + "(){}\n\n");122:
123: // ?setter/getter Ҏ
124: for (int i = 0; i < fields.length; i++) {
125: Class fieldType = fields[i].getType();
126: String typeName = getClassType(fieldType);
127: // setter
128: bw.write(repeat(" ", 8) + "public function set "129: + fields[i].getName() + "(value:" + typeName + "):void{\n");130: bw.write(repeat(" ", 12) + "this._" + fields[i].getName()131: + " = value;\n");132: bw.write(repeat(" ", 8) + "}\n\n");133: // getter
134: bw.write(repeat(" ", 8) + "public function get "135: + fields[i].getName() + "():" + typeName + "{\n");136: bw.write(repeat(" ", 12) + "return this._" + fields[i].getName()137: + ";\n");138: bw.write(repeat(" ", 8) + "}\n\n\n");139:
140: }
141: bw.write(repeat(" ", 4) + "}\n");142: bw.write("}");143: bw.close();
144: }
145:
146: public static void generateAsFileBatch(String pojoPackageRootFolder, String pojoPackageName, String voPackageRootFolder, String voPackageName) throws ClassNotFoundException, IOException {
147: String packageFolder = pojoPackageRootFolder + "\\" + pojoPackageName.replace(".", "\\");148: File dir = new File(packageFolder);
149: File[] files = dir.listFiles();
150: for(File file : files){
151: if(file.isDirectory()) continue;
152:
153: if(file.getName().endsWith("BO.java")){154: String pojoName = pojoPackageName + "." + file.getName().replace(".java", "");155: POJO2VO.generateAsFile(pojoName, voPackageRootFolder, voPackageName);
156: }
157: }
158: }
159:
160:
161: /*
162: * 试
163: */
164: public static void main(String[] args) throws ClassNotFoundException, IOException {
165:
166: // String[] pojos = { "com.bankcomm.icms.domain.migrate.RawCollateralBasicBO" };167: // for (int i = 0; i < pojos.length; i++) {
168: // POJO2VO.generateAsFile(pojos[i], "C:", "com.bankcomm.icms.domain");169: // }
170:
171: POJO2VO.generateAsFileBatch("D:\\ICMS\\eclipse\\workspace\\Utils\\src\\",172: "com.bankcomm.icms.domain.migrate",173: "C:\\Documents and Settings\\Administrator\\My Documents\\Flex Builder 3\\Temp\\src",174: "com.bankcomm.icms.domain");
175: }
176: }