Advertisement
danieleteti

DelphiMVCFramework: How to render a Spring4d ObjectList

Apr 22nd, 2016
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.86 KB | None | 0 0
  1. unit Unit5;
  2.  
  3. interface
  4.  
  5. uses
  6.   MVCFramework, Spring, Spring.Collections;
  7.  
  8. type
  9.  
  10.   [MVCPath('/')]
  11.   TMyController = class(TMVCController)
  12.   public
  13.     [MVCPath('/')]
  14.     [MVCHTTPMethod([httpGET])]
  15.     procedure Index(ctx: TWebContext);
  16.     procedure OnBeforeAction(Context: TWebContext; const AActionName: string;
  17.       var Handled: Boolean); override;
  18.     procedure OnAfterAction(Context: TWebContext;
  19.       const AActionName: string); override;
  20.   end;
  21.  
  22.   TPerson = class
  23.   private
  24.     FFirstName: String;
  25.     FLastName: String;
  26.   public
  27.     constructor Create(aName, aLastName: String);
  28.     property FirstName: String read FFirstName write FFirstName;
  29.     property LastName: String read FLastName write FLastName;
  30.   end;
  31.  
  32. implementation
  33.  
  34. uses
  35.   System.Generics.Collections;
  36.  
  37. procedure TMyController.Index(ctx: TWebContext);
  38. var
  39.   lList: IList<TPerson>;
  40.   lObjList: TObjectList<TPerson>;
  41. begin
  42.   lList := TCollections.CreateObjectList<TPerson>;
  43.   lList.Add(TPerson.Create('Daniele', 'Teti'));
  44.   lList.Add(TPerson.Create('Peter', 'Parker'));
  45.   lList.Add(TPerson.Create('Bruce', 'Banner'));
  46.  
  47.   //copy the references to a standard TObjectList<T>
  48.   lObjList := TObjectList<TPerson>.Create(false);
  49.   lObjList.AddRange(lList.ToArray);
  50.   Render<TPerson>(lObjList);
  51. end;
  52.  
  53. procedure TMyController.OnAfterAction(Context: TWebContext;
  54.   const AActionName: string);
  55. begin
  56.   { Executed after each action }
  57.   inherited;
  58. end;
  59.  
  60. procedure TMyController.OnBeforeAction(Context: TWebContext;
  61.   const AActionName: string; var Handled: Boolean);
  62. begin
  63.   { Executed before each action
  64.     if handled is true (or an exception is raised) the actual
  65.     action will not be called }
  66.   inherited;
  67. end;
  68.  
  69. { TPerson }
  70.  
  71. constructor TPerson.Create(aName, aLastName: String);
  72. begin
  73.   inherited Create;
  74.   FirstName := aName;
  75.   LastName := aLastName;
  76. end;
  77.  
  78. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement