如何为请求参数自定义拆分模式 [英] How to customize split-pattern for request parameter
问题描述
我的问题是我有以下网址:
My problem is that I have the following URL:
http://localhost:8080/shiSolrClient/app/shi/search?q=xyz&fq=author:"Max, Muster"
我有一个映射这些requestParameters的bean:
I have a bean that maps these requestParameters:
public class SearchParams {
private String q = "";
private String[] fq;
// getters goes here...
}
我的问题是Spring会自动在逗号上分割fq参数.因此,在我的bean中,fq中有两个String:
My Problem is that Spring automatically split the fq-parameter on the comma. So in my bean there are two Strings in fq:
String[0]: author:"Max
String[1]: Muster"
我不要这种行为.我想要的是告诉Spring将'&'-令牌拆分为','-令牌.例如.
I don't want this behaviour. What I want is to tell Spring to split on '&'-tokens not on ','-tokens. E.g.
http://localhost:8080/shiSolrClient/app/shi/search?q=xyz&fq=author:"Max, Muster"&content:"someContent"
fq=
String[0]: author:"Max, Muster"
String[1]: content:"someContent"
谁能告诉我如何在Spring MVC 3中将其归档
Can anyone tell me how to archive this in Spring MVC 3
我的控制器如下:
@RequestMapping(value = "search", method = RequestMethod.GET)
public String search(SearchParams searchParams, BindingResult bindResult, Model
model) {
SolrQuery solrQ = getBasicQuery(searchParams).setQuery(searchParams.getQ());
for(String fq : searchParams.getFq()) {
solrQ.setParam("fq", fq);
}
try {
QueryResponse rsp = getSolrServer().query(solrQ);
model.addAttribute("solrResults", transformResults(rsp.getResults(),
rsp.getHighlighting(), searchParams, rsp));
model.addAttribute("facetFields", transformFacets(rsp.getFacetFields(),
rsp.getFacetDates(), searchParams));
model.addAttribute("pagination", calcPagination(searchParams,
rsp.getResults()));
...
}
我的Spring-Config看起来像这样:
And my Spring-Config looks like this:
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Configure Apache Tiles for the view -->
<beans:bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<beans:property name="definitions">
<beans:list>
<beans:value>/WEB-INF/views/layout/layouts.xml</beans:value>
<beans:value>/WEB-INF/views/hitlist/views.xml</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<beans:property name="requestContextAttribute" value="requestContext"/>
<beans:property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</beans:bean>
<beans:bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<beans:property name="basenames">
<beans:list>
<beans:value>global</beans:value>
<beans:value>hitlist</beans:value>
<beans:value>local/messages</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<!-- Scans within the base package of the application for @Components to configure
as beans -->
<context:component-scan base-package="com.shi.solrclient.web" />
推荐答案
我在尝试将@RequestParameter与List一起使用时遇到了类似的问题(它总是被逗号分隔.
I ran into a similar problem trying to use a @RequestParameter with List (it kept getting chopped up at commas.
我想出的解决方案是在控制器方法中使用WebRequest参数,这使我可以使用request.getParameterValues("blar")
The solution I came up with was to use a WebRequest parameter in my controller method, which allows me to use request.getParameterValues("blar")
@RequestMapping(value = "search", method = RequestMethod.GET)
public String search(WebRequest req, BindingResult bindResult, Model model) {
String[] searchParams = req.getParameterValues("fq")
这篇关于如何为请求参数自定义拆分模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!