Java Code Examples for org.thymeleaf.context.ITemplateContext#getVariable()

The following examples show how to use org.thymeleaf.context.ITemplateContext#getVariable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageUtils.java    From thymeleaf-spring-data-dialect with Apache License 2.0 4 votes vote down vote up
public static Page<?> findPage(final ITemplateContext context) {
    // 1. Get Page object from local variables (defined with sd:page-object)
    // 2. Search Page using ${page} expression
    // 3. Search Page object as request attribute

    final Object pageFromLocalVariable = context.getVariable(Keys.PAGE_VARIABLE_KEY);
    if (isPageInstance(pageFromLocalVariable)) {
        return (Page<?>) pageFromLocalVariable;
    }

    // Check if not null and Page instance available with ${page} expression
    final IEngineConfiguration configuration = context.getConfiguration();
    final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);
    final IStandardExpression expression = parser.parseExpression(context, Keys.PAGE_EXPRESSION);
    final Object page = expression.execute(context);
    if (isPageInstance(page)) {
        return (Page<?>) page;
    }

    // Search for Page object, and only one instance, as request attribute
    if (context instanceof IWebContext) {
        HttpServletRequest request = ((IWebContext) context).getRequest();
        Enumeration<String> attrNames = request.getAttributeNames();
        Page<?> pageOnRequest = null;
        while (attrNames.hasMoreElements()) {
            String attrName = (String) attrNames.nextElement();
            Object attr = request.getAttribute(attrName);
            if (isPageInstance(attr)) {
                if (pageOnRequest != null) {
                    throw new InvalidObjectParameterException("More than one Page object found on request!");
                }

                pageOnRequest = (Page<?>) attr;
            }
        }

        if (pageOnRequest != null) {
            return pageOnRequest;
        }
    }

    throw new InvalidObjectParameterException("Invalid or not present Page object found on request!");
}
 
Example 2
Source File: PageUtils.java    From thymeleaf-spring-data-dialect with Apache License 2.0 4 votes vote down vote up
private static String buildBaseUrl(final ITemplateContext context, Collection<String> excludeParams) {
    // URL defined with pagination-url tag
    final String url = (String) context.getVariable(Keys.PAGINATION_URL_KEY);

    if (url == null && context instanceof IWebContext) {
        // Creates url from actual request URI and parameters
        final StringBuilder builder = new StringBuilder();
        final IWebContext webContext = (IWebContext) context;
        final HttpServletRequest request = webContext.getRequest();

        // URL base path from request
        builder.append(request.getRequestURI());

        Map<String, String[]> params = request.getParameterMap();
        Set<Entry<String, String[]>> entries = params.entrySet();
        boolean firstParam = true;
        for (Entry<String, String[]> param : entries) {
            // Append params not excluded to basePath
            String name = param.getKey();
            if (!excludeParams.contains(name)) {
                if (firstParam) {
                    builder.append(Q_MARK);
                    firstParam = false;
                } else {
                    builder.append(AND);
                }

                // Iterate over all values to create multiple values per
                // parameter
                String[] values = param.getValue();
                Collection<String> paramValues = Arrays.asList(values);
                Iterator<String> it = paramValues.iterator();
                while (it.hasNext()) {
                    String value = it.next();
                    builder.append(name).append(EQ).append(value);
                    if (it.hasNext()) {
                        builder.append(AND);
                    }
                }
            }
        }

        // Escape to HTML content
        return HtmlEscape.escapeHtml4Xml(builder.toString());
    }

    return url == null ? EMPTY : url;
}
 
Example 3
Source File: PageUtils.java    From thymeleaf-spring-data-dialect with Apache License 2.0 4 votes vote down vote up
private static String getParamPrefix(final ITemplateContext context) {
    final String prefix = (String) context.getVariable(Keys.PAGINATION_QUALIFIER_PREFIX);

    return prefix == null ? EMPTY : prefix.concat("_");
}
 
Example 4
Source File: FullPaginationDecorator.java    From thymeleaf-spring-data-dialect with Apache License 2.0 4 votes vote down vote up
private String createPageLinks(final Page<?> page, final ITemplateContext context) {
    if( page.getTotalElements()==0 ){
        return Strings.EMPTY;
    }
    
    int pageSplit = DEFAULT_PAGE_SPLIT;
    Object paramValue = context.getVariable(Keys.PAGINATION_SPLIT_KEY);
    if (paramValue != null) {
        pageSplit = (Integer) paramValue;
    }

    int firstPage = 0;
    int latestPage = page.getTotalPages();
    int currentPage = page.getNumber();
    if (latestPage >= pageSplit) {
        // Total pages > than split value, create links to split value
        int pageDiff = latestPage - currentPage;
        if (currentPage == 0) {
            // From first page to split value
            latestPage = pageSplit;
        } else if (pageDiff < pageSplit) {
            // From split value to latest page
            firstPage = currentPage - (pageSplit - pageDiff);
        } else {
            // From current page -1 to split value
            firstPage = currentPage - 1;
            latestPage = currentPage + pageSplit - 1;
        }
    }

    StringBuilder builder = new StringBuilder();
    // Page links
    for (int i = firstPage; i < latestPage; i++) {
        int pageNumber = i + 1;
        String link = PageUtils.createPageUrl(context, i);
        boolean isCurrentPage = i == currentPage;
        Locale locale = context.getLocale();
        String li = isCurrentPage ? getLink(pageNumber, locale) : getLink(pageNumber, link, locale);
        builder.append(li);
    }

    return builder.toString();
}