【问题标题】:Where I should place the jsp files in a spring-boot project我应该将jsp文件放在spring-boot项目中的位置
【发布时间】:2014-12-21 11:01:48
【问题描述】:

最近,我开始使用 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";
    }

}

还有这个配置类:

WebAppConfig.java

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

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

}

WebAppInitializer.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("/");
    }

}

DispatcherConfig.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;
    }

}

【问题讨论】:

  • 为什么要导入WebAppConfig,它可能会复制应用程序中的所有bean。 WebApplicationInitializer 在普通的 Spring Boot 应用程序中也没有任何作用(除非你没有使用 Spring Boot)。您应该有一个应用程序类,或者当您想要部署它时使用 SpringBootServletInitializer 代替。从您过去 2 天提出的所有问题来看,我强烈建议您阅读 Spring Boot 以及如何使用它,而不是反复试验并将您的问题转储到此处。
  • 不要使用 JSP!使用真正的模板引擎,例如 Thymeleaf

标签: spring jsp spring-mvc spring-boot


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-10-23
    • 2011-11-30
    • 2010-10-11
    • 2014-12-12
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    相关资源
    最近更新 更多