如何在javabean中获取Java Web根路径,而无需直接访问ServletContext

发布于 2025-01-29 04:08:12 字数 1296 浏览 2 评论 0 原文

我有一个部署在Java Web应用程序中的Javabean,该应用程序基于CMN,BPMN和DMN等复杂的工作流框架。这里的一点是,我的Javabean是从BPMN调用的,在我看来,就像在REST API端点的背景下,因为我可以从Postman中测试已部署的Javabean。当我从BPMN调用Javabean时,我没有看到ServletContext或JSP或请求对象或类似的任何参考。

我需要修改Javabean来添加一些功能,这些功能需要从Web根路径下的数据文件夹加载JSON数据文件。

我确实使用Dynamic Java Web应用程序和Javabean成功构建了一个示例Web应用程序,并能够从请求对象访问Web根路径,并且我能够按预期加载JSON数据文件。

我现在需要从真正的Java Web应用程序框架中进行同样的操作。我面临的唯一问题是,如果我无法访问httpservlet和其他相关对象,则如何访问Web根路径。

我在示例Web应用程序中使用了下面的API,它们成功工作:

Request.getsession (). Getservletcontext (). Getrealpath ();
Servletconfig.getservletcontext (). Getrealpath ("/");

有关示例项目,请参阅下面的相关回购,以阐明以下问题:

https://github.com/tarekahf/javabeanexample2/tree/master

在上述回购中显示的项目中,我正在从Webroot下的文件夹中加载一个JSON文件。我之所以能够这样做,是因为我可以访问请求对象和 httpservletContext 。请参阅上面存储库中的以下文件中的源代码:

  • src \ com \ app \ app \ appleeeclass.java
  • webcontent \ data \ list.json
  • webcontent \ index.jsp

现在问题现在如何访问ServletContext,如果我看不到它在主Java应用程序中的任何地方定义?

如果从REST API执行线程的上下文中调用Javabean,我如何访问此Javabean中的Webroot路径,以便我可以在root路径下加载所需的JSON数据文件?

塔雷克

I have a JavaBean that is deployed in a Java web application which is based on a complex workflow framework such as CMN, BPMN, and DMN. The point here is that my JavaBean is invoked from a BPMN and it seems to me like it is in a context of a rest API endpoint because I can test the deployed JavaBean from postman. When I invoke the JavaBean from the BPMN, I don't see any reference to ServletContext or JSP or request object or anything like that.

I need to modify the JavaBean to add some feature that requires loading json data file from a data folder under the web root path.

I did build a sample web application successfully using dynamic Java web application and JavaBean and was able to access the web root path from the request object, and I was able to load the json data file as intended.

I need now to do the same from the real Java web application framework. The only issue I'm facing is how to access the web root path if i don't have access to HttpServlet and the other related objects.

I used the APIs below in a sample web application and they worked successfully:

Request.getsession (). Getservletcontext (). Getrealpath ();
Servletconfig.getservletcontext (). Getrealpath ("/");

See the related repo below for the sample project to clarify the question:

https://github.com/tarekahf/JavaBeanExample2/tree/master

In the project presented in the above repo, I'm loading a JSON file from a folder under the webroot. I was able to do so because I have access to the request object and HttpServletContext. See the source code in the following files in the above repo:

  • src\com\app\EmployeeClass.java
  • WebContent\data\list.json
  • WebContent\index.jsp

The question now is how to access the ServletContext if I don't see it defined anywhere in the main java application?

If the JavaBean is invoked from the context of a rest API execution thread, how I can access the webroot path in this JavaBean so that I can load the needed JSON data file under the root path?

Tarek

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面犯桃花 2025-02-05 04:08:12

如果Javabean是CDI管理的,则可以将 ServletContext 注入以下

@Inject
private ServletContext;

方式

: https://stackoverflow.com/questions/15749192/how-do--i-load-a-file-file-from-rom-rom-rom-rom-rom-rom-rom-rom-rom-rom-------------


=

“ 是基于弹簧的,然后使用 @Autowired 注释或实现ServletContexTaware接口,将Servlet注入bean中的适当方法是。有关详细信息,请参阅此信息:

此外,这是如何在Java中读取文件的另一种资源:

https://www.baeldung.com/reading-file-file-in-in-java

If the JavaBean is CDI managed, you can inject the ServletContext with:

@Inject
private ServletContext;

——

If you just want to read a json file in the java code, I would say that you maybe look at this: read ressource from folder


Edit:

Since it is found that the application is based on Spring, then the appropriate method to inject a servlet into a Bean is by using the @Autowired annotation or implementing the ServletContextAware interface. See this for details:
https://roytuts.com/how-to-get-servletcontext-and-servletconfig-object-in-a-spring-bean/

Also, this is another resource for how to read files in Java:

https://www.baeldung.com/reading-file-in-java

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文