六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 38|回复: 0

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

[复制链接]

升级  33.67%

83

主题

83

主题

83

主题

举人

Rank: 3Rank: 3

积分
301
 楼主| 发表于 2013-1-29 11:42:00 | 显示全部楼层 |阅读模式
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
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表