I am new in thymeleaf, i want to create dynamic query string using thymeleaf url. For pagination i am using common fragment and include fragment in all pages. There is search option in all pages and also need pagination for search. So, we need to pass, dynamic query string in pagination for searching criteria. But when i create the query string dynamic, the URL encode the special character like =
and &
. Following is my code
<!-- Pagination Bar -->
<div th:fragment='paginationbar(searchArguments)'>
<div class='pagination pagination-centered'>
<ul>
<li th:class="${page.firstPage}? 'disabled' : ''">
<span th:if='${page.firstPage}'> First</span>
<a th:if='${not page.firstPage}' th:href='@{${page.url}(page=0,size=${page.size},${searchArguments})}'>First</a>
</li>
<li th:class="${page.hasPreviousPage}? '' : 'disabled'">
<span th:if='${not page.hasPreviousPage}'><<</span>
<a th:if='${page.hasPreviousPage}' th:href='@{${page.url}(page=${page.currentNumber - 2},size=${page.size},${searchArguments})}' title='Go to previous page'><<</a>
</li>
<li th:each='item : ${page.items}' th:class="${item.current}? 'active' : ''">
<span th:if='${item.current}' th:text='${item.number}'>1</span>
<a th:if='${not item.current}' th:href='@{${page.url}(page=${item.number - 1},size=${page.size},${searchArguments})}'><span th:text='${item.number}'>1</span></a>
</li>
<li th:class="${page.hasNextPage}? '' : 'disabled'">
<span th:if='${not page.hasNextPage}'>>></span>
<a th:if='${page.hasNextPage}' th:href='@{${page.url}(page=${page.currentNumber},size=${page.size},${searchArguments})}' title='Go to next page'>>></a>
</li>
<li th:class="${page.lastPage}? 'disabled' : ''">
<span th:if='${page.lastPage}'>Last </span>
<a th:if='${not page.lastPage}' th:href='@{${page.url}(page=${page.totalPages - 1},size=${page.size},${searchArguments})}'>Last</a>
</li>
</ul>
</div>
</div>
Include the fragment in page:
<div th:include="admin/fragments/pagination-fragment :: paginationbar('title='+${title}+'&status='+${status})" th:if="${title != null}"></div>
When view the source code for page, following URL is create:
<a href="/sample/admin/search-category?page=1&size=5&title%3Dtil%26status%3D"><span>2</span></a>
In above URL the browser convert title='+${title}+'&status='+${status}
into title%3Dtil%26status%3D
. How we set actual query string for pagination link?
Update Thymeleaf Configuration
public class MultiViewResolverConfig{
@Autowired
private Environment env;
/**
* This method is used to create tiles view resolver which have
* view resolver first priority for resolve response view
* */
@Bean
public TilesViewResolver tilesViewResolver() {
TilesViewResolver resolver = new TilesViewResolver();
resolver.clearCache();
resolver.setCache(Boolean.parseBoolean(env.getProperty("tiles.cache")));
resolver.setCacheLimit(Integer.parseInt(env.getProperty("tiles.cache.limit")));
return resolver;
}
/**
* This method is used to configure tiles for application and
* specify which definitions files are used.
* This method create a bean and register.
* */
@Bean
public TilesConfigurer tilesConfigure(){
TilesConfigurer configurer = new TilesConfigurer();
configurer.setDefinitions(new String[]{"classpath:tiles-definitions/user-definitions.xml",
"classpath:tiles-definitions/public-definitions.xml", "classpath:tiles-definitions/admin-definitions.xml"});
//configurer.setValidateDefinitions(true);
//configurer.setCheckRefresh(true);
return configurer;
}
/* Thymeleaf configuration */
private ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setPrefix("/WEB-INF/views/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCacheable(Boolean.parseBoolean(env.getProperty("thm.cache")));
templateResolver.setCacheTTLMs(Long.parseLong(env.getProperty("thm.cache.limit")));
return templateResolver;
}
private SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addDialect(new LayoutDialect());
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}
private ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
thymeleafViewResolver.setCharacterEncoding("UTF-8");
thymeleafViewResolver.setViewClass(ThymeleafView.class);
thymeleafViewResolver.setViewNames(new String[]{"admin/**"});
thymeleafViewResolver.setTemplateEngine(templateEngine());
return thymeleafViewResolver;
}
/* JSP view resolver */
private InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
resolver.setCache(Boolean.parseBoolean(env.getProperty("url.cache")));
resolver.setCacheLimit(Integer.parseInt(env.getProperty("url.cache.limit")));
return resolver;
}
@Bean
public ViewResolver setupViewResolver(ContentNegotiationManager manager) {
List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
resolvers.add(tilesViewResolver());
resolvers.add(thymeleafViewResolver());
resolvers.add(jspViewResolver());
ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver();
viewResolver.setViewResolvers(resolvers);
viewResolver.setContentNegotiationManager(manager);
return viewResolver;
}}
Another way of accessing request parameters in thymeleaf is by using #httpServletRequest utility object which gives direct access to javax. servlet. http. HttpServletRequest object.
Definition and Usage. The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink.
This problem have simple solution, i am trying to create the URL from thymeleaf href
attribute like this:
th:href='@{${page.url}(page=0,size=${page.size},${searchArguments})}'
But there is no need to pass parameter to the thymeleaf th:href
attribute. Just append the query string with your th:href
attribute like this:
th:href='@{${page.url}(page=0,size=${page.size})}+${searchArguments}'
In this, thymeleaf create the URL and we append our dynamic query string with URL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With