Struts 2 + Spring 2 + JPA + AJAX (五)

Validation

Because we don't want any John Doe on our database, we will add some basic client side validation to our form. In Struts 2, validation can be placed on xml files with the name pattern ActionName-validation.xml, located on the same package as the action. To add validation to an specific alias of an action (like a method), the validation file name follows the pattern ActionName-alias-validation.xml, where "alias" is the action alias name (in this case a method name, "save"). Add a file named "PersonAction-save-validation.xml" under /src/quickstart/action, and set its content to:

<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
    <field name="person.firstName">
        <field-validator type="requiredstring">
            <message>First name is required!</message>
        </field-validator>
    </field>
    <field name="person.lastName">
        <field-validator type="requiredstring">
            <message>Last name is required!</message>
        </field-validator>
    </field>
</validators>

See the Struts documentation for details on existing validators, and how to write, and plug in, your own validators.

To run the project, Right click on your project and Run As -> Run on Server. You can debug it on the same way, Right click on the project and Debug As -> Debug on Server. Download and install Struts 2 Showcase to see more examples.
Using Toplink Essentials instead of Hibernate

   1. Add this to pom.xml

      <repositories>
           <repository>
               <id>java.net</id>
               <url>https://maven-repository.dev.java.net/nonav/repository</url>
               <layout>legacy</layout>
           </repository>
       </repositories>

   2. Add this to the dependencies node in pom.xml

      <dependency>
           <groupId>toplink.essentials</groupId>
           <artifactId>toplink-essentials</artifactId>
           <version>2.0-38</version>
           <exclusions>
               <exclusion>
                   <groupId>javax.transaction</groupId>
                   <artifactId>jta</artifactId>
               </exclusion>
           </exclusions>
      </dependency>

   3. Replace the jpaVendorAdapter element in applicationContext.xml with this:

      <property name="jpaVendorAdapter">
          <bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
              <property name="databasePlatform" value="oracle.toplink.essentials.platform.database.MySQL4Platform" />
              <property name="generateDdl" value="true" />
              <property name="showSql" value="true" />
          </bean>
      </property>

References

Struts
Spring JPA Doc
JPA and Spring Tutorial
Eclipse Dali

你可能感兴趣的:(spring,xml,Ajax,struts,jpa)