从spring控制器中的http上下文中读取变量

时间:2016-08-04 18:06:40

标签: spring http spring-mvc

我想说我想从http上下文中检索一些信息。在我的百里香页面中,我使用了类似的东西:

<span th:text="${#httpServletRequest.getHeader('Accept-Language')}"></span>
但是,在我的百万富翁处理器中,我使用的代码如下:

String language = arguments.getContext().getLocale().getLanguage();
String country = arguments.getContext().getLocale().getCountry();

如何在弹簧控制器中检索相同的信息,实现方式如下?

  @RequestMapping("/insert_texto/{page}")
  public ModelAndView insert_texto(Model model, @PathVariable("page") String id)    {
    ...
  }

1 个答案:

答案 0 :(得分:1)

我会直接在你的控制器中注释servlet请求或上下文:

@Autowired
ServletContext context;

@RequestMapping("/insert_texto/{page}")
public ModelAndView insert_texto(Model model, @PathVariable("page") String id)  
{
    // use context
}

或者,如果您愿意,可以将ServletContext作为变量传递给您的方法:

@RequestMapping("/insert_texto/{page}")
public ModelAndView insert_texto(Model model, @PathVariable("page") String id, ServletContext context)  
{
    // use context
}