Last modified by Bastian Triller on 2021/08/07 03:59

From version 11.1
edited by Pascal Robert
on 2012/08/09 04:05
Change comment: There is no comment for this version
To version 14.1
edited by Pascal Robert
on 2012/12/12 07:57
Change comment: There is no comment for this version

Summary

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.
... ... @@ -138,8 +138,17 @@
138 138  
139 139  {{/code}}
140 140  
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**.
137 +Save the file. Now open Author.java and add the following method:
142 142  
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 +
143 143  After the component have been created, open **AdminMainPage.java** and override the content of the class with the following code:
144 144  
145 145  {{code}}
... ... @@ -160,7 +160,7 @@
160 160  import er.extensions.eof.ERXEC;
161 161  
162 162  public class AdminMainPage extends ERXComponent {
163 -
168 +
164 164   private ERXBatchingDisplayGroup<BlogEntry> _dg;
165 165  
166 166   public AdminMainPage(WOContext context) {
... ... @@ -171,42 +171,42 @@
171 171   _dg.setDataSource(dataSource);
172 172   _dg.setObjectArray(BlogEntry.fetchBlogEntries(editingContext(), BlogEntry.AUTHOR.eq(session().loggedAuthor()), BlogEntry.LAST_MODIFIED.descs()));
173 173   }
174 -
179 +
175 175   public ERXBatchingDisplayGroup<BlogEntry> displayGroup() {
176 176   return this._dg;
177 177   }
178 -
183 +
179 179   private String _emailAddress;
180 -
185 +
181 181   public String emailAddress() {
182 182   return this._emailAddress;
183 183   }
184 -
189 +
185 185   public void setEmailAddress(String emailAddress) {
186 186   this._emailAddress = emailAddress;
187 187   }
188 -
193 +
189 189   private BlogEntry _blogEntryItem;
190 190  
191 191   public void setBlogEntryItem(BlogEntry blogEntryItem) {
192 192   this._blogEntryItem = blogEntryItem;
193 193   }
194 -
199 +
195 195   public BlogEntry blogEntryItem() {
196 196   return this._blogEntryItem;
197 197   }
198 -
203 +
199 199   @Override
200 200   public Session session() {
201 201   return ((Session)super.session());
202 202   }
203 -
208 +
204 204   public boolean isLogged() {
205 205   return ((session()).loggedAuthor() == null) ? false: true;
206 206   }
207 -
212 +
208 208   private EOEditingContext _ec;
209 -
214 +
210 210   public EOEditingContext editingContext() {
211 211   if (_ec == null) {
212 212   _ec = ERXEC.newEditingContext();
... ... @@ -213,13 +213,13 @@
213 213   }
214 214   return _ec;
215 215   }
216 -
221 +
217 217   private String _errorMessage = null;
218 -
223 +
219 219   public String errorMessage() {
220 220   return this._errorMessage;
221 221   }
222 -
227 +
223 223   public WOActionResults login() {
224 224   Author loggedAuthor = Author.validateLogin(editingContext(), _emailAddress);
225 225   if (loggedAuthor != null) {
... ... @@ -228,7 +228,7 @@
228 228   _errorMessage = "Invalid email address";
229 229   }
230 230   return null;
231 - }
236 + }
232 232  }
233 233  
234 234  {{/code}}
... ... @@ -239,30 +239,37 @@
239 239  
240 240  {{code}}
241 241  
242 -
243 - <wo:AjaxUpdateContainer id="main">
247 +<wo:AjaxUpdateContainer id="main">
244 244   <wo:if condition="$isLogged">
245 245   <wo:if condition="$displayGroup.hasMultipleBatches">
246 246   <div>
247 247   <wo:link action="$displayGroup.displayPreviousBatch">Previous</wo:link>
248 - | Batch
252 + | Batch
249 249   <wo:str value="$displayGroup.currentBatchIndex" />
250 - of
254 + of
251 251   <wo:str value="$displayGroup.batchCount" />
252 - |
256 + |
253 253   <wo:link action="$displayGroup.displayNextBatch">Next</wo:link>
254 254   </div>
255 255   </wo:if>
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>
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>
266 266   </wo:if>
267 267   <wo:else>
268 268   <wo:if condition="$errorMessage">
... ... @@ -284,7 +284,7 @@
284 284  
285 285  {{code}}
286 286  
287 - <wo:link pageName="AdminMainPage">Admin</wo:link>
299 +<wo:link pageName="AdminMainPage">Admin</wo:link>
288 288  
289 289  {{/code}}
290 290  
... ... @@ -291,3 +291,128 @@
291 291  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.
292 292  
293 293  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]].