Community Implementation (Part 2)
This Tutorial shows how to implement the community/platform features. It is based on the Observer 1: Implement a CMS from Scratch and Observers Design from OSWD.
In the previous Tutorial we implemented the basic Cms features like navigation, path, displaying the content and a few fancier things.
We will now implement the advanced functions like:
- automatic index pages
- the login
- register
- comments on articles
- administration options
Topics that will be covered:
- mix JSP/Java calls with TagLib (get preinitialized Core object)
- connection between html(forms) and module functions
- Error Messages
What we want
People may register on the page and leave comments to articles. The automatic index page will display the pagetitle and abstract, plus a few interesting information (last change, creation date, ...) about the pages. The index page will be a folder with the template 'index', all documents inside the folder will be indexed and displayed.
In the template for an article you may choose by checkbox if you want comments or not. Only logged in users may post to comments and moderators will be able to delete comments.
We will define a moderator as a logged in user who has the right GUESTBOOK_DELETE and the modify right on the document currently displayed. The GUESTBOK_DELETE right can easily be applied to a group of users using the Core administration, without it even if you provide the functionality to delete a guestbook entry, they wont be able to do so.
In addition we only provide the functionality to people who can modify the current document, thus the creator of an article is also the moderator. In order to accomplish this we need to go a little deeper into how corinis works - we will code a few lines java (until now we only accessed java through TagLibs? or used templates). This will show you the power of being able to go deeper into the modules without actually having to work on the module code - which is one of the advantages of corinis over many php based content management systems.
Implement the Login
see also: Login
The observer design already has a nice designed login box, all we need to do is change the name of the input fields and insert the login code:
<h5>Log In To This Site</h5> <form class="login" method="post" action=""> <p> User:<br /> <input type="text" size="23" name="Login_username" /> <br /> Password: (<a href="passwordretrieval.jsp">forgotten</a>)<br/> <input type="password" size="23" name="Login_userauth" /> <br /> <input type="checkbox" name="Login_remember" value="true" /> remember me<br/> <input class="button" name="Login_createEntry" type="submit" value="Log in"/> </p> </form>
We are not using the action field since we want the user to be logged in on this page - and not redirected to another one. It does not matter if you use mthod post or get - corinis checks both parameter types.
By now we have the form on the page, now we need the user to actually "Log in" to the system.
You need at least Core 3.0.7 for this to work (or the latest svn build)
We add a few functions:
<corinis:module name="Login" xsl="show.xsl" function="deleteEntry"/> <corinis:module name="Login" xsl="show.xsl" function="createEntry"/> <corinis:function name="if" field="online" value="true"> <a title="logout" href="?Login_deleteEntry=true"><corinis:core name="name"/></a> </corinis:function> <corinis:function name="if" field="online" value="false"> <h5>Log In To This Site</h5> <form class="login" method="post" action=""> <p> User:<br /> <input type="text" size="23" name="Login_username" /> <br /> Password: (<a href="passwordretrieval.jsp">forgotten</a>)<br/> <input type="password" size="23" name="Login_userauth" /> <br /> <input type="checkbox" name="Login_remember" value="true" /> remember me<br/> <input class="button" name="Login_createEntry" type="submit" value="Log in"/> </p> </form> <p> New? <a href="index.jsp">Register »</a><br /> </p> </corinis:function>
Whats going on:
- corinis:module is a taglib that allows the call of ANY corinis module and most functions (see TagLib)
- corinis:module name=Login - loads the login modules
- function=createEntry - call the createEntry function (=log the user in)
- function=deleteEntry - call the deleteEntry function (=log the user out)
- the xsl parameter calls the show.xsl (in you xsl directory) to show you the xml tree returned - if you dont want anything being displayed just remove it
FYI: the two corinis:module function without the taglib would be:
((Login)core.getModule(Login.class)).createEntry(); ((Login)core.getModule(Login.class)).deleteEntry();
Since we don't want the login to appear if the user is already logged in, we use the <corinis:function name="if" field="online" value="[true|false]> tag to show certain blocks only if the user is logged on.
Next Steps
For the next steps we will combine TagLib and JSP functions - so before you go on please take a good look at the implementation basics page - unless you already know a good deal about Java and JSP.
Split the Layout
The next tutorials (like Tutorial/Observer_-_Webalbum-Photolibrary?) will each be on its own jsp page(s) - and because developers are lazy by nature we do not want the whole head/footer stuff copy/pased in every jsp (also makes changes much much harder).
We could write one "huge" jsp to handle all functionality we want, but this is neither necessary nor usable (maintaining a few lines of code/html is always easier than having to work with 1000 lines).
Thankfully Java Server Pages already have a good method of including code from different files. This is done by using <%@include file="path/file.jsp"%> see Implementation.
Before going on create two files: top.ijsp (ijsp for IncludedJSP) and bottom.ijsp - you can use any extension/filename for those files.
Now you put everything above <corinis:cms/> into top.ijsp and everything below into bottom.ijsp and save them.
Now change the index.jsp to look like:
<%@include file="top.ijsp"%> <corinis:cms/> <%@include file="bottom.ijsp"%>
Save it and try it out - it will work and look like just before.
The last preparation step is the initialisation of the Core module (object). In the top.ijsp add following code at the beginning of the file (best place is right after the doctype declaration):
<%@ page language="java" import="corinis.*,corinis.modules.*" %> <% Core core = new Core (pageContext); %>
