Changes for page Your First Stateful Project
Last modified by Bastian Triller on 2021/08/07 03:59
From version 16.1
edited by Pascal Robert
on 2012/12/12 07:59
on 2012/12/12 07:59
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,18 +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 - Author user = Author.fetchAuthor(ERXEC.newEditingContext(), Author.EMAIL.eq(_emailAddress)); 142 - return user; 143 -} 144 - 145 -{{/code}} 146 - 147 -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**. 148 - 149 149 After the component have been created, open **AdminMainPage.java** and override the content of the class with the following code: 150 150 151 151 {{code}} ... ... @@ -166,7 +166,7 @@ 166 166 import er.extensions.eof.ERXEC; 167 167 168 168 public class AdminMainPage extends ERXComponent { 169 - 163 + 170 170 private ERXBatchingDisplayGroup<BlogEntry> _dg; 171 171 172 172 public AdminMainPage(WOContext context) { ... ... @@ -177,42 +177,42 @@ 177 177 _dg.setDataSource(dataSource); 178 178 _dg.setObjectArray(BlogEntry.fetchBlogEntries(editingContext(), BlogEntry.AUTHOR.eq(session().loggedAuthor()), BlogEntry.LAST_MODIFIED.descs())); 179 179 } 180 - 174 + 181 181 public ERXBatchingDisplayGroup<BlogEntry> displayGroup() { 182 182 return this._dg; 183 183 } 184 - 178 + 185 185 private String _emailAddress; 186 - 180 + 187 187 public String emailAddress() { 188 188 return this._emailAddress; 189 189 } 190 - 184 + 191 191 public void setEmailAddress(String emailAddress) { 192 192 this._emailAddress = emailAddress; 193 193 } 194 - 188 + 195 195 private BlogEntry _blogEntryItem; 196 196 197 197 public void setBlogEntryItem(BlogEntry blogEntryItem) { 198 198 this._blogEntryItem = blogEntryItem; 199 199 } 200 - 194 + 201 201 public BlogEntry blogEntryItem() { 202 202 return this._blogEntryItem; 203 203 } 204 - 198 + 205 205 @Override 206 206 public Session session() { 207 207 return ((Session)super.session()); 208 208 } 209 - 203 + 210 210 public boolean isLogged() { 211 211 return ((session()).loggedAuthor() == null) ? false: true; 212 212 } 213 - 207 + 214 214 private EOEditingContext _ec; 215 - 209 + 216 216 public EOEditingContext editingContext() { 217 217 if (_ec == null) { 218 218 _ec = ERXEC.newEditingContext(); ... ... @@ -219,13 +219,13 @@ 219 219 } 220 220 return _ec; 221 221 } 222 - 216 + 223 223 private String _errorMessage = null; 224 - 218 + 225 225 public String errorMessage() { 226 226 return this._errorMessage; 227 227 } 228 - 222 + 229 229 public WOActionResults login() { 230 230 Author loggedAuthor = Author.validateLogin(editingContext(), _emailAddress); 231 231 if (loggedAuthor != null) { ... ... @@ -234,7 +234,7 @@ 234 234 _errorMessage = "Invalid email address"; 235 235 } 236 236 return null; 237 - } 231 + } 238 238 } 239 239 240 240 {{/code}} ... ... @@ -245,37 +245,30 @@ 245 245 246 246 {{code}} 247 247 248 -<wo:AjaxUpdateContainer id="main"> 242 + 243 + <wo:AjaxUpdateContainer id="main"> 249 249 <wo:if condition="$isLogged"> 250 250 <wo:if condition="$displayGroup.hasMultipleBatches"> 251 251 <div> 252 252 <wo:link action="$displayGroup.displayPreviousBatch">Previous</wo:link> 253 - | Batch 248 + | Batch 254 254 <wo:str value="$displayGroup.currentBatchIndex" /> 255 - of 250 + of 256 256 <wo:str value="$displayGroup.batchCount" /> 257 - | 252 + | 258 258 <wo:link action="$displayGroup.displayNextBatch">Next</wo:link> 259 259 </div> 260 260 </wo:if> 261 - <table> 262 - <tr> 263 - <th><wo:WOSortOrder displayGroup="$displayGroup" key="title" /> Title</th> 264 - <th>Author</th> 265 - <th><wo:WOSortOrder displayGroup="$displayGroup" key="creationDate" /> Created on</th> 266 - <th><wo:WOSortOrder displayGroup="$displayGroup" key="lastModified" /> Last modified</th> 267 - </tr> 268 - <wo:loop list="$displayGroup.displayedObjects" item="$blogEntryItem"> 269 - <tr> 270 - <td> 271 - <wo:str value="$blogEntryItem.title" /> 272 - </td> 273 - <td> <wo:str value="$blogEntryItem.author.fullName" /> </td> 274 - <td> <wo:str value="$blogEntryItem.creationDate" dateformat="%Y/%m/%d" /> </td> 275 - <td> <wo:str value="$blogEntryItem.lastModified" dateformat="%Y/%m/%d" /> </td> 276 - </tr> 277 - </wo:loop> 278 - </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> 279 279 </wo:if> 280 280 <wo:else> 281 281 <wo:if condition="$errorMessage"> ... ... @@ -297,7 +297,7 @@ 297 297 298 298 {{code}} 299 299 300 -<wo:link pageName="AdminMainPage">Admin</wo:link> 287 + <wo:link pageName="AdminMainPage">Admin</wo:link> 301 301 302 302 {{/code}} 303 303 ... ... @@ -304,128 +304,3 @@ 304 304 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. 305 305 306 306 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. 307 - 308 -Create a new component, and name it **EditBlogEntry**. Open **EditBlogEntry.java** and override the code with: 309 - 310 -{{code}} 311 - 312 -package your.app.components; 313 - 314 -import your.app.Session; 315 -import your.app.model.Author; 316 -import your.app.model.BlogEntry; 317 - 318 -import com.webobjects.appserver.WOActionResults; 319 -import com.webobjects.appserver.WOContext; 320 -import com.webobjects.eocontrol.EOEditingContext; 321 - 322 -import er.extensions.components.ERXComponent; 323 -import er.extensions.eof.ERXEC; 324 -import er.extensions.eof.ERXEOControlUtilities; 325 - 326 -public class EditBlogEntry extends ERXComponent { 327 - 328 - public EditBlogEntry(WOContext context) { 329 - super(context); 330 - } 331 - 332 - private BlogEntry _blogEntry; 333 - 334 - public BlogEntry blogEntry() { 335 - return this._blogEntry; 336 - } 337 - 338 - public void setBlogEntry(BlogEntry blogEntry) { 339 - if (blogEntry == null) { 340 - this._blogEntry = ERXEOControlUtilities.createAndInsertObject(editingContext(), BlogEntry.class); 341 - Author localUser = ERXEOControlUtilities.localInstanceOfObject(editingContext(), session().loggedAuthor()); 342 - this._blogEntry.setAuthorRelationship(localUser); 343 - } else { 344 - this._blogEntry = ERXEOControlUtilities.localInstanceOfObject(editingContext(), blogEntry); 345 - } 346 - } 347 - 348 - private EOEditingContext _ec; 349 - 350 - public EOEditingContext editingContext() { 351 - if (_ec == null) { 352 - _ec = ERXEC.newEditingContext(); 353 - } 354 - return _ec; 355 - } 356 - 357 - @Override 358 - public Session session() { 359 - return ((Session)super.session()); 360 - } 361 - 362 - public WOActionResults save() { 363 - editingContext().saveChanges(); 364 - return pageWithName(AdminMainPage.class); 365 - } 366 -} 367 - 368 -{{/code}} 369 - 370 -Open **EditBlogEntry.wo** and between the <body> tag, add the following: 371 - 372 -{{code}} 373 - 374 -<wo:form> 375 - <div> 376 - <label>Title:</label> 377 - <wo:textfield value="$blogEntry.title" /> 378 - </div> 379 - <div> 380 - <label>Content:</label> 381 - <wo:text value="$blogEntry.content" rows="20" cols="80" /> 382 - </div> 383 - <div>Author: <wo:str value="$session.loggedAuthor.fullName" /></div> 384 - <div><wo:submitButton action="$save" value="Save changes" /></div> 385 - </wo:form> 386 - 387 -{{/code}} 388 - 389 -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: 390 - 391 -{{code}} 392 - 393 -public WOActionResults editBlogEntry() { 394 - EditBlogEntry nextPage = pageWithName(EditBlogEntry.class); 395 - nextPage.setBlogEntry(_blogEntryItem); 396 - return nextPage; 397 - } 398 - 399 - public WOActionResults createBlogEntry() { 400 - EditBlogEntry nextPage = pageWithName(EditBlogEntry.class); 401 - nextPage.setBlogEntry(null); 402 - return nextPage; 403 - } 404 - 405 -{{/code}} 406 - 407 -Open **AdminMainPage.wo** and just after <wo:if condition="$isLogged">, add the following line: 408 - 409 -{{code}} 410 - 411 -<div><wo:link action="$createBlogEntry">Create a new blog entry</wo:link></div> 412 - 413 -{{/code}} 414 - 415 -Find this line: 416 - 417 -{{code}} 418 - 419 -<wo:str value="$blogEntryItem.title" /> 420 - 421 -{{/code}} 422 - 423 -and replace it with: 424 - 425 -{{code}} 426 - 427 -<wo:link action="$editBlogEntry"><wo:str value="$blogEntryItem.title" /></wo:link> 428 - 429 -{{/code}} 430 - 431 -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]].