简体中文
会员名称: 登入密码: [Register] 注册 忘记密码 启用我的帐号
 
[转]Struts 2 + Spring 2 + JPA + AJAX  XML
论坛首页 »项目开发» Java开发交流
发表人 内容
pinke

[Avatar]

注册时间: 2006-11-18 22:27:35
文章: 919
离线
性别: 男
出生:1982-06-18

来自: http://cwiki.apache.org/S2WIKI/struts-2-spring-2-jpa-ajax.html
状态:解读中..(不能说是汉化,因为内容会按我的理解增加与修改)
这里我们将介绍如果在eclipse里使用Struts2,及基于spring/JPA/hibernate/struts2 ajax等标签开发.

备注: Struts 2 版本2.0.3

获取代码
你可以直接下载打包好的zip Eclipse 源码程序,再增加相应的类(lib)库到 /WebContent/WEB-INF/lib 下,最后导入Eclipse.


Prerequisites
  • Struts 2

  • Tomcat 5.5

  • Eclipse

  • Eclipse WTP

  • Hibernate Core

  • Hibernate Annotations

  • Hibernate Entity Manager

  • MySql Server

  • Mysql JDBC Driver

  • Spring 2.0

  • Tomcat

    开始之前先安装一个tomcat,具体安装方法参考相应文档.这里不做介绍.

    MySql
    安装mysql,并建立一个库,名为"quickstart" 然后运行下面的SQL脚本生成一个 "Person" 表. Later, on applicationContext.xml, we'll use 'root' as the user name and password for the database, remember to replace those values with the right ones for your database.


    Create Eclipse project
    1.Open Eclipse. Seriously, you need to open Eclipse.
    2.Click File -> New -> Project. Under the "Web" folder, select "Dynamic Web Project" and click "Next".
    3.Enter the project name, "quickstart" from here on. The project will be running inside Tomcat, so we need to create a server configuration for it.
    3.1 Under "Target Runtime", click "New", select "Apache Tomcat 5.5" and click next.
    3.2 Enter Tomcat's installation directory and select an installed JRE (1.5 is required)
    4.Now you should be back to the project creation wizard, with Tomcat as your Target Runtime. Click "Next". Select "Dynamic Web Module" and "Java" facets, and click "Finish".
    Dependencies
    Your project should contain the folders "src", "build" and "WebContent". We are going to put all the required jars under "/WebContent/WEB-INF/lib". To add files to the "lib" folder, just copy them to ${workspace}\quickstart\WebContent\WEB-INF\lib, where ${workspace} is the location of your Eclipse workspace folder. The version has been removed from the jar files.


    Right click on the project and select "Refresh" (to notify Eclipse of the jars that we just added).
    Domain
    Our domain model will consist of just a simple "Person" class with a couple of fields.

    1. Create a new class named "Person" (File -> New -> Class), and enter "quickstart.model" for the package name.
    2. Add the fields "id" (int), "firstName" (String), and lastName ("String") with their setter/getter methods.
    3. Mark your class with the "@Entity" annotation, and the "id" field with the annotations "@Id" and "@GeneratedValue".

    your class will look like:

    @Entity will let the provider know that this class can be persisted. @Id marks the "id" field as the primary key for this class. @GeneratedValue will cause the id field to be generated by the provider (Hibernate). Classes and fields are by default mapped to tables and columns with the same name, see JPA's documentation for more details.

    Person service.

    We will now write the class that will take care of CRUD operations on "Person" objects.

    1. Create a new interface (File -> New -> Interface), enter "PersonService" for the name, and "quickstart.service" for the namespace. Set its content to:


    2 . Create a new class (File -> New -> Class), enter "PersonServiceImpl" for the name and "quickstart.service" for the namespace. Set its content to:



    @PersistenceContext will make Spring inject an EntityManager into the service when it is instantiated. The @PersistenceContext annotation can be placed on the field, or on the setter method. If the class is annotated as @Transactional, Spring will make sure that its methods run inside a transaction.

    JPA configuration
    1.Create a folder named "META-INF" under the "src" folder.
    2.Create a file named "persistence.xml" under the "META-INF" folder and set its content to:
    persistence.xml

    JPA configuration can be set on this file. On this example it will be empty because the datasource configuration will be on the Spring configuration file.

    Spring
    1.Update the content of web.xml under /WebContent/WEB-INF/web.xml to:
    web.xml

    This will make the container redirect all requests to Struts "FilterDispatcher" class. "index.jsp" is set as the home page, and Spring's "ContextLoaderListener" is configured as a listener.
    1.Create a file named "applicationContext.xml" under /WebContent/WEB-INF, and set its content to:

    applicationContext.xml



    Note that the "class" attribute of the bean "personAction" is set to the name of the action class, and the "personService" bean will be passed as a parameter to the action constructor. Change the "url", "username" and "password" in the "dataSource" bean to the appropiate values for your database. For more details on the rest of the beans on this file, see Spring's documentation. The "scope" attribute is new in Spring 2, and it means that Spring will create a new PersonAction object every time an object of that type is requested. In Struts 2 a new action object is created to serve each request, that's why we need scope="prototype".

    Struts
    We will now create a simple Struts action that wraps PersonServices methods, and we will configure Struts to use Spring as the object factory.


    1. Open the new class dialog (File -> New -> Class) and enter "PersonAction" for the classname, and "quickstart.action" for the namespace. Set its content to:
    PersonAction.java


    Look mom my action is a simple POJO!
    The "Preparable" interface instructs Struts to call the "prepare" method if the "PrepareInterceptor" is applied to the action (by default, it is). The constructor of the action takes a "PersonService" as a parameter, which Spring will take care of passing when the action is instatiated.

    1.Create a new file named "struts.xml" under the "src" folder. And set its content to:

    struts.xml



    Setting "struts.objectFactory" to "spring" will force Struts to instantiate the actions using Spring, injecting all the defined dependencies on applicationContext.xml. The "class" attribute for each action alias is set to "personAction", which is the bean id that we defined on applicationContext.xml for the PersonAction class. This is all that is needed to make Struts work with Spring.

    The pages

    We only have two pages, "index.jsp" and "list.jsp". "list.jsp" returns a table with a list of the persons on the database.We have this list on a different page because we are going to add some AJAX to spicy it up.

    1 . Create a new file named "list.jsp" under /WebContent/pages/ and set its content to:
    list.jsp


    This is going to render a table with each row showing the first and last name of the person, a link to remove the person, and a link to edit. The remove link has the attribute "targets", set to "persons", which means that when the user clicks on it, an asynchronous request will be made to the "remove" action (as configured on struts.xml, "remove" points to the "remove" method in PersonAction), passing the person id as parameter.

    When the edit link is clicked on, it will publish the "/edit" topic, which will trigger a javascript function to populate the fields.

    1 Create a new file named "index.jsp" under /WebContent and set its content to:
    index.jsp


    Look mom no page refresh!
    The div "persons" will load its content asynchronously, and will show "Loading..." while while the request is on progress (you can use the "indicator" attribute for better progress feedback), you can force it to refresh clicking on the "Refresh" link. The "submit" button, will make an asynchronous request to the action "save" ("save" method on PersonAction), and will publish the topic "/save" to which we subscribed to, using "dojo.event.topic.subscribe", to clear the input fields.

    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:



    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.

     文件名称 quickstart.zip [Disk] 下载
     描述
     文件大小 10 Kbytes
     下载次数:  35 次

    这篇文章被编辑了 6 次. 最近一次更新是在 2007-05-09 11:23:55


    享我所想,乐你所乐
    [MSN] Tencent QQ:44020567
     
    论坛首页 »项目开发» Java开发交流
    前往:   
    Powered by JForum 2.1.8 © JForum Team Template: Trydone