MVC Library

Introduction

Library MVC is available to develop XPages using the MVC paradigm for Lutece version 4.1 and higher

The library contains :

  • request dispatcher to process actions and display views
  • identification of actions and views with annotations
  • population and validation of beans (JSR 303)
  • management validation errors directly included in the model template ( marker " errors ")
  • management notifications and informational messages included in the model template ( marker " infos ")
  • HTTP redirection to views without using JSP

Links to code samples and online demo available at the end of the document.

MVC Model implementation

The library provides an implementation MVC MVCApplication interface XPageApplication providing mechanisms for designing functions according to the pattern Model-View-Controller as described in the following schema :

The entry to the XPage points plays the role of controller. This is MVCApplication class that supports this feature by implementing the getPage method interface.

Actions and views are identified by annotations @Action and @View .

The controller dispatches treatments on methods annotated according to the parameters of the request .

The @Controller annotation to define the derived class MVCApplication used to define the parameters of the class.

The home page of the XPage must set the parameter defaultView @View annotation to true.

Sample of code

Here is the code of an XPage offering two views :

  • the home page displays a list of contacts
  • page to create a contact

and two actions:

  • create a contact
  • delete a contact
@Controller ( xpageName = " mvcdemo " pageTitle = " mvcdemo.pageTitle " pagePath = " mvcdemo.pagePathLabel " )
public class extends MVCDemoApp MVCApplication
{
 
    private static final String TEMPLATE_HOME = "/ skins / plugins / mvcdemo / home.html "
    private static final String TEMPLATE_FORM = "/ skins / plugins / mvcdemo / form.html "
    private static final String VIEW_HOME = "home " ;
    private static final String VIEW_ADD_CONTACT = " addContact "
    private static final String ACTION_ADD_CONTACT = " addContact "
    private static final String ACTION_REMOVE_CONTACT = " removeContact "
    private static final String MARK_CONTACTS_LIST = " contacts_list "
    private static final String PARAMETER_ID = "id" ;
     
     
    @View ( value = VIEW_HOME , defaultView = true)
    public XPage viewHome ( HttpServletRequest request )
    {
        Map <string,object> model = getModel ();
        model.put ( MARK_CONTACTS_LIST , ContactService.getContacts ());
        return getXPage ( TEMPLATE_HOME , request.getLocale () , model ) ;
    }
 
    @View ( VIEW_ADD_CONTACT )
    public XPage viewAddContact ( HttpServletRequest request )
    {
        return getXPage ( TEMPLATE_FORM , request.getLocale ());
    }
     
    @Action ( ACTION_ADD_CONTACT )
    public XPage addContact ( HttpServletRequest request )
    {
        Contact contact = new Contact ();
        populate ( contact, request);
 
        if (! validateBean (contact) )
        {
            redirectView return (request, VIEW_ADD_CONTACT ) ;
        }
        ContactService.addContact (contact) ;
        redirectView return (request, VIEW_HOME ) ;
    }
     
    @Action ( ACTION_REMOVE_CONTACT )
    public XPage removeContact ( HttpServletRequest request )
    {
        StrContact String = request.getParameter ( PARAMETER_ID ) ;
        ContactService.removeContact ( strContact ) ;
        redirectView return (request, VIEW_HOME ) ;
    }

The complete source code for this example is available on the SVN at / sandbox / plugin- mvcdemo and available at http://dev.lutece.paris.fr/viewvc/lutece/sandbox/plugin-mvcdemo/

The application in action can be seen at this address: href="http://dev.lutece.paris.fr/incubator/jsp/site/Portal.jsp?page=mvcdemo" http://dev.lutece.paris.fr/incubator/jsp/site/Portal.jsp?page=mvcdemo

The plugin PlugInWizard has been rewritten in version 3.1 with this library and is an advanced example of its use .