Example usage for org.springframework.web.servlet ModelAndView getView

List of usage examples for org.springframework.web.servlet ModelAndView getView

Introduction

In this page you can find the example usage for org.springframework.web.servlet ModelAndView getView.

Prototype

@Nullable
public View getView() 

Source Link

Document

Return the View object, or null if we are using a view name to be resolved by the DispatcherServlet via a ViewResolver.

Usage

From source file:org.wallride.web.support.DefaultModelAttributeInterceptor.java

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView mv) throws Exception {
    if (mv == null)
        return;/*w ww  .ja  va  2 s  . co  m*/
    if (mv.getView() instanceof RedirectView)
        return;
    if (mv.getViewName().startsWith("redirect:"))
        return;

    Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
    mv.addObject("BLOG", blog);

    List<String> languages = new ArrayList<>();
    if (blog != null) {
        for (BlogLanguage blogLanguage : blog.getLanguages()) {
            languages.add(blogLanguage.getLanguage());
        }
    }

    String currentLanguage = LocaleContextHolder.getLocale().getLanguage();

    mv.addObject("LANGUAGES", languages.toArray(new String[languages.size()]));
    mv.addObject("LANGUAGE_LINKS", buildLanguageLinks(currentLanguage, languages, request));

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    AuthorizedUser authorizedUser = null;
    if (authentication != null && authentication.getPrincipal() instanceof AuthorizedUser) {
        authorizedUser = (AuthorizedUser) authentication.getPrincipal();
    }
    mv.addObject("USER", authorizedUser);

    mv.addObject("WEBSITE_TITLE", blog != null ? blog.getTitle(currentLanguage) : null);
    mv.addObject("WEBSITE_LINK", buildGuestLink());
    mv.addObject("WEBSITE_PATH", buildGuestPath(currentLanguage, languages));

    mv.addObject("ADMIN_LINK", buildAdminLink());
    mv.addObject("ADMIN_PATH", buildAdminPath(currentLanguage));
}

From source file:fm.last.citrine.web.AdminControllerTest.java

@Test
public void testPrepareForShutdown() throws Exception {
    ModelAndView modelAndView = adminController.prepareForShutdown(mockRequest, mockResponse);
    verify(mockSchedulerManager).prepareForShutdown();
    RedirectView view = (RedirectView) modelAndView.getView();
    assertEquals("admin.do", view.getUrl());
    Map<String, Object> model = modelAndView.getModel();
    assertEquals(0, model.size());/*from  ww  w  .ja  va 2  s .  com*/
}

From source file:de.otto.mongodb.profiler.web.PageInterceptor.java

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {

    if (handler instanceof HandlerMethod
            && ((HandlerMethod) handler).getMethod().isAnnotationPresent(Page.class)
            && (modelAndView.getView() instanceof RedirectView == false)) {
        addMainNavigationViewModel(((HandlerMethod) handler), modelAndView);
        addFooterViewModel(modelAndView, request.getLocale());
    }//from  www .  j a va  2  s.  c o  m
}

From source file:fm.last.citrine.web.ChildTaskFormsControllerTest.java

@Test
public void testCancel() throws Exception {
    mockRequest.setParameter(Constants.PARAM_CANCEL, "true");
    TaskChildCandidatesDTO dto = new TaskChildCandidatesDTO();
    dto.setSelectedGroupName("testGroupName");
    ModelAndView modelAndView = childTasksFormController.processFormSubmission(mockRequest, mockResponse, dto,
            null);/* w  ww.  j a v  a2s. co  m*/
    RedirectView view = (RedirectView) modelAndView.getView();
    assertEquals("tasks.do?selectedGroupName=testGroupName", view.getUrl());
    assertEquals(0, modelAndView.getModel().size());
}

From source file:org.duracloud.account.app.controller.AccountUsersControllerTest.java

@Test
public void testAddUserByUsername() throws Exception {
    Long accountId = 2L;/* w w  w . ja  v a 2  s. c  o  m*/
    UsernameForm usernameForm = new UsernameForm();
    usernameForm.setUsername(TEST_USERNAME);
    Model model = new ExtendedModelMap();
    DuracloudUser user = createUser(TEST_USERNAME);

    EasyMock.expect(result.hasErrors()).andReturn(false);
    EasyMock.expect(userService.loadDuracloudUserByUsernameInternal(TEST_USERNAME)).andReturn(user).anyTimes();

    EasyMock.expect(userService.addUserToAccount(accountId, user.getId())).andReturn(true);

    addFlashAttribute();

    replayMocks();
    ModelAndView mav = this.accountUsersController.addUser(accountId, usernameForm, result, model,
            redirectAttributes);
    Assert.assertNotNull(mav);
    Assert.assertTrue(mav.getView() instanceof RedirectView);
}

From source file:fm.last.citrine.web.ChildTaskFormsControllerTest.java

@Test
public void testProcessFormSubmissionNoChildTasks() throws Exception {
    long taskId = 100;
    Task task = new Task("task100");
    task.setId(taskId);/*from  w w  w  . java2  s. com*/
    when(mockTaskManager.get(taskId)).thenReturn(task);

    TaskChildCandidatesDTO dto = new TaskChildCandidatesDTO();
    dto.setTask(task);
    dto.setSelectedGroupName("testGroupName");
    BindException bindException = new BindException(dto, "bla");
    ModelAndView modelAndView = childTasksFormController.processFormSubmission(mockRequest, mockResponse, dto,
            bindException);
    RedirectView view = (RedirectView) modelAndView.getView();
    assertEquals("tasks.do?selectedGroupName=testGroupName", view.getUrl());
    assertEquals(0, modelAndView.getModel().size());
}

From source file:fm.last.citrine.web.ChildTaskFormsControllerTest.java

@Test
public void testProcessFormSubmissionCandidateChildTasksOnly() throws Exception {
    long taskId = 100;
    Task task = new Task("task100");
    task.setId(taskId);/*from  w  w w .  j  ava2 s  . c  o  m*/
    when(mockTaskManager.get(taskId)).thenReturn(task);

    TaskChildCandidatesDTO dto = new TaskChildCandidatesDTO();
    dto.setTask(task);
    dto.setSelectedGroupName("testGroupName");

    long candidateTaskId = 200;
    Task candidateTask = new Task("task200");
    candidateTask.setId(candidateTaskId);
    when(mockTaskManager.get(candidateTaskId)).thenReturn(candidateTask);
    Set<Long> candidateTaskIds = new HashSet<Long>();
    candidateTaskIds.add(candidateTaskId);
    dto.setCandidateChildTaskIds(candidateTaskIds);

    BindException bindException = new BindException(dto, "bla");
    ModelAndView modelAndView = childTasksFormController.processFormSubmission(mockRequest, mockResponse, dto,
            bindException);
    RedirectView view = (RedirectView) modelAndView.getView();
    assertEquals("tasks.do?selectedGroupName=testGroupName", view.getUrl());
    assertEquals(0, modelAndView.getModel().size());

    Set<Task> childTasks = task.getChildTasks();
    assertEquals(1, childTasks.size());
    assertEquals(candidateTask, childTasks.iterator().next());
}

From source file:fm.last.citrine.web.ChildTaskFormsControllerTest.java

@Test
public void testProcessFormSubmissionChildTasksOnly() throws Exception {
    long taskId = 100;
    Task task = new Task("task100");
    task.setId(taskId);//from w  w w.j a v  a2  s  .  co  m
    when(mockTaskManager.get(taskId)).thenReturn(task);

    TaskChildCandidatesDTO dto = new TaskChildCandidatesDTO();
    dto.setTask(task);
    dto.setSelectedGroupName("testGroupName");

    long childTaskId = 300;
    Task childTask = new Task("task300");
    childTask.setId(childTaskId);
    when(mockTaskManager.get(childTaskId)).thenReturn(childTask);
    Set<Long> childTaskIds = new HashSet<Long>();
    childTaskIds.add(childTaskId);
    dto.setChildTaskIds(childTaskIds);

    BindException bindException = new BindException(dto, "bla");
    ModelAndView modelAndView = childTasksFormController.processFormSubmission(mockRequest, mockResponse, dto,
            bindException);
    RedirectView view = (RedirectView) modelAndView.getView();
    assertEquals("tasks.do?selectedGroupName=testGroupName", view.getUrl());
    assertEquals(0, modelAndView.getModel().size());

    Set<Task> childTasks = task.getChildTasks();
    assertEquals(1, childTasks.size());
    assertEquals(childTask, childTasks.iterator().next());
}

From source file:fm.last.citrine.web.ChildTaskFormsControllerTest.java

@Test
public void testProcessFormSubmissionCandidateAndChildTasks() throws Exception {
    long taskId = 100;
    Task task = new Task("task100");
    task.setId(taskId);/*from   ww w. ja  v a2s  . c  o  m*/
    when(mockTaskManager.get(taskId)).thenReturn(task);

    TaskChildCandidatesDTO dto = new TaskChildCandidatesDTO();
    dto.setTask(task);
    dto.setSelectedGroupName("testGroupName");

    long candidateTaskId = 200;
    Task candidateTask = new Task("task200");
    candidateTask.setId(candidateTaskId);
    when(mockTaskManager.get(candidateTaskId)).thenReturn(candidateTask);
    Set<Long> candidateTaskIds = new HashSet<Long>();
    candidateTaskIds.add(candidateTaskId);
    dto.setCandidateChildTaskIds(candidateTaskIds);

    long childTaskId = 300;
    Task childTask = new Task("task300");
    childTask.setId(childTaskId);
    when(mockTaskManager.get(childTaskId)).thenReturn(childTask);
    Set<Long> childTaskIds = new HashSet<Long>();
    childTaskIds.add(childTaskId);
    dto.setChildTaskIds(childTaskIds);

    BindException bindException = new BindException(dto, "bla");
    ModelAndView modelAndView = childTasksFormController.processFormSubmission(mockRequest, mockResponse, dto,
            bindException);
    RedirectView view = (RedirectView) modelAndView.getView();
    assertEquals("tasks.do?selectedGroupName=testGroupName", view.getUrl());
    assertEquals(0, modelAndView.getModel().size());

    Set<Task> childTasks = task.getChildTasks();
    assertEquals(2, childTasks.size());
    assertTrue(childTasks.contains(childTask));
    assertTrue(childTasks.contains(candidateTask));
}

From source file:org.sventon.web.ctrl.template.GoToControllerTest.java

@Test
public void testSvnHandle() throws Exception {
    final RepositoryService mockService = EasyMock.createMock(RepositoryService.class);
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest();

    final BaseCommand command = new BaseCommand();
    command.setName(new RepositoryName("test"));
    command.setPath("/file.txt");
    command.setRevision(Revision.create(12));

    final GoToController ctrl = new GoToController();

    final ConfigDirectory configDirectory = TestUtils.getTestConfigDirectory();
    configDirectory.setCreateDirectories(false);
    final MockServletContext servletContext = new MockServletContext();
    servletContext.setContextPath("sventon-test");
    configDirectory.setServletContext(servletContext);
    final Application application = new Application(configDirectory);

    application.setConfigured(true);//from w  w  w  . ja va 2  s  . c  o m
    ctrl.setServletContext(new MockServletContext());
    ctrl.setApplication(application);
    ctrl.setRepositoryService(mockService);
    ctrl.setAvailableCharsets(new AvailableCharsets("UTF-8"));

    // Test NodeKind.FILE
    expect(mockService.getNodeKind(null, command.getPath(), command.getRevisionNumber()))
            .andStubReturn(DirEntry.Kind.FILE);
    replay(mockService);

    ModelAndView modelAndView = ctrl.svnHandle(null, command, 100, null, mockRequest, null, null);
    Map model = modelAndView.getModel();
    verify(mockService);

    assertEquals(1, model.size());
    RedirectView view = (RedirectView) modelAndView.getView();
    assertEquals("/repos/test/show/file.txt", view.getUrl());

    reset(mockService);
    command.setPath("/dir");

    // Test NodeKind.DIR
    expect(mockService.getNodeKind(null, command.getPath(), command.getRevisionNumber()))
            .andStubReturn(DirEntry.Kind.DIR);
    replay(mockService);

    modelAndView = ctrl.svnHandle(null, command, 100, null, mockRequest, null, null);
    model = modelAndView.getModel();
    verify(mockService);

    assertEquals(1, model.size());
    view = (RedirectView) modelAndView.getView();
    assertEquals("/repos/test/list/dir/", view.getUrl());

    reset(mockService);

    // Test NodeKind.UNKNOWN
    expect(mockService.getNodeKind(null, command.getPath(), command.getRevisionNumber()))
            .andStubReturn(DirEntry.Kind.UNKNOWN);
    replay(mockService);

    final BindException errors = new BindException(command, "test");
    assertEquals(0, errors.getAllErrors().size());

    modelAndView = ctrl.svnHandle(null, command, 100, null, mockRequest, null, errors);
    model = modelAndView.getModel();
    verify(mockService);

    assertEquals(10, model.size());
    assertEquals("goto", modelAndView.getViewName());
}