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

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

Introduction

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

Prototype

public Map<String, Object> getModel() 

Source Link

Document

Return the model map.

Usage

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 ww . j a va  2  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:alpha.portal.webapp.controller.ContributorRoleControllerTest.java

/**
 * Test basic page call.//from  w w  w  .  j  a  v a2 s.  c o m
 * 
 * @throws Exception
 *             the exception
 */
@SuppressWarnings("unchecked")
@Test
public void testBasicPage() throws Exception {
    final MockHttpServletRequest request = this.newGet("/contributorRole");
    request.setRemoteUser(this.testUserName);

    final int numberOfRoles = this.contributorRoleManager.getAll().size();
    /**
     * Only test precondition!
     */
    Assert.assertTrue("No roles to test!", numberOfRoles > 0);

    final ModelAndView result = this.ctrl.showPage(request);
    final Map<String, Object> resModel = result.getModel();

    Assert.assertTrue(resModel.containsKey("contributorRolesList"));
    final Object contribListObj = resModel.get("contributorRolesList");
    final List<ContributorRole> contribList = (List<ContributorRole>) contribListObj;
    Assert.assertEquals(numberOfRoles, contribList.size());
}

From source file:alpha.portal.webapp.controller.ContributorRoleControllerTest.java

/**
 * Test for get error redirects./*from  w ww  .  jav a2s  . c  om*/
 * 
 * @throws Exception
 *             the exception
 */
@Test
public void testGetErrors() throws Exception {
    MockHttpServletRequest request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    ModelAndView result = this.ctrl.showPage(request);
    Map<String, Object> resModel = result.getModel();
    Assert.assertTrue(resModel.containsKey("contributorRolesList"));

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("delete", "1234567890");
    result = this.ctrl.showPage(request);
    resModel = result.getModel();
    Assert.assertTrue(resModel.containsKey("contributorRolesList"));

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("delete", this.contributorRoleManager.getContributorRoleByName("Radiologe")
            .getContributorRoleId().toString());
    request.addParameter("edit", "1234567890");
    result = this.ctrl.showPage(request);
    resModel = result.getModel();

    request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("edit", "1234567890");
    result = this.ctrl.showPage(request);
    resModel = result.getModel();
    Assert.assertFalse(resModel.containsKey("showEditingForm"));
    Assert.assertFalse(resModel.containsKey("roleToEditId"));
    Assert.assertFalse(resModel.containsKey("roleToEdit"));
}

From source file:no.dusken.aranea.web.spring.ChainedController.java

/**
 * Calls the handleRequest controller for each of the Controllers in the
 * chain sequentially, merging the ModelAndView objects returned after each
 * call and returning the merged ModelAndView object. An exception thrown by
 * any of the controllers in the chain will propagate upwards through the
 * handleRequest() method of the ChainedController. The ChainedController
 * itself does not support any communication between the controllers in the
 * chain, but this can be effected by the controllers posting to a common
 * accessible object such as the ApplicationContext. Note that this will
 * introduce coupling between the controllers and will be difficult to
 * arrange into a parallel chain. A controller can stop processing of the
 * chain by returning a null ModelAndView object. Enhanced with adding the
 * chained views to the controller. This enables the controller to render
 * the views from the chained controllers.
 *
 * @param request  the HttpServletRequest object.
 * @param response the HttpServletResponse object.
 * @return the merged ModelAndView object for all the controllers.
 * @throws Exception if one is thrown by one of the controllers in the chain.
 *///from  w  w  w.  j  a v  a 2 s. com
public ModelAndView handleRequestSequentially(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    ModelAndView mergedModel = new ModelAndView();
    List<String> mergedViews = new ArrayList<String>();
    for (Controller controller : controllers) {
        try {
            ModelAndView model = controller.handleRequest(request, response);
            if (model == null) {
                // chain will stop if a controller returns a null
                // ModelAndView object.
                break;
            }
            mergedModel.addAllObjects(model.getModel());
            mergedViews.add(model.getViewName());
        } catch (Exception e) {
            throw new Exception(
                    "Controller: " + controller.getClass().getName() + " threw exception: " + e.getMessage(),
                    e);
        }
    }
    mergedModel.addObject("views", mergedViews);
    if (StringUtils.isNotEmpty(this.viewName)) {
        mergedModel.setViewName(this.viewName);
    }
    return mergedModel;
}

From source file:alpha.portal.webapp.controller.ContributorRoleControllerTest.java

/**
 * "Umlaut"-Test./*from w  w  w. ja  va2 s .  c o  m*/
 * 
 * @throws Exception
 *             the exception
 */
@SuppressWarnings("unchecked")
@Test
public void testUmlaute() throws Exception {
    final String newCRName = "'";

    MockHttpServletRequest request = this.newPost("/contributorRole");
    request.setRemoteUser(this.testUserName);
    request.addParameter("save_new", "button.save");
    request.addParameter("newContributorRole", newCRName);
    final MockHttpServletResponse response = new MockHttpServletResponse();
    this.ctrl.saveNew(request, response);
    Assert.assertTrue(StringUtils.isBlank(response.getErrorMessage()));

    request = this.newGet("/contributorRole");
    request.setRemoteUser(this.testUserName);
    final ModelAndView result = this.ctrl.showPage(request);
    final Map<String, Object> resModel = result.getModel();
    final Object contribListObj = resModel.get("contributorRolesList");
    final List<ContributorRole> contribList = (List<ContributorRole>) contribListObj;
    boolean contains = false;
    for (int c = 0; (c < contribList.size()) && (contains == false); c++) {
        if (contribList.get(c).getName().equals(newCRName)) {
            contains = true;
        }
    }
    Assert.assertTrue("Umlaut-Test failed", contains);
}

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 www . j a  v  a 2 s  .com
    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.centralperf.controller.ApiController.java

/**
 * Get run results as an Excel document (XSLX)
 * The file name for now is centralperf.xlsx
 * @param mav   ModelAndView will be used to return an Excel view
 * @param projectId ID of the project (from URI)
 * @param runId ID of the run (from URI)
 * @return A view that will be resolved as an Excel view by the view resolver
 *//*  www .  j  a v  a 2  s .  co  m*/
@RequestMapping(value = { "/project/{projectId}/run/{runId}/centralperf.xlsx",
        "/api/getRunResultsHTML/{rundId}/centralperf.xlsx" }, method = RequestMethod.GET)
public ModelAndView getRunResultsAsExcel(ModelAndView mav, @PathVariable("runId") Long runId) {
    Run run = runRepository.findOne(runId);

    // get the view and setup
    ExcelOOXMLView excelView = applicationContext.getBean(ExcelOOXMLView.class);
    excelView.setUrl("/WEB-INF/views/xlsx/centralperf_template");
    mav.getModel().put("run", run);
    mav.setView(excelView);
    // return a view which will be resolved by an excel view resolver
    return mav;
}

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

@Test
public void testGetNewForm() {
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    HttpSession session = EasyMock.createMock(HttpSession.class);

    EasyMock.expect(session.getAttribute("redemptionCode")).andReturn(null).anyTimes();
    EasyMock.expect(request.getSession()).andReturn(session).anyTimes();

    replayMocks();/*w  ww .j av a2  s . com*/
    EasyMock.replay(request, session);

    ModelAndView mv = userController.getNewForm(request);

    Assert.assertEquals(UserController.NEW_USER_VIEW, mv.getViewName());

    Map<String, Object> map = mv.getModel();
    Assert.assertNotNull(map);
    Assert.assertTrue(map.containsKey(UserController.NEW_USER_FORM_KEY));

    Object obj = map.get(UserController.NEW_USER_FORM_KEY);
    Assert.assertNotNull(obj);
    Assert.assertTrue(obj instanceof NewUserForm);

    EasyMock.verify(request, session);
}

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

@Test
public void testGetForgotPasswordForm() {
    replayMocks();/*from  w ww . j a  va  2s.c o m*/

    ModelAndView mv = userController.getForgotPasswordForm(null);

    Assert.assertEquals(UserController.FORGOT_PASSWORD_VIEW, mv.getViewName());

    Map<String, Object> map = mv.getModel();
    Assert.assertNotNull(map);
    Assert.assertTrue(map.containsKey(UserController.FORGOT_PASSWORD_FORM_KEY));

    Object obj = map.get(UserController.FORGOT_PASSWORD_FORM_KEY);
    Assert.assertNotNull(obj);
    Assert.assertTrue(obj instanceof ForgotPasswordForm);
}

From source file:org.springmodules.validation.valang.javascript.taglib.ValangRulesExportInterceptor.java

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

    BaseCommandController controller = retrieveBaseCommandControllerIfPossible(handler);
    if (controller == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Controller is of type " + controller.getClass() + " so ignoring");
        }//  w  w w  .j a  v  a2s  .co  m
        return;
    }

    Map model = modelAndView.getModel();
    String commandName = controller.getCommandName();
    if (model == null || !model.containsKey(commandName)) {
        if (logger.isWarnEnabled()) {
            logger.debug("Handler '" + handler + "' did not export command object '"
                    + controller.getCommandName() + "'; no rules added to model");
        }
        return;
    }

    Validator[] validators = controller.getValidators();
    for (int i = 0; i < validators.length; i++) {
        if (!ValangValidator.class.isInstance(validators[i])) {
            continue;
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Adding Valang rules from handler '" + handler + "' to model");
        }

        ValangValidator validator = (ValangValidator) validators[i];
        ValangJavaScriptTagUtils.addValangRulesToModel(commandName, validator.getRules(), model);
    }

}