Example usage for org.springframework.ui Model containsAttribute

List of usage examples for org.springframework.ui Model containsAttribute

Introduction

In this page you can find the example usage for org.springframework.ui Model containsAttribute.

Prototype

boolean containsAttribute(String attributeName);

Source Link

Document

Does this model contain an attribute of the given name?

Usage

From source file:sample.portlet.BooksController.java

@RequestMapping(params = "action=editBook") // render phase
public String showEditBookForm(@RequestParam("book") Integer id, Model model) {
    if (!model.containsAttribute("book")) {
        model.addAttribute("book", bookService.getBook(id));
    }/*from   w  ww  .j  a va  2s  .  c  o m*/
    return "bookEdit";
}

From source file:sample.portlet.BooksController.java

@RequestMapping(params = "action=addBook") // render phase
public String showAddBookForm(Model model) {
    if (!model.containsAttribute("book")) {
        model.addAttribute("book", new Book());
        model.addAttribute("page", 0);
    }/*from  w ww.j a va 2  s  .c  o  m*/
    return "bookAdd";
}

From source file:org.jasig.portlet.ClassifiedsPortlet.web.SubmitAdFormController.java

@RequestMapping(params = "action=addAd")
public String setupForm(@RequestParam(value = "id", required = false) Long id, Model model,
        PortletRequest request) {//from ww  w.  j a  va2 s .  co m

    Ad ad = new Ad();

    if (!model.containsAttribute("ad")) {
        if (id != null) {
            List<Ad> adList = this.adService.getAd(id);
            ad = (Ad) adList.get(0);
            ad.setPolicyAccepted(false);
        }

        model.addAttribute("ad", ad);
    }

    return "submitAdForm";
}

From source file:controllers.admin.PostController.java

@GetMapping("/edit/{postId}")
public String showUpdatePostForm(@PathVariable Long postId, Model model) {
    /* if "fresh" GET (ie, not redirect w validation errors): */
    if (!model.containsAttribute(BINDING_RESULT_NAME)) {
        Post post = postService.findById(postId);
        if (post == null) {
            throw new PostNotFoundException();
        }//  w w w  . j a  va  2 s.  co  m
        model.addAttribute(ATTRIBUTE_NAME, post);
    }
    return "admin/post/edit";
}

From source file:org.fenixedu.bennu.spring.FenixEDUBaseController.java

@ModelAttribute
protected void addModelProperties(final Model model, final HttpServletRequest request) {
    if (!model.containsAttribute(INFO_MESSAGES)) {
        model.addAttribute(INFO_MESSAGES, new ArrayList<String>());
    }//from w ww.j ava  2  s. co m
    if (!model.containsAttribute(WARNING_MESSAGES)) {
        model.addAttribute(WARNING_MESSAGES, new ArrayList<String>());
    }
    if (!model.containsAttribute(ERROR_MESSAGES)) {
        model.addAttribute(ERROR_MESSAGES, new ArrayList<String>());
    }

    //HACK: Forcing the "Messages AS DEFAULT CODE"
    WebApplicationContext webAppContext = RequestContextUtils.getWebApplicationContext(request);
    MessageSource messageSource = (MessageSource) webAppContext.getBean("messageSource");
    if (messageSource != null && messageSource instanceof ReloadableResourceBundleMessageSource) {
        ((ReloadableResourceBundleMessageSource) messageSource).setUseCodeAsDefaultMessage(true);
    }
    // Add here more attributes to the Model
    // model.addAttribute(<attr1Key>, <attr1Value>);
    // ....
}

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

/**
 * Test show form./* ww w .  j a  v  a  2  s. co m*/
 */
@Test
public void testShowForm() {
    final MockHttpServletRequest request = this.newGet("/cardassignform");
    request.setRemoteUser("admin");
    request.addParameter("card", "");
    request.addParameter("case", "");

    Model m = new ExtendedModelMap();
    this.form.showForm(request, m);
    Assert.assertFalse(m.containsAttribute("users"));

    request.setParameter("card", "440e4816-e01b-74d4-a716-449955440092");
    m = new ExtendedModelMap();
    this.form.showForm(request, m);
    Assert.assertFalse(m.containsAttribute("users"));

    request.setParameter("case", "atjaerhe");
    m = new ExtendedModelMap();
    this.form.showForm(request, m);
    Assert.assertFalse(m.containsAttribute("users"));

    request.setParameter("case", "550e4713-e22b-11d4-a716-446655440000");
    m = new ExtendedModelMap();
    this.form.showForm(request, m);

    final List<UserExtension> l = new LinkedList<UserExtension>();
    l.add(this.userExtensionManager.get(-5L));
    l.add(this.userExtensionManager.get(-4L));
    l.add(this.userExtensionManager.get(-2L));

    // FIXME the sample-data.xml change concerning the contributorrole
    // values has broken this
    // ModelAndViewAssert.assertModelAttributeValue(new ModelAndView("",
    // m.asMap()), "users", l);
}

From source file:com.blogspot.jmelon.portlet.quiz.controller.QuizEditController.java

@RequestMapping
public String view(RenderRequest request, RenderResponse response, Model model)
        throws JsonParseException, JsonMappingException, IOException {
    if (!model.containsAttribute(QUIZ_PREFS)) {
        PortletPreferences portletPrefs = request.getPreferences();
        String prefsJson = portletPrefs.getValue(QUIZ_PREFS, BLANK);
        LOGGER.debug("Marshalled quiz portlet prefs: {}", prefsJson);

        if (StringUtils.hasText(prefsJson)) {
            QuizPrefs quizPrefs = new ObjectMapper().readValue(prefsJson, QuizPrefs.class);
            model.addAttribute(QUIZ_PREFS, quizPrefs);
            LOGGER.debug("Unmarshalled quiz portlet prefs: {}", quizPrefs);
        } else {//www .  ja v a2 s . co m
            LOGGER.debug("No prefs set for quiz portlet at {}",
                    ((ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY)).getURLCurrent());
        }
    } else {
        LOGGER.debug("Prefs seem already set");
    }

    return EDIT_JSP;
}

From source file:org.fenixedu.bennu.spring.FenixEDUBaseController.java

protected String redirect(final String destinationAction, final Model model,
        final RedirectAttributes redirectAttributes) {
    if (model.containsAttribute(INFO_MESSAGES)) {
        redirectAttributes.addFlashAttribute(INFO_MESSAGES, model.asMap().get(INFO_MESSAGES));
    }//from  www  . ja v  a2s .  c o  m
    if (model.containsAttribute(WARNING_MESSAGES)) {
        redirectAttributes.addFlashAttribute(WARNING_MESSAGES, model.asMap().get(WARNING_MESSAGES));
    }
    if (model.containsAttribute(ERROR_MESSAGES)) {
        redirectAttributes.addFlashAttribute(ERROR_MESSAGES, model.asMap().get(ERROR_MESSAGES));
    }

    return "redirect:" + destinationAction;
}

From source file:ch.javaee.basicMvc.web.controller.UserController.java

@RequestMapping("/public/signup")
public String create(Model model) {
    logger.debug("Enter: create");
    if (!model.containsAttribute("user")) {
        model.addAttribute("user", new UserForm());
    }/*from   w  w  w  . j  a  v a  2s . com*/
    logger.debug("Check: reCaptcha {}", reCaptcha != null);
    if (reCaptcha != null) {
        model.addAttribute("recaptcha", reCaptcha.createRecaptchaHtml(null, null));
    }
    return "view/public/signup";
}

From source file:es.ucm.fdi.dalgs.learningGoal.web.LearningGoalController.java

@RequestMapping(value = "/degree/{degreeId}/competence/{competenceId}/learninggoal/{learninggoalId}/modify.htm", method = RequestMethod.GET)
public String modifyLearningGoalGET(@PathVariable("degreeId") Long id_degree,
        @PathVariable("competenceId") Long id_competence, @PathVariable("learninggoalId") Long id_learningGoal,
        Model model) throws ServletException {

    if (!model.containsAttribute("learningGoal")) {

        LearningGoal p = serviceLearningGoal.getLearningGoal(id_learningGoal, id_competence, id_degree)
                .getSingleElement();/*from   w w w.ja v a 2  s  .  c o m*/
        model.addAttribute("learningGoal", p);
    }
    model.addAttribute("valueButton", "Modify");
    model.addAttribute("typeform", "form.modify");

    return "learningGoal/form";

}