我应该将jsp文件放在spring-boot项目中

问题描述 投票:1回答:1

最近,我开始使用spring-boot,我正在尝试转换我的旧Spring项目,所有这些都是web应用程序,以使用它。我设法编译,打包和运行应用程序,但是当我尝试在浏览器中访问它们时,我无法看到我的观点。

首先,我尝试将jsp页面放在通常的文件夹src / main / webapp / WEB-INF / jsp中,但是从官方文档中读取这篇文章之后:

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-static-content

我尝试将它们放在src / main / resources文件夹中。这些都不起作用。任何人都可以告诉我应该把这些文件放在哪里,以便在应用程序运行时可以访问它们?

我的pom.xml是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.spring</groupId>
    <artifactId>app</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.8.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
           </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
        </dependency>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901-1.jdbc4</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <properties>
        <start-class>com.spring.app.Application</start-class>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我有这个控制器来映射视图:

@Controller
public class AcessoController {

    @RequestMapping(value = "/signin")
    public String signin(Model model) {
        return "acesso/signin";
    }

    @RequestMapping(value = "/admin")
    public String admin(Model model) {
        return "private/admin";
    }

    @RequestMapping(value = "/index")
    public String index(Model model) {
        return "public/index";
    }

}

和这个配置类:

Web app config.Java

@EnableWebMvc
@Configuration
@ComponentScan(value="com.spring.app")
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

Web app initializer.Java

@Order(value=1)
public class WebAppInitializer implements WebApplicationInitializer {

    @SuppressWarnings("resource")
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
          // Create the 'root' Spring application context
          AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
          rootContext.register(WebAppConfig.class);

          // Create the dispatcher servlet's Spring application context
          AnnotationConfigWebApplicationContext jspContext = new AnnotationConfigWebApplicationContext();
          jspContext.register(DispatcherConfig.class);

          // Register and map the dispatcher servlet
          ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(jspContext));
          dispatcher.setLoadOnStartup(1);
          dispatcher.addMapping("/");
    }

}

dispatcher config.Java

@Configuration
@Import(WebAppConfig.class)
public class DispatcherConfig {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

}
spring jsp spring-mvc spring-boot
1个回答
0
投票

如果使用Jetty或Tomcat作为嵌入式servlet容器,只需将包装从jar更改为war,然后使用java -jar启动它...

© www.soinside.com 2019 - 2024. All rights reserved.