List of usage examples for org.springframework.web.bind ServletRequestDataBinder bind
public void bind(ServletRequest request)
From source file:org.onecmdb.web.remote.RemoteController.java
/** * Create command//from w ww .j ava 2s . c om * * @param request * @param resp * @return * @throws Exception */ public ModelAndView createHandler(HttpServletRequest request, HttpServletResponse resp) throws Exception { long start = System.currentTimeMillis(); try { getLog().info("CreateHandler()"); CreateCommand command = new CreateCommand(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.onecmdb.web.remote.RemoteController.java
/** * Delete command//from w ww. j a v a 2s . co m * * @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();/*w ww . j a v a 2s. co 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; }//from w w w. ja v a 2s. 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 .ja va 2s. co 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); try {//from ww w.ja v a 2 s . c o m 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(); }
From source file:org.springframework.web.multipart.commons.CommonsMultipartResolverTests.java
private void doTestBinding(MockCommonsMultipartResolver resolver, MockHttpServletRequest originalRequest, MultipartHttpServletRequest request) throws UnsupportedEncodingException { MultipartTestBean1 mtb1 = new MultipartTestBean1(); assertArrayEquals(null, mtb1.getField1()); assertEquals(null, mtb1.getField2()); ServletRequestDataBinder binder = new ServletRequestDataBinder(mtb1, "mybean"); binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); binder.bind(request); List<MultipartFile> file1List = request.getFiles("field1"); CommonsMultipartFile file1a = (CommonsMultipartFile) file1List.get(0); CommonsMultipartFile file1b = (CommonsMultipartFile) file1List.get(1); CommonsMultipartFile file2 = (CommonsMultipartFile) request.getFile("field2"); assertEquals(file1a, mtb1.getField1()[0]); assertEquals(file1b, mtb1.getField1()[1]); assertEquals(new String(file2.getBytes()), new String(mtb1.getField2())); MultipartTestBean2 mtb2 = new MultipartTestBean2(); assertArrayEquals(null, mtb2.getField1()); assertEquals(null, mtb2.getField2()); binder = new ServletRequestDataBinder(mtb2, "mybean"); binder.registerCustomEditor(String.class, "field1", new StringMultipartFileEditor()); binder.registerCustomEditor(String.class, "field2", new StringMultipartFileEditor("UTF-16")); binder.bind(request);//from www . j av a 2 s. com assertEquals(new String(file1a.getBytes()), mtb2.getField1()[0]); assertEquals(new String(file1b.getBytes()), mtb2.getField1()[1]); assertEquals(new String(file2.getBytes(), "UTF-16"), mtb2.getField2()); resolver.cleanupMultipart(request); assertTrue(((MockFileItem) file1a.getFileItem()).deleted); assertTrue(((MockFileItem) file1b.getFileItem()).deleted); assertTrue(((MockFileItem) file2.getFileItem()).deleted); resolver.setEmpty(true); request = resolver.resolveMultipart(originalRequest); binder.setBindEmptyMultipartFiles(false); String firstBound = mtb2.getField2(); binder.bind(request); assertFalse(mtb2.getField2().isEmpty()); assertEquals(firstBound, mtb2.getField2()); request = resolver.resolveMultipart(originalRequest); binder.setBindEmptyMultipartFiles(true); binder.bind(request); assertTrue(mtb2.getField2().isEmpty()); }
From source file:org.springframework.web.servlet.mvc.generic.GenericFormController.java
/** * Show a new form. Prepares a backing object for the current form * and the given request, including checking its validity. * @param request current HTTP request//from w w w . j ava2 s . c o m * @return the prepared form view * @throws Exception in case of an invalid new form object */ protected final ModelAndView showNewForm(HttpServletRequest request) throws Exception { logger.debug("Displaying new form"); // Create form-backing object for new form. T formObject = formBackingObject(request); Assert.state(formObject != null, "Form object returned by formBackingObject() must not be null"); // Bind without validation, to allow for prepopulating a form, and for // convenient error evaluation in views (on both first attempt and resubmit). ServletRequestDataBinder binder = createBinder(request, formObject); binder.bind(request); BindingResult bindingResult = binder.getBindingResult(); onBind(request, formObject, bindingResult); return showForm(request, bindingResult); }
From source file:org.springframework.web.servlet.mvc.generic.GenericFormController.java
/** * This implementation calls {@link #showForm} in case of errors, * and delegates to {@link #onSubmit}'s variant else. * <p>This can only be overridden to check for an action that should be executed * without respect to binding errors, like a cancel action. To just handle successful * submissions without binding errors, override the {@link #onSubmit} method. * @see #showForm//from w ww. j av a 2 s . c o m * @see #onSubmit */ protected ModelAndView processFormSubmission(HttpServletRequest request) throws Exception { T formObject = formBackingObject(request); ServletRequestDataBinder binder = createBinder(request, formObject); binder.bind(request); BindingResult bindingResult = binder.getBindingResult(); onBind(request, formObject, bindingResult); return processFormSubmission(request, formObject, bindingResult); }
From source file:org.springframework.web.servlet.mvc.multiaction.MultiActionController.java
/** * Bind request parameters onto the given command bean * @param request request from which parameters will be bound * @param command command object, that must be a JavaBean * @throws Exception in case of invalid state or arguments *//*w w w.java 2 s .c o m*/ protected void bind(HttpServletRequest request, Object command) throws Exception { logger.debug("Binding request parameters onto MultiActionController command"); ServletRequestDataBinder binder = createBinder(request, command); binder.bind(request); if (this.validators != null) { for (Validator validator : this.validators) { if (validator.supports(command.getClass())) { ValidationUtils.invokeValidator(validator, command, binder.getBindingResult()); } } } binder.closeNoCatch(); }