简体   繁体   English

如何从 Springboot 服务 *.jsp

[英]How to serve *.jsp from Springboot

I am trying to convert a legacy JSP app to be hosted within a Springboot 2.3.1 app.我正在尝试将旧版 JSP 应用程序转换为托管在 Springboot 2.3.1 应用程序中。 I would like to have the.jsp files served up when referenced with a.jsp extension.当使用 .jsp 扩展名引用时,我希望提供 .jsp 文件。 I understand that they should go via the DispactherServlet to get the view mapped correctly, but this is not working.我知道他们应该通过 DispactherServlet go 来正确映射视图,但这不起作用。

I have setup the following in application.properties:我在 application.properties 中设置了以下内容:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

I have also set up a controller class with the following mapping:我还使用以下映射设置了 controller class:

    @GetMapping({"/", "/login"})
    public String login(Model model) {
      return "login";
    }

This allows me to resolve references to either "/" or "/login" successfully as the logs show:这使我可以成功解析对“/”或“/login”的引用,如日志所示:

o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
o.s.web.servlet.DispatcherServlet        : Detected StandardServletMultipartResolver
o.s.web.servlet.DispatcherServlet        : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
o.s.web.servlet.DispatcherServlet        : Completed initialization in 18 ms
o.s.web.servlet.DispatcherServlet        : GET "/", parameters={}
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to itree.m5cb.MainController#login(Model)
o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
o.s.web.servlet.view.JstlView            : View name 'login', model {}
o.s.web.servlet.view.JstlView            : Forwarding to [/WEB-INF/jsp/login.jsp]
org.apache.jasper.servlet.JspServlet     : JspEngine --> /WEB-INF/jsp/login.jsp
org.apache.jasper.servlet.JspServlet     :            ServletPath: /WEB-INF/jsp/login.jsp
org.apache.jasper.servlet.JspServlet     :               PathInfo: null
org.apache.jasper.servlet.JspServlet     :               RealPath: null
org.apache.jasper.servlet.JspServlet     :             RequestURI: /WEB-INF/jsp/login.jsp
org.apache.jasper.servlet.JspServlet     :            QueryString: null
o.s.web.servlet.DispatcherServlet        : Completed 200 OK

But if I try to resolve anything ending in.jsp, the DispatcherServlet is not called.但是,如果我尝试解决任何以.jsp 结尾的问题,则不会调用 DispatcherServlet。 The JspServlet is called directly without resolving the path to the /WEB-INF/jsp directory:直接调用JspServlet,不解析/WEB-INF/jsp目录的路径:

org.apache.jasper.servlet.JspServlet     : JspEngine --> /login.jsp
org.apache.jasper.servlet.JspServlet     :            ServletPath: /login.jsp
org.apache.jasper.servlet.JspServlet     :               PathInfo: null
org.apache.jasper.servlet.JspServlet     :               RealPath: null
org.apache.jasper.servlet.JspServlet     :             RequestURI: /login.jsp
org.apache.jasper.servlet.JspServlet     :            QueryString: null
o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
o.s.web.servlet.view.JstlView            : View name 'error', model {timestamp=Thu Jul 23 16:00:23 AEST 2020, status=404, error=Not Found, message=, path=/login.jsp}
o.s.web.servlet.view.JstlView            : Forwarding to [/WEB-INF/jsp/error.jsp]
org.apache.jasper.servlet.JspServlet     : JspEngine --> /WEB-INF/jsp/error.jsp
org.apache.jasper.servlet.JspServlet     :            ServletPath: /WEB-INF/jsp/error.jsp
org.apache.jasper.servlet.JspServlet     :               PathInfo: null
org.apache.jasper.servlet.JspServlet     :               RealPath: null
org.apache.jasper.servlet.JspServlet     :             RequestURI: /WEB-INF/jsp/error.jsp
org.apache.jasper.servlet.JspServlet     :            QueryString: null
o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

Is there a way to configure Springboot so I can also resolve.jsp files?有没有办法配置Springboot,所以我也可以解析.jsp文件?

Here is the web.xml.这是 web.xml。 I have tried with and without the servlet definition:我尝试过使用和不使用 servlet 定义:

<?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"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <display-name>MyApp</display-name>

    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>app</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

I think the issue is that when you provide the url like something:portno/login.jsp then springboot does not match the url with your dispatcher controller method instead sees it as a request to serve the jsp file directly and hence try to resolve the view directly from your static file directory without the prefix and suffix added , so if you have a login.jsp in your static folder this should work but since you want a dynamic jsp to be returned I think you need to decide on whether to keep the extension in your url or not. I think the issue is that when you provide the url like something:portno/login.jsp then springboot does not match the url with your dispatcher controller method instead sees it as a request to serve the jsp file directly and hence try to resolve the view directly from your static file directory without the prefix and suffix added , so if you have a login.jsp in your static folder this should work but since you want a dynamic jsp to be returned I think you need to decide on whether to keep the extension在您的 url 中与否。 Usually what happends is that the container gets the request sees it as a url that needs to be resolved by the dispatcher forwards it and then the dispatcher servlet will resolve the view through a view resolver that adds the prefix and the suffix to resolve the view.通常发生的情况是容器收到请求时将其视为 url 需要由调度程序解析并转发它,然后调度程序 servlet 将通过添加前缀和后缀的视图解析器解析视图以解析视图。 For you this is not happening and I think it's because the container sees the url as not matching any endpoint and as a request to fetch the view.对您来说,这没有发生,我认为这是因为容器将 url 视为不匹配任何端点并作为获取视图的请求。

Try:尝试:

@GetMapping({"/", "/login","/login.jsp"})
public String login(Model model) {
  return "login";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM