!--a11y-->
Implementing the Controller of the Form
View 
The implementation of the Form view controller is divided up into the following procedures:
· First of all, additional methods are declared for the input check and for the initialization of value attributes.
· The input check for the input fields Name, Birthday, and E-Mail Address is started in the event handler for the Save action using the appropriate method calls.
· The user should be able to reset all input fields by selecting Clear Errors, even if they contain incorrect entries
· The navigation to the EMailEditor should only be possible when the user has entered an e-mail address
The IWDMessageManager interface is used in different controller methods to display messages.
Before you can actually start implementing the controller, you must declare four additional methods in the Methods tab of the Form view.
...
1. Declare the four methods using the following table:
Public Methods in the View Controller Form.java |
|||
Method Name |
Type of Return Value |
Parameter Name |
Parameter Type |
checkDateInPast |
void |
fieldName |
string |
checkDesired |
void |
fieldName |
string |
checkMandatory |
void |
filedName |
string |
initialize |
void |
|
|
The methods checkDateInPast(), checkDesired(), and checkMandatory() are called by the event handler of the onActionSave() action. They contain the actual input checks.
The relevant error message is stored internally by a source code line of the following type:
messageMgr.reportContextAttributeMessage( wdContext.currentContextElement(), attributeInfo, IMessageSimpleErrors.<MessageKey>, Object[] arguments, true); |
By calling the report methods of the IWDMessageManager interface, it is possible to use the temporary storage of the Web Dynpro runtime environment for multiple messages and display them together in the Web browser after completing a request/response cycle.
If the message text contains arguments, they are passed in an object array (see java.text.MessageFormat).
2. Implement the methods checkDateInPast(), checkDesired(), and checkMandatory()using the following source code. Note the source code block between //@begin imports and //@end for importing used Java classes.
Form.java |
... //@@begin imports import java.sql.Date; import com.sap.tc.webdynpro.progmodel.api.IWDMessageManager; import com.sap.tc.webdynpro.tutorial.errorbehavior.wdp.IMessageSimpleErrors; import com.sap.tc.webdynpro.tutorial.errorbehavior.wdp.IPrivateForm; //@@end ... /** declared method */ public void checkDesired(java.lang.String fieldName) { //@@begin checkDesired() IWDMessageManager messageMgr = wdComponentAPI.getMessageManager(); Object attributeValue = wdContext.currentContextElement().getAttributeValue(fieldName); IWDAttributeInfo attributeInfo = wdContext.getNodeInfo().getAttribute(fieldName); if (attributeValue instanceof String) { if (((String) attributeValue).length() == 0) { if (fieldName.equals(IPrivateForm.IContextElement.E_MAIL_ADDRESS)) messageMgr.reportContextAttributeMessage( wdContext.currentContextElement(), attributeInfo, IMessageSimpleErrors.DESIRED_E_MAIL, null, true); } } //@@end }
/** declared method */ public void checkMandatory(java.lang.String fieldName) { //@@begin checkMandatory() IWDMessageManager messageMgr = wdComponentAPI.getMessageManager(); Object attributeValue = wdContext.currentContextElement().getAttributeValue(fieldName); IWDAttributeInfo attributeInfo = wdContext.getNodeInfo().getAttribute(fieldName); if (attributeValue instanceof String) { if (((String) attributeValue).length() == 0) { String fieldLabel = wdContext.getNodeInfo().getAttribute(fieldName) .getSimpleType().getFieldLabel(); messageMgr.reportContextAttributeMessage( wdContext.currentContextElement(), attributeInfo, IMessageSimpleErrors.MISSING_INPUT, new Object[] { fieldLabel }, true); } } //@@end }
/** declared method */ public void checkDateInPast(java.lang.String fieldName) { //@@begin checkDateInPast() IWDMessageManager msgMgr = wdComponentAPI.getMessageManager(); Date theDate = (Date) wdContext.currentContextElement().getAttributeValue(fieldName); IWDAttributeInfo attributeInfo = wdContext.getNodeInfo().getAttribute(fieldName);
if (theDate.after(new Date(System.currentTimeMillis()))) { String fieldLabel = wdContext.getNodeInfo().getAttribute(fieldName) .getSimpleType().getFieldLabel(); msgMgr.reportContextAttributeMessage( wdContext.currentContextElement(), attributeInfo, IMessageSimpleErrors.DATE_IS_IN_FUTURE, new Object[] { fieldLabel, theDate }, true); } //@@end } ... |

Note that setting the state=required property for the input field NameInputon the user interface merely results in the field being specially marked. If the user leaves the input field blank, no error is displayed by the Web Dynpro runtime environment, regardless of the defined value of the property (normal or required). The application developer is responsible for checking any input made using application code (method checkMandatory()).
The previously declared initialize() method is used to initialize the value attributes in the controller context and the data type information field label that belongs to the EMailAddress value attribute. The method is called from the wdDoInit() method and from the non-validating action event handler onActionClear() directly after instantiating the view controller.
Form.java |
... /** Hook method called to initialize controller. */ public void wdDoInit() { //@@begin wdDoInit() this.initialize(); //@@end } ... /** declared method */ public void initialize() { //@@begin initialize() wdContext.currentContextElement().setName(""); wdContext.currentContextElement().setBirthday(new Date(0)); wdContext.currentContextElement().setEMailAddress(""); wdContext.getNodeInfo().getAttribute(IPrivateForm.IContextElement.E_MAIL_ADDRESS) .getModifiableSimpleType().setFieldLabel("E-Mail Address"); //@@end } ... |

For the names of the defined constant attributes, constants are created in the corresponding context node interfaces. For example, for the Name attribute, the constant IPrivateForm.IContextElement.NAME is generated in the root node of the context of the Form view. The value of this constant macthes the string "Name". At design time, you can thus already avoid runtime errors caused by incorrectly typed attribute names.
Three different input checks are run in the onActionsSave() event handler using the previously implemented methods checkDateInPast(), checkDesired(), and checkMandatory(). Any error messages that may be contained in the event handler are first stored internally and displayed by calling the interface method raisePendingException()of the IWDMessageManager interface and the subsequent source code is not executed.
IWDMessageManager.java |
... /** * This method checks if any exceptions were reported to the exception manager and * are still stored in the exceptions manager. If at least one * exception is still stored, this method does not return, but raises a framework * internal exception instead to return to the framework error handler. */ public void raisePendingException(); ... |
The raisePendingException() method triggers a framework-internal exception error if at least one error was reported to the message manager. Only in this case does the method not return to the caller (in this example: the view controller), and – as intended – the following source code line is not executed.
wdComponentAPI.getMessageManager().reportSuccess( "The sample form data was successfully saved!"); |
3. Now implement the method onActionSave(IWDCustomEvent wdEvent):
Form.java |
... /** declared validating event handler */ public void onActionSave(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent ) { //@@begin onActionSave(ServerEvent) this.checkMandatory(IPrivateForm.IContextElement.NAME); this.checkDateInPast(IPrivateForm.IContextElement.BIRTHDAY); this.checkDesired(IPrivateForm.IContextElement.E_MAIL_ADDRESS); wdComponentAPI.getMessageManager().raisePendingException(); wdComponentAPI.getMessageManager().reportSuccess( "The sample form data was successfully saved!"); //@@end } ... |
Navigation to the input of an e-mail message in the EMailEditor view is only started in the Web Dynpro runtime environment if no error is reported in the checkMandatory()method.
4. The implementation of the onActionSendEMail(IWDCustomEvent wdEvent) method contains two lines of source code:
Form.java |
... /** declared validating event handler */ public void onActionSendEMail(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent ) { //@@begin onActionSendEMail(ServerEvent) this.checkMandatory(IPrivateForm.IContextElement.E_MAIL_ADDRESS); wdThis.wdFirePlugSendEMailOut( wdContext.currentContextElement().getEMailAddress()); //@@end } ... |
In the last step, the event handler is implemented for the non-validating Clear action. Such an event handler is called before individual user entries are validated and stored in the controller context. This allows you to initialize the context again despite incorrect user input.
5. Add the following source code to the view controller Form:
Form.java |
... //@@begin javadoc:onActionClear(ServerEvent) /** declared non validating event handler */ //@@end public void onActionClear(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent ) { //@@begin onActionClear(ServerEvent) // Re-initialize this view´s context this.initialize(); //@@end } ... |
The next step is the implementation of the
EMailEditor view controller.