Changes for page Your First Stateful Project
Last modified by Bastian Triller on 2021/08/07 03:59
From version 8.1
edited by Pascal Robert
on 2012/08/09 03:52
on 2012/08/09 03:52
Change comment:
There is no comment for this version
To 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
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -1,7 +5,3 @@ 1 -{{info}} 2 -Work in progress 3 -{{/info}} 4 - 5 5 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. 6 6 7 7 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. ... ... @@ -91,11 +91,11 @@ 91 91 <wo:if condition="$displayGroup.hasMultipleBatches"> 92 92 <div> 93 93 <wo:link action="$displayGroup.displayPreviousBatch">Previous</wo:link> 94 - | Batch 90 + | Batch 95 95 <wo:str value="$displayGroup.currentBatchIndex" /> 96 - of 92 + of 97 97 <wo:str value="$displayGroup.batchCount" /> 98 - | 94 + | 99 99 <wo:link action="$displayGroup.displayNextBatch">Next</wo:link> 100 100 </div> 101 101 </wo:if> ... ... @@ -102,9 +102,9 @@ 102 102 <wo:loop list="$displayGroup.displayedObjects" item="$blogEntryItem"> 103 103 <p><wo:str value="$blogEntryItem.title" /></p> 104 104 <p><wo:str value="$blogEntryItem.content" escapeHTML="false" /></p> 105 - <p>Created by 101 + <p>Created by 106 106 <wo:str value="$blogEntryItem.author.fullName" /> 107 - on 103 + on 108 108 <wo:str value="$blogEntryItem.creationDate" /> 109 109 </p> 110 110 <hr /> ... ... @@ -122,44 +122,92 @@ 122 122 123 123 {{code}} 124 124 125 - 121 +private Author _loggedAuthor; 126 126 127 127 public Session() { 128 128 this._loggedAuthor = null; 129 129 } 130 - 126 + 131 131 public Author loggedAuthor() { 132 132 return this._loggedAuthor; 133 133 } 134 - 130 + 135 135 public void setAuthor(Author loggedAuthor) { 136 136 this._loggedAuthor = loggedAuthor; 137 137 } 138 138 135 +{{/code}} 139 139 137 +Save the file. Now open Author.java and add the following method: 138 + 139 +{{code}} 140 +public static Author validateLogin(EOEditingContext editingContext, String _emailAddress) { 141 + // TODO Auto-generated method stub 142 + return null; 143 +} 140 140 {{/code}} 141 141 142 - 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**.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**. 143 143 144 -After the component have been created, open **AdminMainPage.java** and addthe following code:148 +After the component have been created, open **AdminMainPage.java** and override the content of the class with the following code: 145 145 146 146 {{code}} 147 147 152 +package your.app.components; 153 + 148 148 import your.app.Session; 155 +import your.app.model.Author; 156 +import your.app.model.BlogEntry; 149 149 158 +import com.webobjects.appserver.WOActionResults; 159 +import com.webobjects.appserver.WOContext; 160 +import com.webobjects.eoaccess.EODatabaseDataSource; 161 +import com.webobjects.eocontrol.EOEditingContext; 162 + 163 +import er.extensions.batching.ERXBatchingDisplayGroup; 164 +import er.extensions.components.ERXComponent; 165 +import er.extensions.eof.ERXEC; 166 + 167 +public class AdminMainPage extends ERXComponent { 168 + 169 + private ERXBatchingDisplayGroup<BlogEntry> _dg; 170 + 171 + public AdminMainPage(WOContext context) { 172 + super(context); 173 + EODatabaseDataSource dataSource = new EODatabaseDataSource(editingContext(), BlogEntry.ENTITY_NAME); 174 + _dg = new ERXBatchingDisplayGroup<BlogEntry>(); 175 + _dg.setNumberOfObjectsPerBatch(20); 176 + _dg.setDataSource(dataSource); 177 + _dg.setObjectArray(BlogEntry.fetchBlogEntries(editingContext(), BlogEntry.AUTHOR.eq(session().loggedAuthor()), BlogEntry.LAST_MODIFIED.descs())); 178 + } 179 + 180 + public ERXBatchingDisplayGroup<BlogEntry> displayGroup() { 181 + return this._dg; 182 + } 183 + 150 150 private String _emailAddress; 151 - 185 + 152 152 public String emailAddress() { 153 153 return this._emailAddress; 154 154 } 155 - 189 + 156 156 public void setEmailAddress(String emailAddress) { 157 157 this._emailAddress = emailAddress; 158 158 } 159 159 194 + private BlogEntry _blogEntryItem; 195 + 196 + public void setBlogEntryItem(BlogEntry blogEntryItem) { 197 + this._blogEntryItem = blogEntryItem; 198 + } 199 + 200 + public BlogEntry blogEntryItem() { 201 + return this._blogEntryItem; 202 + } 203 + 160 160 @Override 161 - public WOSession session() {162 - return ((Session)session()); 205 + public Session session() { 206 + return ((Session)super.session()); 163 163 } 164 164 165 165 public boolean isLogged() { ... ... @@ -166,4 +166,221 @@ 166 166 return ((session()).loggedAuthor() == null) ? false: true; 167 167 } 168 168 213 + private EOEditingContext _ec; 214 + 215 + public EOEditingContext editingContext() { 216 + if (_ec == null) { 217 + _ec = ERXEC.newEditingContext(); 218 + } 219 + return _ec; 220 + } 221 + 222 + private String _errorMessage = null; 223 + 224 + public String errorMessage() { 225 + return this._errorMessage; 226 + } 227 + 228 + public WOActionResults login() { 229 + Author loggedAuthor = Author.validateLogin(editingContext(), _emailAddress); 230 + if (loggedAuthor != null) { 231 + session().setAuthor(loggedAuthor); 232 + } else { 233 + _errorMessage = "Invalid email address"; 234 + } 235 + return null; 236 + } 237 +} 238 + 169 169 {{/code}} 240 + 241 +You will notice that we are using a ERXBatchingDisplayGroup again. But this time, when we call dg.setObjectArray//, we set the array of objects so that only the blog entries created by the logged author are displayed.// 242 + 243 +Open **AdminMainPage.wo** and override all the content between the <body> tag to be: 244 + 245 +{{code}} 246 + 247 +<wo:AjaxUpdateContainer id="main"> 248 + <wo:if condition="$isLogged"> 249 + <wo:if condition="$displayGroup.hasMultipleBatches"> 250 + <div> 251 + <wo:link action="$displayGroup.displayPreviousBatch">Previous</wo:link> 252 + | Batch 253 + <wo:str value="$displayGroup.currentBatchIndex" /> 254 + of 255 + <wo:str value="$displayGroup.batchCount" /> 256 + | 257 + <wo:link action="$displayGroup.displayNextBatch">Next</wo:link> 258 + </div> 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> 278 + </wo:if> 279 + <wo:else> 280 + <wo:if condition="$errorMessage"> 281 + <span style="text-color: red;">Error: <wo:str value="$errorMessage" /></span> 282 + </wo:if> 283 + <wo:form> 284 + <div> 285 + <label>Email address:</label> 286 + <wo:textField value="$emailAddress" /> 287 + </div> 288 + <div><wo:AjaxSubmitButton updateContainerID="main" action="$login" value="Login" /></div> 289 + </wo:form> 290 + </wo:else> 291 + </wo:AjaxUpdateContainer> 292 + 293 +{{/code}} 294 + 295 +Last step: we need a link to the admin page. Open **Main.wo** and just before the </body> tag, add the following: 296 + 297 +{{code}} 298 + 299 +<wo:link pageName="AdminMainPage">Admin</wo:link> 300 + 301 +{{/code}} 302 + 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 + 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]].