Changes for page Your First Stateful Project
Last modified by Bastian Triller on 2021/08/07 03:59
From version 14.1
edited by Pascal Robert
on 2012/12/12 07:57
on 2012/12/12 07:57
Change comment:
There is no comment for this version
To version 11.1
edited by Pascal Robert
on 2012/08/09 04:05
on 2012/08/09 04:05
Change comment:
There is no comment for this version
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -1,3 +1,7 @@ 1 +{{info}} 2 +Work in progress 3 +{{/info}} 4 + 1 1 So far, we have seen two of the technologies, D2W and ERRest, that Project Wonder offers for viewing and managing the data. In this tutorial, we will show how to do it with the "stateful" way of doing things. Stateful have been around since the beginning of WebObjects in 1996, so it's the oldest way of presenting data and constructing pages. 2 2 3 3 Stateful means that you don't have to worry about creating sessions and keeping track of data coming from HTML input fields and controls. In fact, D2W is also stateful. ... ... @@ -134,17 +134,8 @@ 134 134 135 135 {{/code}} 136 136 137 -Save the file. N ow openAuthor.javaand addthe followingmethod:141 +Save the file. Next: we need to add a component to present the login form to the user. Right-click on the **Components** folder in the project, and select **New** > **WOComponent**. Change the name of the component to be **AdminMainPage** and change the superclass to **er.extensions.components.ERXComponent**. 138 138 139 -{{code}} 140 -public static Author validateLogin(EOEditingContext editingContext, String _emailAddress) { 141 - // TODO Auto-generated method stub 142 - return null; 143 -} 144 -{{/code}} 145 - 146 -Next: we need to add a component to present the login form to the user. Right-click on the **Components** folder in the project, and select **New** > **WOComponent**. Change the name of the component to be **AdminMainPage** and change the superclass to **er.extensions.components.ERXComponent**. 147 - 148 148 After the component have been created, open **AdminMainPage.java** and override the content of the class with the following code: 149 149 150 150 {{code}} ... ... @@ -165,7 +165,7 @@ 165 165 import er.extensions.eof.ERXEC; 166 166 167 167 public class AdminMainPage extends ERXComponent { 168 - 163 + 169 169 private ERXBatchingDisplayGroup<BlogEntry> _dg; 170 170 171 171 public AdminMainPage(WOContext context) { ... ... @@ -176,42 +176,42 @@ 176 176 _dg.setDataSource(dataSource); 177 177 _dg.setObjectArray(BlogEntry.fetchBlogEntries(editingContext(), BlogEntry.AUTHOR.eq(session().loggedAuthor()), BlogEntry.LAST_MODIFIED.descs())); 178 178 } 179 - 174 + 180 180 public ERXBatchingDisplayGroup<BlogEntry> displayGroup() { 181 181 return this._dg; 182 182 } 183 - 178 + 184 184 private String _emailAddress; 185 - 180 + 186 186 public String emailAddress() { 187 187 return this._emailAddress; 188 188 } 189 - 184 + 190 190 public void setEmailAddress(String emailAddress) { 191 191 this._emailAddress = emailAddress; 192 192 } 193 - 188 + 194 194 private BlogEntry _blogEntryItem; 195 195 196 196 public void setBlogEntryItem(BlogEntry blogEntryItem) { 197 197 this._blogEntryItem = blogEntryItem; 198 198 } 199 - 194 + 200 200 public BlogEntry blogEntryItem() { 201 201 return this._blogEntryItem; 202 202 } 203 - 198 + 204 204 @Override 205 205 public Session session() { 206 206 return ((Session)super.session()); 207 207 } 208 - 203 + 209 209 public boolean isLogged() { 210 210 return ((session()).loggedAuthor() == null) ? false: true; 211 211 } 212 - 207 + 213 213 private EOEditingContext _ec; 214 - 209 + 215 215 public EOEditingContext editingContext() { 216 216 if (_ec == null) { 217 217 _ec = ERXEC.newEditingContext(); ... ... @@ -218,13 +218,13 @@ 218 218 } 219 219 return _ec; 220 220 } 221 - 216 + 222 222 private String _errorMessage = null; 223 - 218 + 224 224 public String errorMessage() { 225 225 return this._errorMessage; 226 226 } 227 - 222 + 228 228 public WOActionResults login() { 229 229 Author loggedAuthor = Author.validateLogin(editingContext(), _emailAddress); 230 230 if (loggedAuthor != null) { ... ... @@ -233,7 +233,7 @@ 233 233 _errorMessage = "Invalid email address"; 234 234 } 235 235 return null; 236 - } 231 + } 237 237 } 238 238 239 239 {{/code}} ... ... @@ -244,37 +244,30 @@ 244 244 245 245 {{code}} 246 246 247 -<wo:AjaxUpdateContainer id="main"> 242 + 243 + <wo:AjaxUpdateContainer id="main"> 248 248 <wo:if condition="$isLogged"> 249 249 <wo:if condition="$displayGroup.hasMultipleBatches"> 250 250 <div> 251 251 <wo:link action="$displayGroup.displayPreviousBatch">Previous</wo:link> 252 - | Batch 248 + | Batch 253 253 <wo:str value="$displayGroup.currentBatchIndex" /> 254 - of 250 + of 255 255 <wo:str value="$displayGroup.batchCount" /> 256 - | 252 + | 257 257 <wo:link action="$displayGroup.displayNextBatch">Next</wo:link> 258 258 </div> 259 259 </wo:if> 260 - <table> 261 - <tr> 262 - <th><wo:WOSortOrder displayGroup="$displayGroup" key="title" /> Title</th> 263 - <th>Author</th> 264 - <th><wo:WOSortOrder displayGroup="$displayGroup" key="creationDate" /> Created on</th> 265 - <th><wo:WOSortOrder displayGroup="$displayGroup" key="lastModified" /> Last modified</th> 266 - </tr> 267 - <wo:loop list="$displayGroup.displayedObjects" item="$blogEntryItem"> 268 - <tr> 269 - <td> 270 - <wo:str value="$blogEntryItem.title" /> 271 - </td> 272 - <td> <wo:str value="$blogEntryItem.author.fullName" /> </td> 273 - <td> <wo:str value="$blogEntryItem.creationDate" dateformat="%Y/%m/%d" /> </td> 274 - <td> <wo:str value="$blogEntryItem.lastModified" dateformat="%Y/%m/%d" /> </td> 275 - </tr> 276 - </wo:loop> 277 - </table> 256 + <wo:loop list="$displayGroup.displayedObjects" item="$blogEntryItem"> 257 + <p><wo:str value="$blogEntryItem.title" /></p> 258 + <p><wo:str value="$blogEntryItem.content" escapeHTML="false" /></p> 259 + <p>Created by 260 + <wo:str value="$blogEntryItem.author.fullName" /> 261 + on 262 + <wo:str value="$blogEntryItem.creationDate" /> 263 + </p> 264 + <hr /> 265 + </wo:loop> 278 278 </wo:if> 279 279 <wo:else> 280 280 <wo:if condition="$errorMessage"> ... ... @@ -296,7 +296,7 @@ 296 296 297 297 {{code}} 298 298 299 -<wo:link pageName="AdminMainPage">Admin</wo:link> 287 + <wo:link pageName="AdminMainPage">Admin</wo:link> 300 300 301 301 {{/code}} 302 302 ... ... @@ -303,128 +303,3 @@ 303 303 Save everything, run the app and try to login. If login is not successful, you will get an error message. If login is valid, you will see the blog entries that you created. 304 304 305 305 For the last part of this tutorial, we are going to add a link on each blog entry in the list that will bring us to a edit page where we can modify a blog entry. We are also going to add a link to create a new blog entry. 306 - 307 -Create a new component, and name it **EditBlogEntry**. Open **EditBlogEntry.java** and override the code with: 308 - 309 -{{code}} 310 - 311 -package your.app.components; 312 - 313 -import your.app.Session; 314 -import your.app.model.Author; 315 -import your.app.model.BlogEntry; 316 - 317 -import com.webobjects.appserver.WOActionResults; 318 -import com.webobjects.appserver.WOContext; 319 -import com.webobjects.eocontrol.EOEditingContext; 320 - 321 -import er.extensions.components.ERXComponent; 322 -import er.extensions.eof.ERXEC; 323 -import er.extensions.eof.ERXEOControlUtilities; 324 - 325 -public class EditBlogEntry extends ERXComponent { 326 - 327 - public EditBlogEntry(WOContext context) { 328 - super(context); 329 - } 330 - 331 - private BlogEntry _blogEntry; 332 - 333 - public BlogEntry blogEntry() { 334 - return this._blogEntry; 335 - } 336 - 337 - public void setBlogEntry(BlogEntry blogEntry) { 338 - if (blogEntry == null) { 339 - this._blogEntry = ERXEOControlUtilities.createAndInsertObject(editingContext(), BlogEntry.class); 340 - Author localUser = ERXEOControlUtilities.localInstanceOfObject(editingContext(), session().loggedAuthor()); 341 - this._blogEntry.setAuthorRelationship(localUser); 342 - } else { 343 - this._blogEntry = ERXEOControlUtilities.localInstanceOfObject(editingContext(), blogEntry); 344 - } 345 - } 346 - 347 - private EOEditingContext _ec; 348 - 349 - public EOEditingContext editingContext() { 350 - if (_ec == null) { 351 - _ec = ERXEC.newEditingContext(); 352 - } 353 - return _ec; 354 - } 355 - 356 - @Override 357 - public Session session() { 358 - return ((Session)super.session()); 359 - } 360 - 361 - public WOActionResults save() { 362 - editingContext().saveChanges(); 363 - return pageWithName(AdminMainPage.class); 364 - } 365 -} 366 - 367 -{{/code}} 368 - 369 -Open **EditBlogEntry.wo** and between the <body> tag, add the following: 370 - 371 -{{code}} 372 - 373 -<wo:form> 374 - <div> 375 - <label>Title:</label> 376 - <wo:textfield value="$blogEntry.title" /> 377 - </div> 378 - <div> 379 - <label>Content:</label> 380 - <wo:text value="$blogEntry.content" rows="20" cols="80" /> 381 - </div> 382 - <div>Author: <wo:str value="$session.loggedAuthor.fullName" /></div> 383 - <div><wo:submitButton action="$save" value="Save changes" /></div> 384 - </wo:form> 385 - 386 -{{/code}} 387 - 388 -We now have a form to edit or create a blog entry. Save the component and the Java class, and open **AdminMainPage.java** to add the following code: 389 - 390 -{{code}} 391 - 392 -public WOActionResults editBlogEntry() { 393 - EditBlogEntry nextPage = pageWithName(EditBlogEntry.class); 394 - nextPage.setBlogEntry(_blogEntryItem); 395 - return nextPage; 396 - } 397 - 398 - public WOActionResults createBlogEntry() { 399 - EditBlogEntry nextPage = pageWithName(EditBlogEntry.class); 400 - nextPage.setBlogEntry(null); 401 - return nextPage; 402 - } 403 - 404 -{{/code}} 405 - 406 -Open **AdminMainPage.wo** and just after <wo:if condition="$isLogged">, add the following line: 407 - 408 -{{code}} 409 - 410 -<div><wo:link action="$createBlogEntry">Create a new blog entry</wo:link></div> 411 - 412 -{{/code}} 413 - 414 -Find this line: 415 - 416 -{{code}} 417 - 418 -<wo:str value="$blogEntryItem.title" /> 419 - 420 -{{/code}} 421 - 422 -and replace it with: 423 - 424 -{{code}} 425 - 426 -<wo:link action="$editBlogEntry"><wo:str value="$blogEntryItem.title" /></wo:link> 427 - 428 -{{/code}} 429 - 430 -Save everything, run the app, click on the "admin" link, login and check if you can create or edit a blog entry. Everything should be working, and just created your first stateful Project Wonder application [[It's time to deploy an application>>Your First Deployment]].