云自无心水自闲

天平山上白云泉,云自无心水自闲。何必奔冲山下去,更添波浪向人间!
posts - 288, comments - 524, trackbacks - 0, articles - 6
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

 

 

http://www.w3schools.com/schema/schema_howto.asp


What is an XML Schema?

The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD.

An XML Schema:

  • defines elements that can appear in a document
  • defines attributes that can appear in a document
  • defines which elements are child elements
  • defines the order of child elements
  • defines the number of child elements
  • defines whether an element is empty or can include text
  • defines data types for elements and attributes
  • defines default and fixed values for elements and attributes

 

Well-Formed is not Enough

A well-formed XML document is a document that conforms to the XML syntax rules, like:

  • it must begin with the XML declaration
  • it must have one unique root element
  • start-tags must have matching end-tags
  • elements are case sensitive
  • all elements must be closed
  • all elements must be properly nested
  • all attribute values must be quoted
  • entities must be used for special characters

The <schema> element may contain some attributes. A schema declaration often looks something like this:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
...
...
</xs:schema>

The following fragment:

xmlns:xs="http://www.w3.org/2001/XMLSchema"

indicates that the elements and data types used in the schema come from the "http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the elements and data types that come from the "http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs:

This fragment:

targetNamespace="http://www.w3schools.com"

indicates that the elements defined by this schema (note, to, from, heading, body.) come from the "http://www.w3schools.com" namespace.

This fragment:

xmlns="http://www.w3schools.com"

indicates that the default namespace is "http://www.w3schools.com".

This fragment:

elementFormDefault="qualified"

indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.

 

What is a Simple Element?

A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes.

 

Defining a Simple Element

The syntax for defining a simple element is:

<xs:element name="xxx" type="yyy"/>

XML Schema has a lot of built-in data types. The most common types are:

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time

Default and Fixed Values for Simple Elements

Simple elements may have a default value OR a fixed value specified.

A default value is automatically assigned to the element when no other value is specified.

In the following example the default value is "red":

<xs:element name="color" type="xs:string" default="red"/>

A fixed value is also automatically assigned to the element, and you cannot specify another value.

In the following example the fixed value is "red":

<xs:element name="color" type="xs:string" fixed="red"/>

 

How to Define an Attribute?

The syntax for defining an attribute is:

<xs:attribute name="xxx" type="yyy"/>

 

 

 

 

Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.

Restrictions on Values

The following example defines an element called "age" with a restriction. The value of age cannot be lower than 0 or greater than 120:

<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 


Restrictions on a Set of Values

To limit the content of an XML element to a set of acceptable values, we would use the enumeration constraint.

The example below defines an element called "car" with a restriction. The only acceptable values are: Audi, Golf, BMW:

<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 

The example above could also have been written like this:

<xs:element name="car" type="carType"/>
<xs:simpleType name="carType">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>

Note: In this case the type "carType" can be used by other elements because it is not a part of the "car" element.


Restrictions on a Series of Values

To limit the content of an XML element to define a series of numbers or letters that can be used, we would use the pattern constraint.

The example below defines an element called "letter" with a restriction. The only acceptable value is ONE of the LOWERCASE letters from a to z:

<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 

The next example defines an element called "initials" with a restriction. The only acceptable value is THREE of the UPPERCASE letters from a to z:

<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 

The next example also defines an element called "initials" with a restriction. The only acceptable value is THREE of the LOWERCASE OR UPPERCASE letters from a to z:

<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 

The next example defines an element called "choice" with a restriction. The only acceptable value is ONE of the following letters: x, y, OR z:

<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 

The next example defines an element called "prodid" with a restriction. The only acceptable value is FIVE digits in a sequence, and each digit must be in a range from 0 to 9:

<xs:element name="prodid">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 


Other Restrictions on a Series of Values

The example below defines an element called "letter" with a restriction. The acceptable value is zero or more occurrences of lowercase letters from a to z:

<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 

The next example also defines an element called "letter" with a restriction. The acceptable value is one or more pairs of letters, each pair consisting of a lower case letter followed by an upper case letter. For example, "sToP" will be validated by this pattern, but not "Stop" or "STOP" or "stop":

<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][A-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 

The next example defines an element called "gender" with a restriction. The only acceptable value is male OR female:

<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 

The next example defines an element called "password" with a restriction. There must be exactly eight characters in a row and those characters must be lowercase or uppercase letters from a to z, or a number from 0 to 9:

<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 


Restrictions on Whitespace Characters

To specify how whitespace characters should be handled, we would use the whiteSpace constraint.

This example defines an element called "address" with a restriction. The whiteSpace constraint is set to "preserve", which means that the XML processor WILL NOT remove any white space characters:

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 

This example also defines an element called "address" with a restriction. The whiteSpace constraint is set to "replace", which means that the XML processor WILL REPLACE all white space characters (line feeds, tabs, spaces, and carriage returns) with spaces:

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 

This example also defines an element called "address" with a restriction. The whiteSpace constraint is set to "collapse", which means that the XML processor WILL REMOVE all white space characters (line feeds, tabs, spaces, carriage returns are replaced with spaces, leading and trailing spaces are removed, and multiple spaces are reduced to a single space):

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 


Restrictions on Length

To limit the length of a value in an element, we would use the length, maxLength, and minLength constraints.

This example defines an element called "password" with a restriction. The value must be exactly eight characters:

<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 

This example defines another element called "password" with a restriction. The value must be minimum five characters and maximum eight characters:

<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element> 


Restrictions for Datatypes

Constraint Description
enumeration Defines a list of acceptable values
fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero
maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value)
maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)
minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
minLength Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
pattern Defines the exact sequence of characters that are acceptable
totalDigits Specifies the exact number of digits allowed. Must be greater than zero
whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

 

 

 

What is a Complex Element?

A complex element is an XML element that contains other elements and/or attributes.

There are four kinds of complex elements:

  • empty elements
  • elements that contain only other elements
  • elements that contain only text
  • elements that contain both other elements and text

How to Define a Complex Element

Look at this complex XML element, "employee", which contains only other elements:

<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>

We can define a complex element in an XML Schema two different ways:

1. The "employee" element can be declared directly by naming the element, like this:

<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

If you use the method described above, only the "employee" element can use the specified complex type. Note that the child elements, "firstname" and "lastname", are surrounded by the <sequence> indicator. This means that the child elements must appear in the same order as they are declared. You will learn more about indicators in the XSD Indicators chapter.

2. The "employee" element can have a type attribute that refers to the name of the complex type to use:

<xs:element name="employee" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>

 

You can also base a complex element on an existing complex element and add some elements, like this:

<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

 

Complex Types with Mixed Content

An XML element, "letter", that contains both text and other elements:

<letter>
Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>

The following schema declares the "letter" element:

<xs:element name="letter">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>

Note: To enable character data to appear between the child-elements of "letter", the mixed attribute must be set to "true". The <xs:sequence> tag means that the elements defined (name, orderid and shipdate) must appear in that order inside a "letter" element.

 

We can control HOW elements are to be used in documents with indicators.


Indicators

There are seven indicators:

Order indicators:

  • All
  • Choice
  • Sequence

Occurrence indicators:

  • maxOccurs
  • minOccurs

Group indicators:

  • Group name
  • attributeGroup name

All Indicator

The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once:

<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>

Note: When using the <all> indicator you can set the <minOccurs> indicator to 0 or 1 and the <maxOccurs> indicator can only be set to 1

 

Choice Indicator

The <choice> indicator specifies that either one child element or another can occur:

<xs:element name="person">
<xs:complexType>
<xs:choice>
<xs:element name="employee" type="employee"/>
<xs:element name="member" type="member"/>
</xs:choice>
</xs:complexType>
</xs:element>

 

Sequence Indicator

The <sequence> indicator specifies that the child elements must appear in a specific order:

<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

 

Occurrence Indicators

Occurrence indicators are used to define how often an element can occur.

Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxOccurs and minOccurs is 1.

 

maxOccurs Indicator

The <maxOccurs> indicator specifies the maximum number of times an element can occur:

<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string" maxOccurs="10"/>
</xs:sequence>
</xs:complexType>
</xs:element>

The example above indicates that the "child_name" element can occur a minimum of one time (the default value for minOccurs is 1) and a maximum of ten times in the "person" element.

 

minOccurs Indicator

The <minOccurs> indicator specifies the minimum number of times an element can occur:

<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string"
maxOccurs="10" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>

The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in the "person" element.

Tip: To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement

 

Group Indicators

Group indicators are used to define related sets of elements.

Element Groups

Element groups are defined with the group declaration, like this:

<xs:group name="groupname">
...
</xs:group>

You must define an all, choice, or sequence element inside the group declaration. The following example defines a group named "persongroup", that defines a group of elements that must occur in an exact sequence:

<xs:group name="persongroup">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
</xs:sequence>
</xs:group>

After you have defined a group, you can reference it in another definition, like this:

<xs:group name="persongroup">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
</xs:sequence>
</xs:group>
<xs:element name="person" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:group ref="persongroup"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>

Attribute Groups

Attribute groups are defined with the attributeGroup declaration, like this:

<xs:attributeGroup name="groupname">
...
</xs:attributeGroup>

The following example defines an attribute group named "personattrgroup":

<xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>

After you have defined an attribute group, you can reference it in another definition, like this:

<xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>
<xs:element name="person">
<xs:complexType>
<xs:attributeGroup ref="personattrgroup"/>
</xs:complexType>
</xs:element>

 

The <any> element enables us to extend the XML document with elements not specified by the schema!


The <any> Element

The <any> element enables us to extend the XML document with elements not specified by the schema.

The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the <any> element we can extend (after <lastname>) the content of "person" with any element:

<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>

Now we want to extend the "person" element with a "children" element. In this case we can do so, even if the author of the schema above never declared any "children" element.

Look at this schema file, called "children.xsd":

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="children">
<xs:complexType>
<xs:sequence>
<xs:element name="childname" type="xs:string"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

The XML file below (called "Myfamily.xml"), uses components from two different schemas; "family.xsd" and "children.xsd":

<?xml version="1.0" encoding="ISO-8859-1"?>
<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.com family.xsd
http://www.w3schools.com children.xsd">
<person>
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
<children>
<childname>Cecilie</childname>
</children>
</person>
<person>
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>
</persons>

The XML file above is valid because the schema "family.xsd" allows us to extend the "person" element with an optional element after the "lastname" element.

The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema.

 

The <anyAttribute> element enables us to extend the XML document with attributes not specified by the schema!


The <anyAttribute> Element

The <anyAttribute> element enables us to extend the XML document with attributes not specified by the schema.

The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the <anyAttribute> element we can add any number of attributes to the "person" element:

<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>

Now we want to extend the "person" element with a "gender" attribute. In this case we can do so, even if the author of the schema above never declared any "gender" attribute.

Look at this schema file, called "attribute.xsd":

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:attribute name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:schema>

The XML file below (called "Myfamily.xml"), uses components from two different schemas; "family.xsd" and "attribute.xsd":

<?xml version="1.0" encoding="ISO-8859-1"?>
<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.com family.xsd
http://www.w3schools.com attribute.xsd">
<person gender="female">
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
</person>
<person gender="male">
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>
</persons>

The XML file above is valid because the schema "family.xsd" allows us to add an attribute to the "person" element.

The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema.

posted @ 2007-09-10 14:15 云自无心水自闲 阅读(1011) | 评论 (0)编辑 收藏


ViewStack的大小是由其子组件的大小决定的,而ViewStack并不会在改变活动子组件的时候自动resize。
只有使用以下方法来控制ViewStack的大小。
1. 使用相同的固定值明确指定所有子组件的大小
2. 使用相同的比例值指定所有子组件的大小
3. 将ViewStack的width和height的值设置为一个固定或者值。

上述3种方法是Adobe官方文档提供的。但是这3种方法不能解决ViewStack子组件大小不一致,ViewStack不能自动调整的问题。
我最后是使用ActionScript动态解决的。
在更换ViewStack的active child之间,首先设置viewStack的大小。

<mx:ViewStack id="appStack">
        <mx:VBox id="v1"/>
        <mx:VBox id="v2"/>
</mx:ViewStack>

public function changeChild() : void {
        appStack.width = 200;
        appStack.height = 200;
        appStack.selectChild = v1;
}

我错了,使用ViewStack的resizeToContent是最好的解决办法。

posted @ 2007-08-24 20:26 云自无心水自闲 阅读(1663) | 评论 (0)编辑 收藏

Spring的关键之一就是容器,在Spring中主要是两种容器:一个是BeanFactory,一个是ApplicationContext。
容器的作用是,管理所有的bean的生命周期,从创建bean的实例开始,到最后bean的消亡。
这两种容器的作用基本相同,但是Application Context是BeanFactory的子类,增加了一些功能,所以更为强大一些,主要体现在3个方面:
1. 能解析文本消息,提供文本的国际化(I18N)。
2. ApplicationContext提供一种通用的方法来加载文件资源,比如:图像文件。
3. 能够发布事件到注册的监听器。
所以,在大多数应用中,都使用Application Context。

ApplicationContext接口的实现类有很多,但常用的有3个:
1. ClassPathXmlApplicationContext
2. FileSystemXmlApplicationContext
3. XmlWebApplicationContext

ApplicationContext和BeanFactory的另一个区别在于对singleton bean的加载上。Bean Factory延迟加载所有的bean直到getBean()的调用,而ApplicationContext稍微智能一些,预先加载所有的singleton bean。

posted @ 2007-08-23 00:12 云自无心水自闲 阅读(620) | 评论 (1)编辑 收藏

1. 安装Mysql,Maven等等。这些在网上都有详细的说明。
2. 我更改了Maven的Repository的路径,缺省是放在C:\document and settings\<user name>\.m2\repository目录下,我觉得放在C:下不好,所以更换了路径。
打开~maven/conf/setting.xml,修改<localRepository>的值。

3. 使用Maven下载appfuse
我使用的是struts所以,使用的命令是:
mvn archetype:create -DarchetypeGroupId=org.appfuse -DarchetypeArtifactId=appfuse-modular-struts -DremoteRepositories=http://static.appfuse.org/repository -DarchetypeVersion=2.0-m5 -DgroupId=com.mycompany.app -DartifactId=appfuse
这里,我把下载的目录名改为了appfuse,在appfuse.org的quick start中是使用myproject的。

4. 运行的过程中,会出错,我在两台机器上都遇到了错误。
关系不大。可以继续进行。

5. 下载源代码。
我是在Eclipse中使用Subversion下载的,可以使用mvn appfuse:full-source,但是只能下载到web下的代码,service, data等部分的代码就没有了。
Svn的Repository的地址是:https://appfuse.dev.java.net/svn/appfuse

6. Java Source Code已经尽在掌握了,只是还分布在不同的目录里。
分别是在:data,service,webapp,都在main\java目录下。

7. 开始获取jsp,配置文件等。
首先cd ~maven\repo\org\hibernate\jtidy\r8-20060801
edit jtidy-r8-20060801.pom
去掉一个重复的 <licenses> 标签.

8. 去掉mysql的root用户的密码,
update user set password=password('') where user='root'; flush privileges;

9. cd appfuse
mvn integration-test
在appfuse-snapshot1.0目录下,把jsp、image,js,css等全部复制过来
另外,还有很多配置文件,象applicationContext-dao.xml等等。
还有一个,就是library了。其中有一个要注意的是ehcache需要使用1.3.0, 如果使用1.2.X,会报
javax.servlet.ServletException: Failure when attempting to set Content-Encoding: gzip
这个错误。

10. 我是使用Eclipse的Tomcat插件的,因此,建立了一个Tomcat project
把Java源文件复制到web-inf\src下,
org.appfuse.dao
org.appfuse.model
org.appfuse.service
org.appfuse.util
org.appfuse.webapp(Webapp目录下)
另外,
common
decorators
images
scripts
styles
template
403.jsp
404.jsp
index.jsp.......
还有web-inf目录下的:
数10个配置文件和lib目录下数10个jar文件

11. 启动Tomcat插件,在浏览器中浏览:http://localhost:8080/appfuse/index.jsp
用户:admin 密码:admin
OK.

之所以,这么麻烦的折腾,主要是想在appfuse应用中,打断点,进行逐步跟踪。充分了解认知演习appfuse的细节。

posted @ 2007-08-08 22:37 云自无心水自闲 阅读(6152) | 评论 (6)编辑 收藏

 

最近做的一个程序是用Swing的,要求能够根据不同的分辨率自动调整界面上所有组件的大小。也就是说不是写死是1024×768,并且字体也需要根据大小自动变化。
我使用的工具是Netbeans,为实现动态变化,我使用了GridBagLayout。首先,新建一个类,继承JPanel。然后设置JPanel的Layout为GridBagLayout。当然,根据情况,可以和Html中的表格一样,Panel里面嵌套Panel,要点是每个Panel的Layout都设置为GridBagLayout(使用其他的Layout也可以实现这样的功能,但是个人感觉GridBagLayout最容易控制和使用)。
Layout的设置只是第一步,缺省情况下,GridBagLayout会把Panel中所有的组件排成一行,从左到右逐个排放。这时候,就要使用Customize Layout(定制布局)的功能,点击后,会再弹出一个窗口
在新的窗口中,可以拖动Panel里面的组件,象表格一样,组织安放所有的组件,相当方便。
这些步骤完成后,重要的两个属性是,填充(Fill),建议把所有组件的Fill属性,都选成Both,也就是水平和垂直方向都延伸填充。这样,Panel里面的所有组件会平铺开来,占满Panel的所有空间。那么,如何调整这些组件的大小呢?需要使用weightx和weighty这两个属性。这两属性的值使用0.0~1.0之间的小数,数越大,组件所占据的空间越大。
通过以上的设置,就可以实现组件大小随着Panel大小的变化而变化了。
那么,又如何实现字体的变化呢?这个只能通过编程实现了。但是initComponents函数里的代码都是自动生成的,如何添加自定义的代码呢。点击属性面板里的字体属性后面的小方框,在弹出的对话框里,点击高级按钮,勾选“生成初始化后的代码”,然后在文本框里,输入代码,这段自定义代码,会在每次自动生成代码的时候,添加到initComponents函数中。

posted @ 2007-07-27 20:16 云自无心水自闲 阅读(21723) | 评论 (4)编辑 收藏

 

SubVersion的官方网站中有两个版本可供下载,一个是for apaache2.0.X的,一个是for apache2.2.X的,第一个是可执行文件,在已经安装了Apache2.0.X的机器上运行后,会自动在httpd.conf文件中添加相应的内容,并自动复制模块和动态链接库到相应目录。
而for Apache2.2.X的那个是一个压缩包,需要手工在apache的httpd.conf中添加相应内容,主要是启用DAV,并增加一个location。这些步骤在网上都可以搜索得到,但是我发现,网上的很多文章都忽略了将动态链接库复制到apache的bin目录下这一个步骤,这样会导致apache http server无法启动。
需要复制的文件是:
libdb44.dll
libeay32.dll
ssleay32.dll

这些文件可以复制到D:\Program Files\Apache Software Foundation\Apache2.2\bin目录(也就是apache安装目录的bin目录)下。

posted @ 2007-07-27 19:50 云自无心水自闲 阅读(1579) | 评论 (0)编辑 收藏

原文地址:http://www.ibm.com/developerworks/java/library/j-cq08296/?ca=dgr-lnxw07JUnit4vsTestNG

JUnit到了4.0以后,增加了许多新特性,变得更加灵活易用。但是另一个也是基于Annotations的TestNG在灵活易用方面已经走在了JUnit的前面。实际上,TestNG是基于Annotation的测试框架的先驱,那么这两者之间的差别是什么呢,在这里,将对两者进行一些简单的比较。


首先两者在外观上看起来是非常相似的。使用起来都非常的简便。但是从核心设计的出发点来说,两者是不一样的。JUnit一直将自己定位于单元测试框架,也就是说用于测试单个对象。而TestNG定位于更高层次的测试,因此具备了一些JUnit所没有的功能。

A simple test case

At first glance, tests implemented in JUnit 4 and TestNG look remarkably similar. To see what I mean, take a look at the code in Listing 1, a JUnit 4 test that has a macro-fixture (a fixture that is called just once before any tests are run), which is denoted by the @BeforeClass attribute:

先来看一个简单的测试例子:
第一眼看上去,JUnit和TestNG几乎一模一样。
package test.com.acme.dona.dep;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.BeforeClass;
import org.junit.Test;

public class DependencyFinderTest {
 
private static DependencyFinder finder;

 @BeforeClass
 
public static void init() throws Exception {
  finder 
= new DependencyFinder();
 }


 @Test
 
public void verifyDependencies() 
  
throws Exception {
   String targetClss 
= 
     
"test.com.acme.dona.dep.DependencyFind";

   Filter[] filtr 
= new Filter[] 
      
new RegexPackageFilter("java|junit|org")}
;

   Dependency[] deps 
= 
      finder.findDependencies(targetClss, filtr);

   assertNotNull(
"deps was null", deps);
   assertEquals(
"should be 5 large"5, deps.length);    
 }

}

 

 

 

package test.com.acme.dona.dep;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Configuration;
import org.testng.annotations.Test;

public class DependencyFinderTest {
 
private DependencyFinder finder;

 @BeforeClass
 
private void init(){
  
this.finder = new DependencyFinder();
 }


 @Test
 
public void verifyDependencies() 
  
throws Exception {
   String targetClss 
= 
     
"test.com.acme.dona.dep.DependencyFind";

   Filter[] filtr 
= new Filter[] 
      
new RegexPackageFilter("java|junit|org")}
;

   Dependency[] deps 
= 
      finder.findDependencies(targetClss, filtr);
   
   assertNotNull(deps, 
"deps was null" );
   assertEquals(
5, deps.length, "should be 5 large");        
 }

}


仔细观察,会发现一些不一样的地方。首先,JUnit要求BeforClass的方法为static,因此finder也随之需要声明为static。另外init方法必须声明为public。而TestNG却都不需要这样做。

posted @ 2007-07-14 19:21 云自无心水自闲 阅读(2702) | 评论 (0)编辑 收藏

看一下典型的Log4j.properties

log4j.rootLogger=DEBUG, A1

log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.File=${LOG_PATH}/pi.log
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] %-5p %c - %m%n

第一行就是定义了Log4j中的层次结构的最顶端root的一些属性
log4j.rootLogger=DEBUG, A1, 声明log4j的根节点允许Debug以上级别的日志输出。其Appender的名称为:A1

从第二行开始就是对名为A1的Appender进行定义了
log4j.appender.A1=org.apache.log4j.FileAppender
这一行定义了这个appender使用的类型,这里是把日志输出到文件,目前log4j支持的appender种类不少,最常见的就是FileAppender和ConsoleAppender了。
其他还有数据库、邮件等等

log4j.appender.A1.File=${LOG_PATH}/pi.log
这一行定义了日志输出文件所在的目录和文件名称

log4j.appender.A1.layout=org.apache.log4j.PatternLayout
这一行定义日志文件所输出使用的模式

log4j.appender.A1.layout.ConversionPattern=[%t] %-5p %c - %m%n
这一行定义了日志每一行的格式

posted @ 2007-07-07 00:11 云自无心水自闲 阅读(407) | 评论 (0)编辑 收藏

Log4j

Log4j的核心是3大组件:Logger、Appender、Layout。

首先是Logger,Logger的意义在于它不象System.out,它可以根据需要屏蔽部分的Log输出,同时其他的Log输出不受影响。
一、Logger有层次结构。有了层次结构就意味着有了继承关系,也就意味着可以重用。这似乎和面向对象语言很想象。
Logger都是有名称的,而Logger的名称和Java一样,也是XXX.XXX.XXX,和Java一样的规则,相当简单。类似于Java中的Object,Logger的存在一个默认的根节点:root

Logger的名称一般这样获得:
Logger logger = Logger.getLogger("XXX.XXX.XXX");
或者
Logger logger = Logger.getLogger(this.getClass());
Log4j并没有强制要求用类名作为Logger的名称,但是这是推荐的做法。

二、有了层次结构后,就要说一下级别了,文章开始的时候就提到,Log4j的优势就在于能够根据需要过滤Log的输出,主要(不是全部)就是通过级别实现的。
Log4j把级别分为:Fatal,Error,Warn,Info,Debug。这样的区分也是经过慎重考虑的,如果引入太多的层次,会使得程序开发者在记录日志的时候,难以选择,会挑花了眼。
级别之间存在优先级的高低。
通过如下语句,输出不同级别的Log
logger.debug("...");
logger.info("...");
logger.warn("...");
logger.error("...");
logger.fatal("...");

如果Logger的级别设为Warn,那么只有级别比Warn高的语句的Log才会输出。 比如:logger.debug语句这时不起作用。

三、层次结构的继承关系现在便发挥作用了。子节点如果没有显式定义级别,那么自动继承最近的父节点的级别。这样,就不需要为每一个Logger都去定义级别了,因为至少根节点是存在的,可以从根节点中获得级别定义。

四、全局级别,可以通过设置日志的“门槛”,来实现全局强制性的级别控制。
LoggerResposity reposity = x.getLoggerResposity();
resposity.setThreshold(Level.WARN); 这完全可以在配置文件中配置。
这样,logger.info语句将不再起作用。

 

其次是Appender,Appender决定了Log究竟输出到什么地方,Log4j提供了多重输出的功能,也就是说可以为Log定义多个输出地点。
同样,层次结构在这里也发挥的威力,子节点的Logger将会继承父节点的Appender,免去了一个一个定义Appender的工作,根节点默认的Appender的Console。
当然,也可以设置不继承父节点的Appender


最后是Layout,Layout决定了Log的格式。

Log4j的配置完全可以通过编程实现,对于特别简单的应用来说,绝对是够用了。但是,对于稍微大一点的应用,把配置硬编码在程序中是不灵活的。所以,使用配置配置文件是比较好的选择。

posted @ 2007-06-23 09:11 云自无心水自闲 阅读(795) | 评论 (0)编辑 收藏

 Java在传统上感觉和硬件打交道的比较少,这部分工作用C语言的比较多。
但并不是说Java不具备这样的能力。

Sun就发布了Java和串口以及并口的开发包。但是在正常的搜索中只能找到Linux和Solaris版本的类库。
下面这个链接是Win32平台下的包,找了很久才找到。
http://javashoplm.sun.com/ECom/docs/Welcome.jsp?StoreId=22&PartDetailId=7235-javacomm-2.0-spec-oth-JSpec&SiteId=JSC&TransactionId=noreg


下面简单说一下使用的步骤。

1. 把下载包中的win32com.dll放到windows\system32目录下,文档中说是放在jdk的bin目录下,但是会报错,我后来放在system32目录下解决此问题
2. 把comm.jar和javax.comm.properties这两个文件放在类路径中,注意要放在一起。文档上是要求放在jdk的lib目录中。
如果缺少javax.comm.properties文件,就找不到任何一个串口和并口

        portList = CommPortIdentifier.getPortIdentifiers();/*不带参数的getPortIdentifiers方法获得一个枚举对象,该对象又包含了系统中管理每个端口的CommPortIdentifier对象。注意这里的端口不仅仅是指串口,也包括并口。这个方法还可以带参数。getPortIdentifiers(CommPort)获得与已经被应用程序打开的端口相对应的CommPortIdentifier对象。 getPortIdentifier(String portName)获取指定端口名(比如“COM1”)的CommPortIdentifier对象。*/

        
while (portList.hasMoreElements()) {
            portId 
= (CommPortIdentifier) portList.nextElement();
            
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)/*getPortType方法返回端口类型*/ {
                
if (portId.getName().equals("COM1"))/* 找Windows下的第一个串口*/ {
                    SimpleRead reader 
= new SimpleRead();
                }

            }

        }

最后向大家推荐一个工具:Virtual Serial Port Driver XP4
这个工具可以在机器上虚拟一对相连接的串口。
这样在一台机器上不需要其他设备,就可以进行串口程序的测试、调试工作了。

 

posted @ 2007-05-18 20:39 云自无心水自闲 阅读(7804) | 评论 (14)编辑 收藏

仅列出标题
共29页: First 上一页 15 16 17 18 19 20 21 22 23 下一页 Last