【问题标题】:Add Extra parameter to HttpServletRequest向 HttpServletRequest 添加额外参数
【发布时间】:2016-06-17 13:05:38
【问题描述】:

spring 4有没有办法给request对象添加参数,

喜欢

public Map<String,Object> myMethod(HttpServletRequest request){
      request.setParameter("myKey", "myValue"); // It dont have this kind of method
}

任何帮助将不胜感激

【问题讨论】:

  • HttpServletRequest 不是 Spring 类。为什么你需要这个?
  • 我们在 web 应用中使用 spring 框架,它是一个 HttpServletRequest servlet api 类,我需要在请求对象中添加一些参数
  • 为什么需要给请求对象加参数?你用它做什么?
  • 这个问题可能对你有帮助:stackoverflow.com/questions/190833/…

标签: java spring spring-mvc


【解决方案1】:

HttpServletRequestobject 无法更改。 但是您可以使用HttpServletRequest.setAttribute() 方法为其添加其他属性。 当您将请求转发到另一个页面时,这很有用:

RequestDispatcher dispatcher = httpServletRequest
    .getRequestDispatcher("/url/to/new/page");
dispatcher.forward(httpServletRequest, httpServletResponse);

然后,接收 servlet 可以读取您设置的附加属性。 它必须在同一个服务器实例上的同一个 Web 应用程序中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多