在我们的Web应用程序中,使用Spring MVC 3.2,我们显示了许多不同对象的分页列表,并且列表中其他页面的链接构造如下:
/servlet/path?pageNum=4&resultsPerPage=10&sortOrder=ASC&sortBy=name
Run Code Online (Sandbox Code Playgroud)
虽然URL中可能还有其他请求参数(例如,搜索过滤器).
所以我们有这样的控制器方法:
@RequestMapping(method = RequestMethod.GET, value="/ajax/admin/list")
public String ajaxlistGroups(Model model,
@RequestParam(value="pageNumber",required=false,defaultValue="0") Long pageNumber,
@RequestParam(value="resultsPerPage",required=false,defaultValue="10") int resultsPerPage,
@RequestParam(value="sortOrder",required=false,defaultValue="DESC") String sortOrder,
@RequestParam(value="orderBy",required=false,defaultValue="modificationDate")String orderBy) {
// create a PaginationCriteria object to hold this information for passing to Service layer
// do Database search
// return a JSP view name
}
Run Code Online (Sandbox Code Playgroud)
所以我们最终得到了这个笨拙的方法签名,在应用程序中重复了几次,每个方法都需要创建一个PaginationCriteria对象来保存分页信息,并验证输入.
有没有办法自动创建我们的PaginationCriteria对象,如果这些请求参数存在?例如,将以上内容替换为:
@RequestMapping(method = RequestMethod.GET, value="/ajax/admin/list")
public String ajaxlistGroups(Model model, @SomeAnnotation? PaginationCriteria criteria,
) {
...
}
Run Code Online (Sandbox Code Playgroud)
即,Spring中是否有一种方法可以从常规GET请求中获取requestParams的已定义子集,并自动将它们转换为对象,因此可以在Controller处理程序方法中使用它?我之前只使用过@ModelAttribute,这似乎不对.
谢谢!
我们使用Spring MVC开发了一个标准的Java Web应用程序,并且最近尝试从3.0.6升级到3.2.0.几乎所有的servlet响应都是JSP或Json视图,但有一些是pdf请求,扩展名为"pdf".
在Spring 3.0.6中,我们有了这个设置,取自Spring MVC文档.
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="pdf" value="application/pdf"/>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,与XMLViewResolver结合使用.
更新到3.2.0后,出现故障:
Error creating bean with name' org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0' defined in class path resource [dispatcher-test-servlet.xml]: Invocation of init method failed; nested exception is
java.lang.ClassCastException: java.lang.String cannot be cast to org.springframework.http.MediaType'
Run Code Online (Sandbox Code Playgroud)
在调查了文档和一些博客之后,这个配置似乎有效:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager">
<bean class="org.springframework.web.accept.ContentNegotiationManager">
<constructor-arg>
<list>
<!-- These are evaluated in order -->
<!-- Is there a media type based on suffix? -->
<bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
<constructor-arg>
<map> …Run Code Online (Sandbox Code Playgroud)