简体   繁体   English

Spring MVC @RequestParam和@SessionAttribute

[英]Spring MVC @RequestParam and @SessionAttribute

I'm new to Spring and Portlet development. 我是Spring和Portlet开发的新手。

I'm using the spring-webmvc-portlet but I think the workflow is almost the same with servlets. 我正在使用spring-webmvc-portlet,但我认为servlet的工作流程几乎相同。

My use case is pretty common but it's still a bit of a struggle to make everything work the way i would like to. 我的用例很普遍,但要使所有功能都按我希望的方式工作仍然有些困难。

I got a controller like this : 我有一个像这样的控制器:

@Controller
@RequestMapping(value = "VIEW")
@SessionAttributes(value = "persons")
public class MyController

// Display a list of person in personList.jsp
@RenderMapping(params = "actionPerson=query")
public String showList() {
  return "personList";
}

@ActionMapping(params = "actionPerson=query"){

  // Here is a trick I use to avoid the MissingPortletRequestParameterException
  // to be raised 
  response.setRenderParameter("query","")
  response.setRenderParameter("actionPerson","query");
}

// Return a list of person and store it in the model
@ModelAttribute("persons")
public List<Person> getPersons(@RequestParam(value="query") String query) {
  List<Person> personList = personService.SearchByName(query);
  return personList;
}

So far i understand that methods annoted with @ModelAttribute are invoked every time to generate the model before any rendering process. 到目前为止,我了解每次使用任何呈现过程之前,都会调用带有@ModelAttribute注释的方法来生成模型。

I don't want this method to invoked every time. 我不希望每次都调用此方法。 I tried to use the annotation @SessionAttributes to store my list of person in the handler's conversational state. 我尝试使用注释@SessionAttributes将我的人员列表存储在处理程序的对话状态中。

My problem is the annotation @RequestParam raise the following expection : 我的问题是注释@RequestParam引起以下期望:

org.springframework.web.portlet.bind.MissingPortletRequestParameterException: Required String parameter 'query' is not present
at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter$PortletHandlerMethodInvoker.raiseMissingParameterException(AnnotationMethodHandlerAdapter.java:559)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestParam(HandlerMethodInvoker.java:514)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:353)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:155)
at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:369)
at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter.doHandle(AnnotationMethodHandlerAdapter.java:356)
at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter.handleRender(AnnotationMethodHandlerAdapter.java:296)
at org.springframework.web.portlet.DispatcherPortlet.doRenderService(DispatcherPortlet.java:764)
at org.springframework.web.portlet.FrameworkPortlet.processRequest(FrameworkPortlet.java:537)
at org.springframework.web.portlet.FrameworkPortlet.doDispatch(FrameworkPortlet.java:483)
at javax.portlet.GenericPortlet.render(GenericPortlet.java:248)

My guess is even if the method getPersons is not invoked. 我的猜测是即使没有调用getPersons方法。 The @RequestParam(value="query) is still evaluated. @RequestParam(value =“ query)仍然被评估。

When I display my list of person, users can select a person to get detailed informations. 当我显示人员列表时,用户可以选择一个人员以获取详细信息。 I would like them to go back to the list of result without submitting a new query. 我希望他们不提交新查询就返回结果列表。

How can i handle this case properly ? 我如何正确处理此案? Is there a way to skip @RequestParam to be evaluated ? 有没有一种方法可以跳过@RequestParam进行评估?

Thanks ! 谢谢 !

You could make @RequestParam as not required . 您可以as not required @RequestParam This would allow your request to succeed, but query would then be null. 这将使您的请求成功,但是query将为null。

@ModelAttribute("persons")
public List<Person> getPersons(@RequestParam(value="query" , required=false) String query) {
  List<Person> personList = personService.SearchByName(query);
  return personList;
}

For more details on RequestParam 有关RequestParam的更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM