1.0          Other languages
 
 

3.3.2 Leaf dispatcher with success/failure

 
 

Some requests need to be validated on server side. For example, when user create a order on web page, and submit to application server. Application server will then check user's input data, and give error messages if it finds something wrong, such as, order code already exists.

Not only insert/update will show success/failure page. There are some other request, will also need to do some validation. As we all know, when user login, system will need to check user's password. This is normally a query request with validation.

VelocityWeb make it easy to use uniform success/failure page, with a pop-up window. When user get a fail message, he/she can close the pop-up window, and correct bad input data in main window. And the success page can redirect the main page to a new page. For example, when user add a new order to system, system will normally redirect to list orders page.

Data inputed by user will not lose when user get a failure page, because the notice is given at a pop-up window. The input window still remains there.

Here is code example for login, including data input page, and process page.

public class LoginInputDispatcher extends QueryDispatcher {

    public boolean process(HttpServletRequest request, WebAppContext webContext, Controller controller)
            throws Exception {
        this.setHtmlTemplateFileName("user/login.htm");
        this.setPageTitle("Login page");

        webContext.put("result_win_url", DispatcherTreeManager.getUrl(ShowProcessResultDispatcher.class));
        webContext.put("target_process_url", DispatcherTreeManager.getUrl(LoginProcessDispatcher.class));
        
        //remember enter url, after login, jump to it.
        webContext.put("enter_url", request.getRequestURL().toString());
        
        return true;
    }

    public String getUrlId() {
        return "login";
    }

}
	  

The ShowProcessResultDispatcher will show success or fail page, based on the return value of LoginProcessDispatcher.process() .

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // call service to check user name and password
        UserService userService = new UserService(webContext);
        boolean passed = userService.checkPassword(username, password);
        if (!passed) {
            List errorMsgList = new LinkedList();
            errorMsgList.add("Login fail, user does not exist or wrong password.");
            webContext.put(controller.getDispatchFailMessageListTag(), errorMsgList);
            log.debug("LoginProcessDispatcher.process() fail end");
            return false;
        }	  

Each project/product need to has it's own success/failure page. To define it, we need to specify it on initial part of our own application controller.

public class PetStoreController extends SimpleController {
    ...
    public boolean initApplication() {
	...
            // set process success template and fail template
            setDispatchFailTemplateFileName("system/dispatcher_fail.htm");
            setDispatchSuccessTemplateFileName("system/dispatcher_success.htm");
            setDispatchFailMessageListTag("fail_msg_list");
            setDispatchSuccessMessageTag("success_msg");
            setDispatchSuccessJumpUrlTag("jump_url");
            setDispatchTargetUrlTag("target_dispatcher_url");
            setDispatchSuccessStaySecondsTag("stay_seconds");	
	  

Here is a picture of validating fail:

The fail messages can be put into a list, in case of many errors been report at one time.

Normally, we need some beautifier on this kind of pages.