Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
HibernateRelationshi...




JSPWiki v2.2.33

[RSS]


Hide Menu

HibernateRelationshipsUI


This is version 19. It is not the current version, and thus it cannot be edited.
[Back to current version]   [Restore this version]


Part II: Create Weblog UI - A HowTo for creating a UI (in Struts) for managing the DAOs created in the Hibernate Relationships tutorial. I've eliminated creating managers in this tutorial for a couple reasons: 1) to reduce the amount of code and 2) to show you that you don't have to create them.

About this Tutorial

In this section, you'll create a UI that has the following features.
  • Edit a User and their Weblogs (many-to-many)
  • CRUD a Weblog and its Users (many-to-many)
  • CRUD a Weblog and its Entries (one-to-many)
  • Add a Category drop-down for Entries (many-to-one)

Table of Contents

  • [1] Modify userForm.jsp to allow creating/editing a weblog (many-to-many)
  • [2] Modify the UserAction to support editing a user's weblogs
  • [3] Create a WeblogAction to handle CRUD-ing Weblogs
  • [4] Create JSPs to display a Weblog's information
  • [5] Add the ability to edit users from the Weblog Detail Screen
  • [6] Add the ability to edit entries from the Weblog Detail Screen

Modify userForm.jsp to allow creating/editing a weblog [#1]

In order for the Struts version of AppFuse to allow editing of a Weblog's attributes from the User object, you need to modify User.java to support indexed properties. You can do this by adding indexedProperties="true" to the @struts.form tag in User.java.


* @struts.form include-all="true" extends="BaseForm" indexedProperties="true"

NOTE: While you're at it, you might as well add this same line to Weblog.java. You can also add it to Entry.java and Category.java, but you don't need indexedProperties="true".

A weblog record has 3 fields: weblogId, blogTitle and dateCreated. The blogTitle is the only field that users should be able to edit. The rest of them can be set programmatically. Open web/pages/userForm.jsp and add the following code to the bottom of the form (between </table> and </form>.

<fieldset style="position: absolute; top: 190px; left: 520px">
    <legend style="font-weight: bold">Weblogs</legend>
    <c:forEach var="blog" items="${userForm.weblogs}" varStatus="index">
        <input type="hidden" name="weblogs[<c:out value='${index.index}'/>].weblogId" value="<c:out value='${blog.weblogId}'/>"/>
        <input type="hidden" name="weblogs[<c:out value='${index.index}'/>].username" value="<c:out value='${blog.username}'/>"/>
        <input type="hidden" name="weblogs[<c:out value='${index.index}'/>].dateCreated" value="<fmt:formatDate value='${blog.dateCreated}' pattern="MM/dd/yyyy"/>"/>
        <input type="text" name="weblogs[<c:out value='${index.index}'/>].blogTitle" size="40" value="<c:out value='${blog.blogTitle}'/>"/><br/>
    </c:forEach>

</fieldset>

Run ant clean deploy, go to http://localhost:8080/appfuse/editProfile.html and login as "tomcat/tomcat". You should see a screenshot like the one below.

UserWithWeblog.png

If you try to save at this point, you'll probably get the following error:

Warning The process did not complete. Details should follow.
Warning IllegalArgumentException occurred calling getter of org.appfuse.model.Weblog.weblogId
Warning object is not an instance of declaring class

This happens because the "weblogs" property of User is populated with a bunch of WeblogForm objects, instead of Weblog objects.

Modify the UserAction to support editing a user's weblogs [#2]

To fix saving a User, add convertLists(user) to the save() method of UserAction.java:


        convertLists(user);

        try {
            mgr.saveUser(user);

In addition, you can add convertLists(user) to UserAction.edit() and get rid of the <fmt:formatDate> tag around the dateCreated property.

NOTE: The reason convertLists(Object) isn't called automatically by the convert(Object) method is because it invokes lazy-loaded collections. APF-81

Create a WeblogAction to handle CRUD-ing Weblogs [#3]

  1. Add WEBLOG_LIST and WEBLOG_KEY to Constants.java
  2. Create WeblogAction.java

Create JSPs to display a Weblog's information [#4]

  1. Add keys to ApplicationResources.properties
  2. Create weblogList.jsp
  3. Create weblogForm.jsp
  4. Add WeblogMenu to menu-config.xml and menu.jsp
  5. Add @struts.validator type="required" to weblogForm.jsp to prevent Struts Exception
  6. Change Weblog.users to List from Set
  7. Make WeblogDAO Transactional by moving bean definition to -service.xml and wrapping it with a txProxy
  8. Add logic to WeblogAction to update User's Roles
  9. Insert record into database to show 2 users: insert into weblog_user values ('mraible', 1);
  10. add class="tallCell" to pretty up view of users
WeblogWithUsers.png

Add the ability to edit users from the Weblog Detail Screen [#5]

Add the ability to edit entries from the Weblog Detail Screen [#6]


Attachments:
WeblogWithUsers.png Info on WeblogWithUsers.png 12462 bytes
WeblogAction.java Info on WeblogAction.java 5803 bytes
weblogList.jsp Info on weblogList.jsp 1483 bytes
WeblogWithEntry.png Info on WeblogWithEntry.png 14386 bytes
WeblogCRUD.png Info on WeblogCRUD.png 38859 bytes
UserWithWeblog.png Info on UserWithWeblog.png 10692 bytes
weblogForm.jsp Info on weblogForm.jsp 1782 bytes
WeblogWithCategory.png Info on WeblogWithCategory.png 15572 bytes


Go to top   More info...   Attach file...
This particular version was published on 06-Nov-2006 13:52:47 MST by MattRaible.