Search in sources :

Example 1 with ExtendedModelMap

use of org.springframework.ui.ExtendedModelMap in project spring-framework by spring-projects.

the class ModelAndViewResolverMethodReturnValueHandler method handleReturnValue.

@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    if (this.mavResolvers != null) {
        for (ModelAndViewResolver mavResolver : this.mavResolvers) {
            Class<?> handlerType = returnType.getContainingClass();
            Method method = returnType.getMethod();
            ExtendedModelMap model = (ExtendedModelMap) mavContainer.getModel();
            ModelAndView mav = mavResolver.resolveModelAndView(method, handlerType, returnValue, model, webRequest);
            if (mav != ModelAndViewResolver.UNRESOLVED) {
                mavContainer.addAllAttributes(mav.getModel());
                mavContainer.setViewName(mav.getViewName());
                if (!mav.isReference()) {
                    mavContainer.setView(mav.getView());
                }
                return;
            }
        }
    }
    // No suitable ModelAndViewResolver...
    if (this.modelAttributeProcessor.supportsReturnType(returnType)) {
        this.modelAttributeProcessor.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
    } else {
        throw new UnsupportedOperationException("Unexpected return type: " + returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
    }
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) ModelAndViewResolver(org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver) ModelAndView(org.springframework.web.servlet.ModelAndView) Method(java.lang.reflect.Method)

Example 2 with ExtendedModelMap

use of org.springframework.ui.ExtendedModelMap in project Asqatasun by Asqatasun.

the class UserManagementControllerTest method testDisplayAdminPageWithDeletedUserMessage.

/**
     * Test of displayAdminPage method, of class UserManagementController.
     */
public void testDisplayAdminPageWithDeletedUserMessage() {
    System.out.println("DisplayAdminPageWithDeletedUserMessage");
    instance = new UserManagementController();
    setUpMockRoleDataService();
    setUpMockUserDataService(false, false, false, false, false);
    setUpMockAuthenticationContext();
    instance.setUserDataService(mockUserDataService);
    HttpServletResponse response = new MockHttpServletResponse();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.getSession().setAttribute(TgolKeyStore.DELETED_USER_NAME_KEY, "DeletedUserName");
    Model model = new ExtendedModelMap();
    String result = instance.displayAdminPage(request, response, model);
    assertEquals(TgolKeyStore.ADMIN_VIEW_NAME, result);
    assertTrue(model.asMap().containsKey(TgolKeyStore.DELETED_USER_NAME_KEY));
    assertEquals(model.asMap().get(TgolKeyStore.DELETED_USER_NAME_KEY), "DeletedUserName");
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Model(org.springframework.ui.Model) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 3 with ExtendedModelMap

use of org.springframework.ui.ExtendedModelMap in project Asqatasun by Asqatasun.

the class UserManagementControllerTest method testSubmitEditCurrentUserForm.

/**
     * Test of submitEditUserForm method, of class UserManagementController.
     */
public void testSubmitEditCurrentUserForm() throws Exception {
    System.out.println("testSubmitEditCurrentUserForm");
    instance = new UserManagementController();
    setUpMockRoleDataService();
    setUpMockUserDataService(false, false, false, false, true);
    setUpMockAuthenticationContext();
    instance.setUserDataService(mockUserDataService);
    CreateUserFormValidator createUserFormValidator = new CreateUserFormValidator();
    createUserFormValidator.setUserDataService(mockUserDataService);
    instance.setCreateUserFormValidator(createUserFormValidator);
    // Finally the form is conform and the admin page is returned
    CreateUserCommand createUserCommand = CreateUserCommandFactory.getInstance().getNewCreateUserCommand();
    createUserCommand.setEmail("admin@test.com");
    createUserCommand.setLastName("newName");
    createUserCommand.setFirstName("newFirstName");
    createUserCommand.setPhoneNumber("0102030405");
    createUserCommand.setActivated(false);
    createUserCommand.setAdmin(false);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.getSession().setAttribute(TgolKeyStore.USER_ID_KEY, Long.valueOf(4));
    BindingResult bindingResult = new BeanPropertyBindingResult(createUserCommand, "createUserCommand");
    Model model = new ExtendedModelMap();
    String result = instance.submitEditUserForm(createUserCommand, bindingResult, request, model);
    assertEquals(TgolKeyStore.ADMIN_VIEW_NAME, result);
    assertFalse(bindingResult.hasErrors());
    assertTrue(bindingResult.getFieldErrors().isEmpty());
    assertEquals(2, model.asMap().size());
    assertEquals("admin@test.com", model.asMap().get(TgolKeyStore.UPDATED_USER_NAME_KEY));
    assertTrue(((List<User>) model.asMap().get(TgolKeyStore.USER_LIST_KEY)).isEmpty());
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) User(org.asqatasun.webapp.entity.user.User) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Model(org.springframework.ui.Model) CreateUserFormValidator(org.asqatasun.webapp.validator.CreateUserFormValidator) CreateUserCommand(org.asqatasun.webapp.command.CreateUserCommand)

Example 4 with ExtendedModelMap

use of org.springframework.ui.ExtendedModelMap in project Asqatasun by Asqatasun.

the class UserManagementControllerTest method testSubmitAddAdminUserForm.

/**
     * Test of submitAddUserForm method, of class UserManagementController.
     */
public void testSubmitAddAdminUserForm() throws Exception {
    System.out.println("SubmitAddAdminUserForm");
    instance = new UserManagementController();
    setUpMockRoleDataService();
    setUpMockUserDataService(false, true, true, false, false);
    setUpMockAuthenticationContext();
    instance.setUserDataService(mockUserDataService);
    CreateUserFormValidator createUserFormValidator = new CreateUserFormValidator();
    createUserFormValidator.setUserDataService(mockUserDataService);
    instance.setCreateUserFormValidator(createUserFormValidator);
    // Finally the form is conform and the admin page is returned
    CreateUserCommand createUserCommand = CreateUserCommandFactory.getInstance().getNewCreateUserCommand();
    createUserCommand.setSiteUrl("http://www.newSite.com/");
    createUserCommand.setEmail("newUser@test.com");
    createUserCommand.setPassword("P4sSw0rD");
    createUserCommand.setConfirmPassword("P4sSw0rD");
    createUserCommand.setAdmin(true);
    createUserCommand.setActivated(true);
    BindingResult bindingResult = new BeanPropertyBindingResult(createUserCommand, "createUserCommand");
    Model model = new ExtendedModelMap();
    String result = instance.submitAddUserForm(createUserCommand, bindingResult, model);
    assertEquals(TgolKeyStore.ADMIN_VIEW_NAME, result);
    assertFalse(bindingResult.hasErrors());
    assertTrue(bindingResult.getFieldErrors().isEmpty());
    assertEquals(2, model.asMap().size());
    assertEquals("newUser@test.com", model.asMap().get(TgolKeyStore.ADDED_USER_NAME_KEY));
    assertTrue(((List<User>) model.asMap().get(TgolKeyStore.USER_LIST_KEY)).isEmpty());
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) User(org.asqatasun.webapp.entity.user.User) Model(org.springframework.ui.Model) CreateUserFormValidator(org.asqatasun.webapp.validator.CreateUserFormValidator) CreateUserCommand(org.asqatasun.webapp.command.CreateUserCommand)

Example 5 with ExtendedModelMap

use of org.springframework.ui.ExtendedModelMap in project Asqatasun by Asqatasun.

the class UserManagementControllerTest method testSubmitAddUserForm.

/**
     * Test of submitAddUserForm method, of class UserManagementController.
     */
public void testSubmitAddUserForm() throws Exception {
    System.out.println("submitAddUserForm");
    instance = new UserManagementController();
    setUpMockRoleDataService();
    setUpMockUserDataService(false, true, false, false, false);
    setUpMockAuthenticationContext();
    instance.setUserDataService(mockUserDataService);
    CreateUserFormValidator createUserFormValidator = new CreateUserFormValidator();
    createUserFormValidator.setUserDataService(mockUserDataService);
    instance.setCreateUserFormValidator(createUserFormValidator);
    // Finally the form is conform and the admin page is returned
    CreateUserCommand createUserCommand = CreateUserCommandFactory.getInstance().getNewCreateUserCommand();
    createUserCommand.setSiteUrl("http://www.newSite.com/");
    createUserCommand.setEmail("newUser@test.com");
    createUserCommand.setPassword("P4sSw0rD");
    createUserCommand.setConfirmPassword("P4sSw0rD");
    BindingResult bindingResult = new BeanPropertyBindingResult(createUserCommand, "createUserCommand");
    Model model = new ExtendedModelMap();
    String result = instance.submitAddUserForm(createUserCommand, bindingResult, model);
    assertEquals(TgolKeyStore.ADMIN_VIEW_NAME, result);
    assertFalse(bindingResult.hasErrors());
    assertTrue(bindingResult.getFieldErrors().isEmpty());
    assertEquals(2, model.asMap().size());
    assertEquals("newUser@test.com", model.asMap().get(TgolKeyStore.ADDED_USER_NAME_KEY));
    assertTrue(((List<User>) model.asMap().get(TgolKeyStore.USER_LIST_KEY)).isEmpty());
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) User(org.asqatasun.webapp.entity.user.User) Model(org.springframework.ui.Model) CreateUserFormValidator(org.asqatasun.webapp.validator.CreateUserFormValidator) CreateUserCommand(org.asqatasun.webapp.command.CreateUserCommand)

Aggregations

ExtendedModelMap (org.springframework.ui.ExtendedModelMap)38 Model (org.springframework.ui.Model)24 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)12 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 ForbiddenPageException (org.asqatasun.webapp.exception.ForbiddenPageException)11 CreateUserCommand (org.asqatasun.webapp.command.CreateUserCommand)10 CreateUserFormValidator (org.asqatasun.webapp.validator.CreateUserFormValidator)6 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)6 BindingResult (org.springframework.validation.BindingResult)6 User (org.asqatasun.webapp.entity.user.User)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 ForbiddenUserException (org.asqatasun.webapp.exception.ForbiddenUserException)3 Test (org.junit.Test)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Method (java.lang.reflect.Method)1 Principal (java.security.Principal)1 Enumeration (java.util.Enumeration)1