ATG教程-DEV1 Chapter 4: Using Repositories

Chapter 4: Using Repositories

Catching Up

Completion of previous chapters is necessary in order to continue these exercises. If you were not able to complete the previous chapter:

1. If you have already created a Dynamusic module, move it to a save location outside of the <A9.1dir> directory. Copy the Dynamusic module from solutions/chapter03 to <A6dir>.

1. Copy the CONFIG.properties file from the solutions\chapter02 directory to <A9.1dir>\Dynamusic\config.

2. Copy the FeaturedSongs.properties file from the solutions\chapter02 directory to <A9.1dir>\Dynamusic\config\dynamusic\.

3. Copy the home.jsp file from solutions\chapter03 to <A9.1dir>\Dynamusic\j2ee-apps\dynamusic-j2ee\dynamusic-web.war.

4. Copy the header.jsp and sidebar.jsp files from solutions\chapter03 to <A9.1dir>\ Dynamusic\j2ee-apps\dynamusic-j2ee\dynamusic-web.war\common.

Exercise 1: Working with repositories

Exercise Objective

After completing this exercise, you will be able to display Repository data on Java Server Pages using standard servlet beans.

Problem Statement

Create pages that display a list of artists in the Songs Repository, the artist details for a particular artist (including a list of the artist’s albums), and the album details for a particular album (including a list of the album’s songs). A pre-populated Songs Repository has been provided for your use. The Songs Repository contains three item types: song, album, and artist.

(From the song repositiory ,we should extract the related information,which are the list of artists,the artist details for a particular artist, the album details for a particular album.)

There are four pages in all in this “drill-down” section for the Songs Repository. (You will create the first three shown below; the fourth is provided for you):

artists.jsp, which lists the name of all the artists in the repository. Each artist links to:

artistDetails.jsp, which shows details about the artist selected, and a list of albums by that artist. Each album links to(作者细节)

albumDetails.jsp, which shows details about the album select, and a list of songs on that album. Each song links to:(专辑细节)

song.jsp, which shows details about the song, include a link to download the MP3(歌曲细节)

Each page in this sequence links to the next by passing an itemId parameter, containing the ID of the selected item. For example, artists.jsp passes the id of the selected artist to the artistDetails.jsp page.(which means: artists.jsp passes the id of the seleted artists to the artistDetails.jsp,and the artistDetails.jsp passes the id of the select album to the albumDetails.jsp ,and the albumDetails.jsp passes the id of the selected song to the song.jsp )

This “drill-down” technique was discussed in the lecture; refer to the lecture notes for details on how to do this.

For this exercises, most of the remaining exercises in this course, JSP starter pages have been provided containing static elements that you will replace with dynamic ATG elements.(正确,基本都是用你的动态信息区取代掉原来的静态的JSP页面) These pages are provided to save you some development time. If you prefer, you can also use an empty “template” page that has been provided for you, called dynamusic-template.jsp. You can open this page, and then go to File > Save As… to save a page with the new page to create. The area in which to insert the lab exercise content is commented.(在原来的JSP页面上,所有的插入点都已经用注释做了标记) The instructions below assume you will be modifying the starter pages, but choose whichever method you find most straightforward.

Guided Instruction
Explore the Songs Repository

1. In the ACC, select Content > SongsRepository. Spend a few minutes becoming familiar with the song, album, and artist item types.

(spend some minutes to be familiar with these item types,Because the Repository is comprised with some items)实际上是看看这些类型对应的有哪些项,这些项是存储在我们的仓库里面的。所以我们必须将他们列出来并且熟悉之

(To list these information,you only need to make a query using specific sentences)

· List items of type artist (fourteen artists)

· Select an artist and examine its properties(description ,id ,name, photoURL)

· List items of type album( a lot of albums)

· Select an album and examine its properties(artists,coverURL,date,description,id ,songlist,title,because album is made by one artist ,and it covers a lot of songs)

· List items of type song(好多歌曲)

· Select a song and examine its properties(artists ,description,downloadURL,genre(类型,风格),id ,title)

· Use the query icon to perform a complex query on the repository. For example:

items of type song whose artist’s name starts with S(what about this kind of query, is it similar with the HQL?)

他有一个向导来帮助我们来构造这些查询的表达式,这些查询包含了机会这个item type的所有属性,以便我们来进行各种不同方式的查询

(Ok ,I have found several records that meet the condition that we have supplied with it)

Create a dynamic list of artists (需要借助于repository)

2. In the ACC, select Pages and Components > J2EE Pages.

3. Double click Dynamusic: j2ee-apps/dynamusic-j2ee.

4. Edit artists.jsp (or create a new one using the dynamusic-template.jsp page).

5. Locate the static list of artists. Use the insert servlet bean button to replace the static list with an RQLQueryForEach servlet bean. (You can find RQLQueryForEach by module under Servlet Beans or by path under /atg/dynamo/droplet).[这个Servlet就是要用到repository,而这个RQL查询的基础就是某个repository,所以他必须会跳出一个让你选择repository的dialog]

When the Select Repository page is displayed, click the ellipsis button to bring up a component selection dialog box. Select the By Path view and select the /dynamusic/SongsRepository component. (You will need to go to By Path for most of the components you will be creating, whereas most ATG-supplied components can be found by module or by path.)

(/atg 之下的都是由ATG自己创建的组件,而非/atg目录之下一般是由自己创建的组件)

Query the /dynamusic/SongsRepository for ALL artist items. (Set the queryRQL parameter of the droplet to “ALL ORDER BY name”.) In the output oparam, display each artist name. If there are no artists, display an appropriate message in the empty oparam.

(进入组件之后用一些特定的RQL语句来选择所需要的repository中的项)

6. Load the Dynamusic home page, select Artists and verify that all the artists from the Songs Repository are displayed.

7. Modify your artist list to display each artist name as a hyperlink to artistDetails.jsp, passing it a parameter called itemId, set to the id of the artist. (We will work with artistDetails.jsp in a moment.)

(注意地址栏的变化:由于

<dsp:param name="itemId" param="element.id" />

<dsp:valueof param="element.name"/>

这段代码的传入,所以把itemId作为一个参数,跟在问号后面,写在了地址栏里面,

http://localhost:8080/dynamusic/artistDetails.jsp?itemId=100006

所以从这里可以看出来,这个<dsp:param> 是作为<dsp:a>这个链接的参数,由于显示的是artistDetail.jsp,但是又不完全只显示这个页面,所以要吧参数也传递进去,传递参数的方法就是这个例子中的<dsp:param>

)

8. Load the Dynamusic home page, select Artists and verify that the artists” names are now displayed as links to artistDetails.jsp.

Create a repository-based artist details page

9. In the ACC, select Pages and Componenets > Components by Path and create a globally-scoped component in the dynamusic folder called ArtistLookupDroplet based on the atg.repository.servlet.ItemLookupDroplet class. Set the itemDescriptor property to artist(itemDescriptor表示的是项的类型,比如artist,或者song 什么的) and link the repository property to /dynamusic/SongsRepository. (Review the exercises and lecture notes for Chapter 3 if you need a reminder how to create and configure a component based on an existing class.)

(这些还是比较简单的,通过向导可以很轻松的完成以上的步骤)

10. Edit artistDetails.jsp. Use your ArtistLookupDroplet (我们刚刚才创建的)to replace the static artist information with artist data from the Songs Repository. Since you have configured the ArtistLookupDroplet to query the SongsRepository and return artist repository items, all it is missing is the repository ID of the artist. Remember the itemId parameter passed in from artists.jsp? (itemId 是在artists.jsp问号后面穿进去的内容)It contains the repository id your servlet bean will use to fetch that artist. (fetch the artist )Configure the id input parameter of the ArtistLookupDroplet to use the itemId param passed in from artist.jsp. For each artist, display the name, photoURL (using <img src=…>), and description properties.

语法为:<img src=”/<dsp:valueof param=’element.photoURL’/>”/>

You’ll need to pass the artist name to header.jsp(到目前为止,我们的页面头都还是静态的home页面,所以我们要动态的给定它的内容) so that it will be displayed at the top of the page, so make sure your ArtistLookupDroplet starts at the top of the JSP, before the inclusion of the header JSP fragment. Essentially, the entire body of the page will fall within the output oparam of the servlet bean.

11. Load the Artists page. Select an artist and verify that the artist details (name, image and description) are displayed correctly.(it works Ok ,has been checked)

///The latter are

12. Within your ArtistLookupDroplet, display the albums for the given artist using a nested RQLQueryForEach servlet bean. Remember that album item types have an artist property, so your query will need to look for all albums given an artist.id. That is, your queryRQL should indicate artist.id = id of current artist passed in. (Review the lecture notes for a reminder of how to do a parameterized query.) For each album returned by the query, display the title property of the album.

Note: This is your first use of a nested droplet in the exercises. You will notice that both the outer droplet (ArtistLookupDroplet) and the inner droplet (RQLQueryForEach) have an element output parameter. This overlap does not present a conflict. Within the inner droplet’s output oparam, element will refer to an item returned by the inner droplet. (If for some reason you needed access to the outer droplet’s element parameter, you should set a new parameter to its value before calling the inner droplet.)

13. Re-load (refresh in the browser) the page. Select an artist and verify that the list of albums is displayed correctly.

14. Edit your album list and, for each album, display its title property as a hyperlink. The target page of the link will be albumDetails.jsp, and you will use the same technique as you did in for artistDetails.jsp, passing a parameter called itemId representing the ID of the album.

15. Re-load the page and make sure each album name is a link passing the correct id.

Create a repository-based album details page

16. Create a new component in the dynamusic directory called AlbumLookupDroplet based on the atg.repository.servlet.ItemLookupDroplet class. This time set the itemDescriptor property to album, and link the repository property to /dynamusic/SongsRepository.

17. Edit albumDetails.jsp. Use your AlbumLookupDroplet to replace the static album information with album data from the Songs Repository. Pass the album title to header.jsp. Display the coverURL, description, and date on the page.

18. Load an artist detail page, select an album and verify your changes.

19. You will also want to display a list of songs from the album. Note that the songList property is multi-valued (containing a Set of song item types) and will need a nested ForEach servlet bean for display. (Unlike with displaying an artist’s albums, you will not need an RQL query droplet to do this; the query is implicit in the property definition for songList.) Display each song as a hyperlink, with the target page set to song.jsp (provided for you). You will need to pass a parameter called itemId containing the id of the song item to song.jsp, which has been supplied for you.

20. Reload the page and verify your changes.

posted on 2011-05-19 12:01  Eason Jiang  阅读(470)  评论(0编辑  收藏  举报

导航