【问题标题】:How can I migrate jsp-servlet to spring-boot?如何将 jsp-servlet 迁移到 spring-boot?
【发布时间】:2016-12-09 10:01:21
【问题描述】:

我正在将基于标准 Servlet (web.xml) 的应用程序迁移到 spring-boot 应用程序。

我们不使用spring-mvc,web.xml配置有700多行。我没有找到任何加载 web.xml 的解决方案。

我尝试迁移我拥有的所有 servlet/过滤器,但在没有实际 servlet 类的情况下遇到了 servlet 问题:

<servlet>
    <servlet-name>index</servlet-name>
    <jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>index</servlet-name>
    <url-pattern>/index</url-pattern>
</servlet-mapping>

ServletRegistrationBeanJspServlet 都不能从该文件中创建。

假设我映射了其他 servlet,如何迁移此类 Servlet?

【问题讨论】:

  • 如果您开始使用 Spring MVC,您可以使用您的 JSP 作为应用程序的视图。

标签: java spring jsp servlets spring-boot


【解决方案1】:

使用控制器

    @SpringBootApplication
    public class SpringBootWebApplication extends SpringBootServletInitializer {

        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(SpringBootWebApplication.class);
        }

        public static void main(String[] args) throws Exception {
            SpringApplication.run(SpringBootWebApplication.class, args);
        }

    }

@Controller
public class IndexController {
    @RequestMapping("/index")
    public String index(Model model) {
        model.addAttribute("name", name);
        return "index";
    }
}

在 application.properties 中

spring.mvc.view.prefix: /
spring.mvc.view.suffix: .jsp

【讨论】:

    【解决方案2】:

    您需要扩展SpringBootServletInitializer 类并使用它来代替web.xml。您可以通过它准确地将所有内容映射到您的需求。

    这将被 Spring Boot 自动拾取。

    【讨论】:

    • 我已经完成了,问题是如何在没有 servlet 类的情况下迁移 servlet?它只有jsp-file