英文:
spring-boot-2, Serving plain old jsp pages WITHOUT controller?
问题
我正在尝试将旧的遗留应用程序从JavaEE转换到spring-boot-2。
如何在没有控制器的情况下实际提供普通的JSP页面?
我已经一遍又一遍地在Google上搜索了,听起来很奇怪,我无法弄清楚或找到一个实际可行的简单示例。每当我尝试访问JSP页面时,我只会得到"404 Not Found"的错误。
我正在使用Maven,将其打包为WAR文件,并且我可以运行它作为"exploded"(这似乎是最好的选择?),但我仍然无法使其工作。
我不明白为什么会这么难,难道在spring-boot-2中已经不可能做到这一点了吗?
有人能够为我指点一个可行的示例吗?
或者一个详细的说明如何操作?
编辑:这是spring文档关于JSP限制的说法:
"使用Jetty和Tomcat,如果您使用war打包,它应该能够工作。"
来自:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
但是怎样呢?这就是我试图弄清楚的...
英文:
I'm trying to convert an old legacy application from JavaEE to spring-boot-2.
How do you actually serve plain old JSP-pages WITHOUT a Controller?
I've been googling this over and over now, and strange as it sounds, I cannot figure it out or find a simple example that actually WORKS. I just keep getting "404 Not Found" when trying to access the JSP.
I'm using maven, packaging as a WAR file and I'm fine running it as 'exploded' (which seems the best bet?), still haven't been able to make it work.
I don't understand how this can be so hard, is it no longer POSSIBLE to do this in spring-boot-2?
Anyone can point me to a working example?
Or a detailed instruction how to?
EDIT: This is what spring docs say re. JSP Limitations:
"With Jetty and Tomcat, it should work if you use war packaging."
From: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
But exactly how? That's what I'm trying to figure out...
答案1
得分: 3
问题在于,默认情况下,Spring Boot 接管了整个世界,即它使用映射为 /
注册了 DispatcherServlet
,从而劫持了对 *.jsp
文件的 servlet 容器处理。
为了让 servlet 容器能够正常处理 JSP,我们需要确保 Spring 的 DispatcherServlet
不会拦截它们,例如通过确保它只处理具有特定后缀或特定前缀的请求。
例如,如果没有任何 JSP 路径以 /api/
开头,那么我们可以将其用于 DispatcherServlet
,即所有对 /api/*
的请求将由 Spring Boot 处理,而任何其他请求将由 servlet 容器处理。
为此,请将以下内容添加到 application.properties
文件中:
spring.mvc.servlet.path = /api
英文:
The problem is that Spring Boot by default takes over the world, i.e. it registers the DispatcherServlet
with a servlet mapping of /
, thereby high-jacking the servlet containers processing of *.jsp
files.
To allow the servlet container to process JSPs normally, we need to make sure the Spring DispatcherServlet
doesn't intercept them, e.g. by ensuring that it only handles requests with certain suffixes, or with certain prefixes.
E.g. if no JSP path starts with /api/
, then we can use that for the DispatcherServlet
, i.e. any request for /api/*
will be handled by Spring Boot, and any other request will be handled by the servlet container.
To do that, add the following to the application.properties
file:
spring.mvc.servlet.path = /api
答案2
得分: -1
你是否在某处声明了Servlet映射?
我相信你需要一个入口点,否则它只是一个指向无处的死文件。
英文:
have you declared the servlet mapping somewhere?
i believe u need a point of entry otherwise is just a dead file pointing to nowhere
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论