Example usage for org.springframework.web.bind ServletRequestDataBinder ServletRequestDataBinder

List of usage examples for org.springframework.web.bind ServletRequestDataBinder ServletRequestDataBinder

Introduction

In this page you can find the example usage for org.springframework.web.bind ServletRequestDataBinder ServletRequestDataBinder.

Prototype

public ServletRequestDataBinder(@Nullable Object target) 

Source Link

Document

Create a new ServletRequestDataBinder instance, with default object name.

Usage

From source file:org.onecmdb.web.remote.RemoteController.java

/**
 * Delete command/*from  w  w  w .j a  va  2 s.  c  om*/
 * 
 * @param request
 * @param resp
 * @return
 * @throws Exception
 */
public ModelAndView deleteHandler(HttpServletRequest request, HttpServletResponse resp) throws Exception {
    long start = System.currentTimeMillis();
    try {
        getLog().info("DeleteHandler()");
        DeleteCommand command = new DeleteCommand(this.onecmdb);
        ServletRequestDataBinder binder = new ServletRequestDataBinder(command);
        binder.bind(request);

        try {
            resp.setContentType(command.getContentType());

            //resp.setContentLength(-1);
            OutputStream out = resp.getOutputStream();
            command.transfer(out);
            out.flush();

        } catch (Throwable e) {
            e.printStackTrace();
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST, "Error:" + e.getMessage());
        }
        return (null);
    } finally {
        long stop = System.currentTimeMillis();
        System.out.println("Update:" + (stop - start) + "ms");
    }
}

From source file:org.openlegacy.rpc.mvc.web.LoginController.java

@RequestMapping(value = "Login", method = RequestMethod.POST)
public String login(Model uiModel, HttpServletRequest request,
        @RequestParam(value = "partial", required = false) String partial,
        @RequestParam(value = "requestedUrl", required = false) String requestedUrl) throws URISyntaxException {

    rpcSession.disconnect();//from  w  w  w  .  jav  a2s.  c o m
    LoginModel loginModel = new LoginModel();
    ServletRequestDataBinder binder = new ServletRequestDataBinder(loginModel);
    binder.bind(request);

    try {
        rpcSession.login(loginModel.getUser(), loginModel.getPassword());
        request.getSession().setAttribute("ol_loggedInUser", loginModel.getUser());
    } catch (Exception e) {
        rpcSession.disconnect();
        loginModel.setErrorMessage(e.getMessage());
        uiModel.addAttribute(MvcConstants.LOGIN_MODEL, loginModel);
        return MvcConstants.LOGIN_VIEW;
    }

    if (StringUtils.isNotEmpty(requestedUrl)) {
        URI uri = new URI(requestedUrl);
        return MvcConstants.REDIRECT + uri.toASCIIString();
    }

    return MvcConstants.REDIRECT + afterLoginURL;
}

From source file:org.openlegacy.terminal.mvc.web.DefaultGenericController.java

@RequestMapping(value = "/{screen}", method = RequestMethod.POST)
public String postScreenEntity(@PathVariable("screen") String screenEntityName,
        @RequestParam(defaultValue = "", value = ACTION) String action,
        @RequestParam(value = "partial", required = false) String partial, HttpServletRequest request,
        HttpServletResponse response, Model uiModel) throws IOException {

    Class<?> entityClass = findAndHandleNotFound(screenEntityName, response);
    if (entityClass == null) {
        return null;
    }// w  ww.  j  av a 2 s.co  m

    ScreenEntity screenEntity = (ScreenEntity) terminalSession.getEntity(screenEntityName);
    if (screenEntity == null) {
        ScreenEntity currentEntity = terminalSession.getEntity();
        return handleEntity(request, uiModel, currentEntity);
    }

    ServletRequestDataBinder binder = new ServletRequestDataBinder(screenEntity);
    registerPropertyEditors(binder);
    binder.bind(request);

    TerminalActionDefinition matchedActionDefinition = screenEntityUtils.findAction(screenEntity, action);
    ScreenEntity resultEntity = terminalSession.doAction((TerminalAction) matchedActionDefinition.getAction(),
            screenEntity);
    if (matchedActionDefinition != null && matchedActionDefinition.getTargetEntity() != null) {
        resultEntity = (ScreenEntity) terminalSession.getEntity(matchedActionDefinition.getTargetEntity());
    }

    return handleEntity(request, uiModel, resultEntity);
}

From source file:org.openlegacy.terminal.mvc.web.DefaultGenericScreensController.java

@RequestMapping(value = "/{entity}/{key:[[\\w\\p{L}]+[:-_ ,]*[\\w\\p{L}]+]+}", method = RequestMethod.POST)
public String postEntityWithKey(@PathVariable("entity") String entityName, @PathVariable("key") String key,
        @RequestParam(defaultValue = "", value = ACTION) String action,
        @RequestParam(value = "partial", required = false) String partial,
        @RequestParam(value = TARGET, required = false) String target, HttpServletRequest request,
        HttpServletResponse response, Model uiModel) throws IOException {
    Class<?> entityClass = findAndHandleNotFound(entityName, response);
    if (entityClass == null) {
        return null;
    }/*  w ww . j av a 2  s .  c o m*/

    ScreenEntity screenEntity = (ScreenEntity) getSession().getEntity(entityName, key);
    boolean isPartial = request.getParameter("partial") != null;
    if (screenEntity == null) {
        ScreenEntity currentEntity = getSession().getEntity();
        return handleEntity(request, uiModel, currentEntity, isPartial ? null : MvcConstants.REDIRECT);
    }

    ServletRequestDataBinder binder = new ServletRequestDataBinder(screenEntity);
    MvcUtils.registerEditors(binder, getEntitiesRegistry());
    binder.bind(request);
    screenBindUtil.bindTables(request, entityClass, screenEntity);

    ScreenEntityDefinition beforeEntityDefinition = (ScreenEntityDefinition) getEntitiesRegistry()
            .get(entityClass);
    TerminalActionDefinition matchedActionDefinition = screenEntityUtils.findAction(screenEntity, action);
    ScreenEntity resultEntity = getSession().doAction((TerminalAction) matchedActionDefinition.getAction(),
            screenEntity);
    if (matchedActionDefinition != null && matchedActionDefinition.getTargetEntity() != null) {
        resultEntity = (ScreenEntity) getSession().getEntity(matchedActionDefinition.getTargetEntity());
    }

    String urlPrefix = isPartial ? null : MvcConstants.REDIRECT;
    if (resultEntity != null) {
        ScreenEntityDefinition afterEntityDefinition = (ScreenEntityDefinition) getEntitiesRegistry()
                .get(resultEntity.getClass());
        if (!beforeEntityDefinition.isWindow() && afterEntityDefinition.isWindow()) {
            urlPrefix = OpenLegacyViewResolver.WINDOW_URL_PREFIX;
        } else if (beforeEntityDefinition.isWindow() && !afterEntityDefinition.isWindow()) {
            urlPrefix = MvcConstants.REDIRECT;
        } else if (!beforeEntityDefinition.isWindow() && !afterEntityDefinition.isWindow()
                && !beforeEntityDefinition.getEntityName().equals(afterEntityDefinition.getEntityName())) {
            urlPrefix = MvcConstants.REDIRECT;
        }
    }
    if (target != null) {
        return urlPrefix + target;
    } else {
        return handleEntity(request, uiModel, resultEntity, urlPrefix);
    }
}

From source file:org.openlegacy.terminal.mvc.web.LoginController.java

@RequestMapping(value = { "Login", "login" }, method = RequestMethod.POST)
public String login(Model uiModel, HttpServletRequest request,
        @RequestParam(value = "partial", required = false) String partial,
        @RequestParam(value = "target", required = false) String target) throws URISyntaxException {

    ScreenEntityDefinition loginEntityDefinition = loginMetadata.getLoginScreenDefinition();

    terminalSession.getModule(Login.class).logoff();

    Object loginEntity = ReflectionUtil.newInstance(loginEntityDefinition.getEntityClass());
    ServletRequestDataBinder binder = new ServletRequestDataBinder(loginEntity);
    binder.bind(request);/*from w w  w . ja  v a2  s. c  o m*/

    try {
        terminalSession.getModule(Login.class).login(loginEntity);
    } catch (LoginException e) {
        logger.error(e.getMessage());
        terminalSession.getModule(Login.class).logoff();
        ScreenPojoFieldAccessor fieldAccessor = new SimpleScreenPojoFieldAccessor(loginEntity);
        fieldAccessor.setFieldValue(Login.ERROR_FIELD_NAME, e.getMessage());
        uiModel.addAttribute(MvcConstants.LOGIN_MODEL, loginEntity);
        return MvcConstants.LOGIN_VIEW;
    }

    System.out.println(request.getParameterNames());
    if (StringUtils.isNotEmpty(target)) {
        URI uri = new URI(target.replaceAll(" ", "%20"));
        return MvcConstants.REDIRECT + uri.toASCIIString();
    }

    ScreenEntity screenEntity = terminalSession.getEntity();

    if (screenEntity != null) {
        String resultEntityName = ProxyUtil.getOriginalClass(screenEntity.getClass()).getSimpleName();
        if (partial != null) {
            return MvcConstants.ROOTMENU_VIEW;
        } else {
            if (afterLoginURL != null) {
                return MvcConstants.REDIRECT + afterLoginURL;
            }
            return MvcConstants.REDIRECT + resultEntityName;
        }
    }
    return MvcConstants.REDIRECT + openlegacyWebProperties.getFallbackUrl();
}