Rest servlet上下文

Rest servlet上下文,rest,servlets,spring-mvc,Rest,Servlets,Spring Mvc,我有下面的web.xml代码 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="

我有下面的web.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>rest.service</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>*.abc</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.form</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
  </welcome-file-list>
  <jsp-config>
  <taglib>
       <taglib-uri>wcm_rates</taglib-uri>
       <taglib-location>/WEB-INF/lib/wcm-rates.tld</taglib-location>
</taglib>
  </jsp-config>  
</web-app>

调度员
org.springframework.web.servlet.DispatcherServlet
1.
泽西岛休息服务
com.sun.jersey.spi.container.servlet.ServletContainer
com.sun.jersey.config.property.packages
休息服务
1.
泽西岛休息服务
*.美国广播公司
调度员
*.表格
redirect.jsp
wcm_费率
/WEB-INF/lib/wcm-rates.tld
在SpringMVCServlet上下文中,我添加了一些属性。我希望在调用任何RESTURL时,这些属性都可以在servlet上下文中使用

但令我惊讶的是,当调用RESTURL时,这些属性并没有出现

我将属性添加为
servletContext.setAttribute(xmlFile.getName(),map)
;拉取为
request.getSession().getServletContext().getAttribute(“test.xml”)

我已验证是否使用相同的属性名称来存储和提取

有人能帮我吗?这样我就可以在SpringMVC和REST之间共享属性了


提前感谢。

您可以直接从请求中获取ServletContext,为什么从会话中获取它

更改自

request.getSession().getServletContext().getAttribute("test.xml")

 request.getServletContext().getAttribute("test.xml")

您可以从以下范围之一设置/获取属性:

  • 请求
  • 会议
  • 应用
  • 如果您使用的是
    servletContext.setAttribute(xmlFile.getName(),map)表示您正在添加到应用程序范围。因此,要检索属性值,请使用
    servletContext.getAttribute(“test.xml”)

    查看下面的链接了解更多详细信息


    向我们展示一些代码:如何设置属性以及如何获取属性。编辑您的问题。不要在评论中发布代码。我猜您得到的属性名称与用于设置属性的名称不同(即文件名可能不是
    “test.xml”
    )。HttpServletRequest中没有方法“getServletContext”。请检查java文档: