我是百老汇的新手,我想使用百万美元网址创建动态查询字符串。对于分页,我在所有页面中使用公共片段和包含片段。所有页面都有搜索选项,还需要分页搜索。因此,我们需要在分页中传递动态查询字符串以用于搜索条件。但是当我创建动态查询字符串时,URL会对=
和&
这样的特殊字符进行编码。以下是我的代码
<!-- 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>
在页面中包含片段:
<div th:include="admin/fragments/pagination-fragment :: paginationbar('title='+${title}+'&status='+${status})" th:if="${title != null}"></div>
查看页面的源代码时,以下URL为create:
<a href="/sample/admin/search-category?page=1&size=5&title%3Dtil%26status%3D"><span>2</span></a>
在上面的网址中,浏览器会将title='+${title}+'&status='+${status}
转换为title%3Dtil%26status%3D
。我们如何为分页链接设置实际查询字符串?
更新Thymeleaf配置
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;
}}
答案 0 :(得分:4)
此问题有简单的解决方案,我正在尝试从thymeleaf href
属性创建这样的网址:
th:href='@{${page.url}(page=0,size=${page.size},${searchArguments})}'
但是没有必要将参数传递给thymeleaf th:href
属性。只需使用您的th:href
属性附加查询字符串,如下所示:
th:href='@{${page.url}(page=0,size=${page.size})}+${searchArguments}'
在此,thymeleaf创建URL,我们使用URL附加动态查询字符串。