org.jsxp.framework
Class ViewController

java.lang.Object
  extended by org.jsxp.framework.ViewController
All Implemented Interfaces:
ViewControllerLifeCycleExceptionHandler
Direct Known Subclasses:
org.jsxp.framework.webroot.generated.jsxpframework.components.AbstractCommandXhtmlControllerGenerated, org.jsxp.framework.webroot.generated.jsxpframework.components.ajax.AjaxXhtmlControllerGenerated, org.jsxp.framework.webroot.generated.jsxpframework.components.DebugMessagesXhtmlControllerGenerated, org.jsxp.framework.webroot.generated.jsxpframework.components.DeveloperWarningsXhtmlControllerGenerated, org.jsxp.framework.webroot.generated.jsxpframework.components.ErrorXhtmlControllerGenerated, org.jsxp.framework.webroot.generated.jsxpframework.components.FlashMessagesXhtmlControllerGenerated, org.jsxp.framework.webroot.generated.jsxpframework.components.MessagesXhtmlControllerGenerated, org.jsxp.framework.webroot.generated.jsxpframework.components.PagerXhtmlControllerGenerated, org.jsxp.framework.webroot.generated.jsxpframework.components.RedirectViewXhtmlControllerGenerated, org.jsxp.framework.webroot.generated.jsxpframework.components.ScopesMessagesXhtmlControllerGenerated

public class ViewController
extends java.lang.Object
implements ViewControllerLifeCycleExceptionHandler

The Class ViewController. A ViewController contains the code for any jsxp component. It has method to be initialized, to validate input parameter, to define the result and so on.

Mostly it has a corresponding view design file (e.g. index.xhtml), for an exclusion see AbstractCommandController. The ViewController is called by the jsxp framework if the corresponding view is requested.

Normally a ViewController class will be generated by the framework for each view design file. You have to derive this generated class to enter your code. The generated file will have special method to set view variables and to get access to identified jsxp element of the view. These methods help you to get compile time safety.

You can also derive your own ViewController classes to build specialized ones or to make a new component based on an existing.

The ViewController follows a ViewControllerLifeCycle for each request. You can get access to the life cycle by getLifeCycle() and you can add a PhaseListener to get more control by ViewControllerLifeCycle.addPhaseListener(LifeCyclePhase, PhaseListener). The possibles phases are listed in this enum: ViewControllerLifeCycle.LifeCyclePhase.


Field Summary
 java.util.Map<java.lang.String,InternalElement> idElements
           
 
Constructor Summary
ViewController(java.lang.String viewFilename)
          Instantiates a new view controller.
 
Method Summary
 void addElement(Element element)
          Adds the element.
 void addElementProcessor(ElementProcessor elementProcessor)
          Adds an element processor to this view controller.
 boolean containsVariable(java.lang.String key)
          Check if the view contains the variable.
 void execute()
          This method will be called by the jsxp framework in the execution phase.
 java.lang.String getCharacterEncoding()
          Gets the character encoding from the Application.getDefaultCharacterEncoding().
 java.lang.String getContentType()
          Gets the content type from the Application.getDefaultContentType().
 Element getElement(java.lang.String id)
          Returns the element with the jsxp id.
protected
<T extends Element>
T
getElement(java.lang.String id, java.lang.Class<T> interfaceType)
          Gets the element with the jsxp id.
 Element getElement(java.lang.String id, int index)
          Returns the element with the jsxp id and index (used if elements are inserted via element templating).
 Element getElement(java.lang.String idPrefix, java.lang.String id)
          Returns the element with the jsxp id prefix and id
 Element getElement(java.lang.String idPrefix, java.lang.String id, int index)
          Returns the element with the jsxp id prefix and id and index (used if elements are inserted via element templating).
 java.lang.String getIdPrefix()
          Gets the id prefix.
 java.lang.Object getInputParameterBean()
          Gets an instance of the input parameter bean.
 ViewControllerLifeCycle getLifeCycle()
          Gets the life cycle.
protected  ViewController getParent()
          Gets the parent ViewController, e.g.
 java.lang.Object getPreviousInputParameterBean()
          Gets an instance of the previous input parameter bean.
 Element getRootElement()
          Gets the root element.
 int getStatusCode()
          Gets the status code.
protected  ViewController getTemplatedView()
          Gets the templated view if this view controller is use as a template.
protected  Element[] getTemplateElements()
          Gets the template elements.
protected  Validator getValidator()
          Gets the validator that you can use in the view controller..
 java.lang.String getValueForVariable(java.lang.String key)
          Gets the value for a view variable.
 java.util.Map<java.lang.String,java.lang.String> getVariables()
          Gets the variables as key/value map.
 java.lang.String getViewFilename()
          Gets the view filename.
 ViewController getViewTemplate()
          Gets the view template.
 void handleExecutingException(java.lang.Exception e, ViewController source)
          Throws the given exception encapsulated in a runtime exception per default.
 void handleInitException(java.lang.Exception e, ViewController source)
          Throws the given exception encapsulated in a runtime exception per default.
 void handleInputProcessingException(java.lang.Exception e, ViewController source)
          Throws the given exception encapsulated in a runtime exception per default.
 void handlePreparingException(java.lang.Exception e, ViewController source)
          Throws the given exception encapsulated in a runtime exception per default.
 void handleRenderingException(java.lang.Exception e, ViewController source)
          Throws the given exception encapsulated in a runtime exception per default.
 void handleTemplatingException(java.lang.Exception e, ViewController source)
          Throws the given exception encapsulated in a runtime exception per default.
 void handleValidatingException(java.lang.Exception e, ViewController source)
          Throws the given exception encapsulated in a runtime exception per default.
<T extends ViewController>
T
importView(T ViewController)
          Import view.
<T extends ViewController>
T
importView(T ViewController, java.lang.String idPrefix, boolean inputParameterInjection, java.lang.String inputParameterPrefix)
          Import view.
protected  void init()
          This method will be called by the jsxp framework in the initializing phase.
protected  boolean isApplicationTemplateEnabled()
          Configures if application template is enabled.
protected  boolean isSkipExecution()
          Checks if is skip execution.
 boolean isUsedAsImportedView()
          Checks if is used as imported view.
 boolean isUsedAsTemplate()
          Checks if is used as template.
 boolean needToSaveScopesToSession()
          Need to save scopes to session.
protected  void replaceVariableValue(java.lang.String variableName, java.lang.String newValue)
          Replace variable value.
 void setApplicationTemplateEnabled(boolean applicationTemplating)
           
 void setNeedToSaveScopesToSession(boolean needToSaveScopesToSession)
          Sets the need to save scopes to session.
 void setVariable(java.lang.String variableName, java.lang.String value)
          Sets the variable.
<ViewTemplateInputParameterType>
void
setViewTemplate(ViewController viewTemplate)
          Sets the view template.
protected  void skipExecution()
          Skip execution.
 void validate()
          This method will be called by the jsxp framework in the validation lifecycle phase.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

idElements

public java.util.Map<java.lang.String,InternalElement> idElements
Constructor Detail

ViewController

public ViewController(java.lang.String viewFilename)
Instantiates a new view controller.

Parameters:
viewFilename - the view filename
Method Detail

addElement

public final void addElement(Element element)
Adds the element.

Parameters:
element - the element

addElementProcessor

public final void addElementProcessor(ElementProcessor elementProcessor)
Adds an element processor to this view controller. Can be used in any phase before the phase ViewControllerLifeCycle.LifeCyclePhase.ELEMENTPROCESSING. The various ElementProcessor can add special behaviour to elements (like Ajax behaviour or change of design).

Parameters:
elementProcessor -

containsVariable

public final boolean containsVariable(java.lang.String key)
Check if the view contains the variable.

Parameters:
key - the key
Returns:
true, if successful

execute

public void execute()
             throws java.lang.Exception
This method will be called by the jsxp framework in the execution phase. It will only be called for directly requested ViewController, not for template and not for imported ViewController. Therefore you have to call their execute() method explicitly if you want them also to be called. But recommended is to implemented code that should be executed also if the ViewController is used indirectly when imported in the init() method.

In this method you should call your business logic.

Throws:
java.lang.Exception - the exception

getCharacterEncoding

public java.lang.String getCharacterEncoding()
Gets the character encoding from the Application.getDefaultCharacterEncoding(). Override this method if your view has a special character encoding. Default is UTF-8.

Returns:
the character encoding

getContentType

public java.lang.String getContentType()
Gets the content type from the Application.getDefaultContentType(). Override this method to get return a special content type.

Returns:
the content type

getElement

public final Element getElement(java.lang.String id)
Returns the element with the jsxp id.

Parameters:
id -
Returns:
the corresponding element

getElement

protected final <T extends Element> T getElement(java.lang.String id,
                                                 java.lang.Class<T> interfaceType)
Gets the element with the jsxp id. Mostly used by the automatically generated view controller classes.

Parameters:
id - the id
interfaceType - the interface type
Returns:
the corresponding element

getElement

public final Element getElement(java.lang.String id,
                                int index)
Returns the element with the jsxp id and index (used if elements are inserted via element templating).

Parameters:
id -
index -
Returns:
the corresponding element

getElement

public final Element getElement(java.lang.String idPrefix,
                                java.lang.String id)
Returns the element with the jsxp id prefix and id

Parameters:
idPrefix -
id -
Returns:
the corresponding element

getElement

public final Element getElement(java.lang.String idPrefix,
                                java.lang.String id,
                                int index)
Returns the element with the jsxp id prefix and id and index (used if elements are inserted via element templating).

Parameters:
idPrefix -
id -
index -
Returns:
the corresponding element

getIdPrefix

public java.lang.String getIdPrefix()
Gets the id prefix. Override this method if you need to use a special idPrefix for your jsxp ids (you will see them in the rendered results). Normally you don�t need to do this.

Returns:
the id prefix

getInputParameterBean

public java.lang.Object getInputParameterBean()
Gets an instance of the input parameter bean. You have to override this method to use a special input parameter class. The jsxp framework will call this method to get a fresh instance and fill the bean by calling the set-method on the beans properties.

Returns:
the input parameter

getLifeCycle

public final ViewControllerLifeCycle getLifeCycle()
Gets the life cycle.

Returns:
the life cycle

getParent

protected final ViewController getParent()
Gets the parent ViewController, e.g. when a view controller is used as a sub component from another view controller.

Returns:
the parent

getPreviousInputParameterBean

public java.lang.Object getPreviousInputParameterBean()
Gets an instance of the previous input parameter bean. As default this returns null. You can override this method if you want to support the restoration of previous input parameter in a different bean. The framework will call this method to get a fresh instance and fill the bean by calling the set-method on the beans properties. Use that value in your execute method.

Returns:
the previous input parameter bean

getRootElement

public final Element getRootElement()
Gets the root element. This is the top most jsxp element of the view (e.g. the corresponding element to the html-tag in HTML).

Returns:
the root element

getStatusCode

public int getStatusCode()
Gets the status code. Override this method if you want to change the status code.

Returns:
the status code

getTemplatedView

protected final ViewController getTemplatedView()
Gets the templated view if this view controller is use as a template.

Returns:
the templated view

getTemplateElements

protected Element[] getTemplateElements()
Gets the template elements. Override this method if your ViewController should be used as a template. Return the elements, that will be exchange by elements with same jsxp id from the templated view.

Returns:
the template elements

getValidator

protected final Validator getValidator()
Gets the validator that you can use in the view controller..

Returns:
the validator

getValueForVariable

public final java.lang.String getValueForVariable(java.lang.String key)
Gets the value for a view variable.

Parameters:
key - the key
Returns:
the value for variable

getVariables

public java.util.Map<java.lang.String,java.lang.String> getVariables()
Gets the variables as key/value map.

Returns:
the variables

getViewFilename

public final java.lang.String getViewFilename()
Gets the view filename. The view filename is the name of the corresponding view design file.

Returns:
the view filename

getViewTemplate

public final ViewController getViewTemplate()
Gets the view template.

Returns:
the view template

handleExecutingException

public void handleExecutingException(java.lang.Exception e,
                                     ViewController source)
Throws the given exception encapsulated in a runtime exception per default.

Specified by:
handleExecutingException in interface ViewControllerLifeCycleExceptionHandler
Parameters:
e - the e
source - the source
See Also:
ViewControllerLifeCycleExceptionHandler.handleExecutingException(java.lang.Exception, org.jsxp.framework.ViewController)

handleInitException

public void handleInitException(java.lang.Exception e,
                                ViewController source)
Throws the given exception encapsulated in a runtime exception per default.

Specified by:
handleInitException in interface ViewControllerLifeCycleExceptionHandler
Parameters:
e - the e
source - the source
See Also:
ViewControllerLifeCycleExceptionHandler.handleInitException(java.lang.Exception, org.jsxp.framework.ViewController)

handleInputProcessingException

public void handleInputProcessingException(java.lang.Exception e,
                                           ViewController source)
Throws the given exception encapsulated in a runtime exception per default.

Specified by:
handleInputProcessingException in interface ViewControllerLifeCycleExceptionHandler
Parameters:
e - the e
source - the source
See Also:
ViewControllerLifeCycleExceptionHandler.handleInputProcessingException(java.lang.Exception, org.jsxp.framework.ViewController)

handlePreparingException

public void handlePreparingException(java.lang.Exception e,
                                     ViewController source)
Throws the given exception encapsulated in a runtime exception per default.

Specified by:
handlePreparingException in interface ViewControllerLifeCycleExceptionHandler
Parameters:
e - the e
source - the source
See Also:
ViewControllerLifeCycleExceptionHandler.handlePreparingException(java.lang.Exception, org.jsxp.framework.ViewController)

handleRenderingException

public void handleRenderingException(java.lang.Exception e,
                                     ViewController source)
Throws the given exception encapsulated in a runtime exception per default.

Specified by:
handleRenderingException in interface ViewControllerLifeCycleExceptionHandler
Parameters:
e - the e
source - the source
See Also:
ViewControllerLifeCycleExceptionHandler.handleRenderingException(java.lang.Exception, org.jsxp.framework.ViewController)

handleTemplatingException

public void handleTemplatingException(java.lang.Exception e,
                                      ViewController source)
Throws the given exception encapsulated in a runtime exception per default.

Specified by:
handleTemplatingException in interface ViewControllerLifeCycleExceptionHandler
Parameters:
e - the e
source - the source
See Also:
ViewControllerLifeCycleExceptionHandler.handleTemplatingException(java.lang.Exception, org.jsxp.framework.ViewController)

handleValidatingException

public void handleValidatingException(java.lang.Exception e,
                                      ViewController source)
Throws the given exception encapsulated in a runtime exception per default.

Specified by:
handleValidatingException in interface ViewControllerLifeCycleExceptionHandler
Parameters:
e - the e
source - the source
See Also:
ViewControllerLifeCycleExceptionHandler.handleValidatingException(java.lang.Exception, org.jsxp.framework.ViewController)

importView

public final <T extends ViewController> T importView(T ViewController)
                                          throws java.lang.Exception
Import view.

Parameters:
ViewController - the view control
Throws:
java.lang.Exception - the exception

importView

public final <T extends ViewController> T importView(T ViewController,
                                                     java.lang.String idPrefix,
                                                     boolean inputParameterInjection,
                                                     java.lang.String inputParameterPrefix)
                                          throws java.lang.Exception
Import view.

Parameters:
ViewController - the view control
idPrefix - the id prefix
inputParameterInjection - the input parameter injection
inputParameterPrefix - the input parameter prefix
Throws:
java.lang.Exception - the exception

init

protected void init()
             throws java.lang.Exception
This method will be called by the jsxp framework in the initializing phase. It will be called for any used ViewController, also for template and for imported ViewController.

In this method you should do anything that a view controller needs to do if he is used in any way.

Throws:
java.lang.Exception - the exception

isApplicationTemplateEnabled

protected boolean isApplicationTemplateEnabled()
Configures if application template is enabled. Override it to disable the use of application template or use the setApplicationTemplateEnabled(boolean) method.

Returns:
true, if global templating is enabled for this view.

isSkipExecution

protected final boolean isSkipExecution()
Checks if is skip execution.

Returns:
true, if is skip execution

isUsedAsImportedView

public final boolean isUsedAsImportedView()
Checks if is used as imported view.

Returns:
true, if is used as imported view

isUsedAsTemplate

public final boolean isUsedAsTemplate()
Checks if is used as template.

Returns:
true, if is used as template

needToSaveScopesToSession

public final boolean needToSaveScopesToSession()
Need to save scopes to session. Return true if this or any imported view controller has called setNeedToSaveScopesToSession(boolean) with true as parameter. This has the effect that the jsxp framework will create a session and save the scopes in it, so you can access them later.

Returns:
true, if successful

replaceVariableValue

protected final void replaceVariableValue(java.lang.String variableName,
                                          java.lang.String newValue)
Replace variable value. Mostly used by the generated ViewController classes.

Parameters:
variableName - the variable name
newValue - the new value

setApplicationTemplateEnabled

public final void setApplicationTemplateEnabled(boolean applicationTemplating)

setNeedToSaveScopesToSession

public final void setNeedToSaveScopesToSession(boolean needToSaveScopesToSession)
Sets the need to save scopes to session. Even if you set this to false it can be that a session is created and the scopes are saved. E.g. if an imported ViewController has the need or if this view controller is part of a view flow.

Parameters:
needToSaveScopesToSession - the new need to save scopes to session

setVariable

public final void setVariable(java.lang.String variableName,
                              java.lang.String value)
Sets the variable.

Parameters:
variableName - the variable name
value - the value

setViewTemplate

public final <ViewTemplateInputParameterType> void setViewTemplate(ViewController viewTemplate)
Sets the view template.

Parameters:
viewTemplate - the new view template

skipExecution

protected final void skipExecution()
Skip execution. Marks the ViewController to skip the execution phase.


validate

public void validate()
              throws java.lang.Exception
This method will be called by the jsxp framework in the validation lifecycle phase. Implement this method to validate the input parameter. You can use any validation framework you like in this method or use Validator if you want the simple jsxp validation support.

Throws:
java.lang.Exception - the exception


Copyright © 2012 Oliver Szymanski & David Tanzer