ATG教程-DEV1 Chapter 6: Using Repository Form Handlers
Chapter 6: Using Repository Form Handlers
Catching Up
If you were unable to complete the exercises from the previous chapter
1. Complete the “Catching Up” section from Chapter 5.
2. Copy the properties and XML files in solutions/chapter05 to <ATG9.1dir>/Dynamusic/config/dynamusic.
Exercise 1: Using Repository form handlers
Exercise Objective
After completing this exercise, you will be able to use Repository Form Handlers to add, delete, and modify repository items. In addition, you will be able to use the Search Form Handler to search for repository items meeting specific criteria.
(2个目的,一个是用repository form handler来处理这些repository items
第二个目的是用Search Form Handler来寻找满足特定条件和规则的repository item)
Problem Statement
In this exercise you will be asked to create several JSPs that contain forms. As with previous exercises, we have provided html-formatted starter JSP files for your use. (These are fundamental files for you to replace some details with) In these starter files, you will find nicely formatted static form templates that use tables to control how the form fields will look on the page. As always, you may choose instead to use dynamusic-template.jsp as a “blank” starter page if you find working with the formatted HTML difficult.
(We can use the template jsp to format the html )
Although you can use the form wizard for insert forms, it is not recommended for this exercise, as it can lead to difficulties.
Guided Instruction
Implement a form to add a new artist
1. Create a new request scoped form handler component called ArtistFormHandler based on the atg.repository.servlet.RepositoryFormHandler class. Set the component’s itemDescriptorName property to artist, and link the repository property to /dynamusic/SongsRepository. Set the requireIdOnCreate property to false.
2. Edit newArtist.jsp (or create a new one using dynamusic-template.jsp).
3. Import the /dynamusic/ArtistFormHandler component (bean).
4. Locate the <form…> … </form> tags and change them to <dsp:form> … </dsp:form> tags. Change the “action” of the form tag to redirect back to the current page.
5. Your form should collect the name, photoURL, and description properties of the new artist. If you are using the mock-up starter file, locate the static fields in the form and replace them with DSP form tags which link input fields to associated item properties in the form handler. (Remember to connect each form field to the associated property using the ArtistFormHandler.value.myProperty syntax. Review the lecture notes, DAF quick reference in the course appendix or the DSP Tag Library appendix of the Page Developer’s Guide for details on DSP form tag syntax.) Make the name property required.
6. Link the submit button (<dsp:input type="submit" …>) to the appropriate submission handler method (ArtistFormHandler.create).
7. Add a hidden input field to set the createSuccessURL property to artists.jsp.
8. At the beginning of the form, handle errors by looping through the form handler’s formErrors property using the ErrorMessageForEach servlet bean. For each error, print out the message oparam.
9. Edit artists.jsp and link the text “Add Artist” (under the sidebar) to newArtist.jsp.
10. Load the Artists page and select the “Add Artist” link. Add an artist, and verify your changes.
11. Try to add an artist with no name entered on the form. Verify that the form is re-displayed with an error indicating that the “name” field is required.
Implement a form to edit or delete an artist
12. Edit editArtist.jsp. Import the /dynamusic/ArtistFormHandler component bean.
13. Create a form to allow the user to modify the name, description or photoURL of the artist whose ID is passed in by the artistId parameter. If you are using the provided mock-up, locate the static form and form fields, and replace each tag with the corresponding DSP form tag. Connect each input field with the bean property (similar to the previous exercise.) Be sure to set the “action” of the form to the current JSP.
14. Link the “Save Changes” button to the ArtistFormHandler.update method, and the “Delete this Artist” button to the ArtistFormHandler.delete method.
15. The form should modify the artist item defined by an input parameter called artistId. In order for the ArtistFormHandler to know which artist we are modifying, we need to set the repositoryId property of the form handler. Just above your form add a <dsp:setvalue> tag to set the repositoryId property of the form handler to the artistId parameter passed in from the calling page.
16. Make sure you use that parameter to set ArtistFormHandler.repositoryId via a hidden form input field. You must give this field a name, specifically artistId, in order for the form error handling to work when the page is re-displayed. (See the lecture notes for details.)
17. Edit artistDetails.jsp and link the text “Edit Artist Info” (under the sidebar) to editArtist.jsp, making sure to pass the artistId as a parameter.
18. Load an artist detail page and select the “Edit Artist Info” link. Modify the artist details and verify your changes. (You can use the ACC to verify changes; be sure to refresh your ACC to show the latest data.)
19. Edit the details for an artist, leaving the name field blank, and verify the form error handling.
20. Test the deletion function. It might fail due to foreign key violations if there are albums defined for that artist. You will address this problem with a custom form handler in Chapters 11 through 13. For now, try creating a new artist (using your previously created form or the ACC) with no albums and then deleting that artist to make sure the form works.
Optional - Implement a song search page
21. Create a session scoped component called SongSearchFormHandler based on the atg.repository.servlet.SearchFormHandler class. Set the doKeywordSearch property to true, the itemTypes property to song, and link the repositories property to /dynamusic/SongsRepository.
22. Modify search.jsp and add a form with a single text box which allows users to search for songs by keyword. Connect the text box to the keywordInput property of the form handler. Add a hidden input field to set the successURL property to searchResults.jsp (which you will create in the next step), and add a submit button to invoke the search method of the form handler.
Please note that the ACC does not provide a wizard to implement a search form, although you can still use the wizard to setup form elements such as automatic error processing.
23. Modify searchResults.jsp to display the results of the user’s search. Insert a ForEach servlet bean to loop through the results (SongSearchFormHandler.searchResults). Display the name of the song, as well as the artist, as links to the respective details pages (song.jsp and artistDetails.jsp), making sure to pass a parameter containing the repository id of, respectively, the song or the artist.
24. Load the home page and select Search. Enter a substring that matches the name or description of multiple songs in the Song Repository (“Love” is always a good choice!) and verify your changes.
posted on 2011-05-20 11:05 Eason Jiang 阅读(508) 评论(0) 编辑 收藏 举报