如何告诉spring使用maven多模块springboot从自定义目录加载资源

时间:2014-10-25 06:32:56

标签: java maven spring-boot

我尝试构建一个maven多模块Web项目:

project A, project B, ...

B 依赖于 A

运行项目 B ,显示的控制台已映射到/hello,但在检查hello.jsp时找不到localhost:8080/hello

这是我的问题:

  1. 在运行项目B时如何使用hello.jsp( A 中的资源?)

  2. 如何从目录src/main/resources/告诉spring加载资源?

  3. 结构A / B

    A/B
    |_src
    |  |_main
    |  |   |_java
    |  |   |  |_com.x
    |  |   |  |  |_AStarter.java/BStarter.java 
    |  |   |  |_com.x.domain
    |  |   |  |_com.x.services
    |  |   |  |_com.x.web
    |  |   |     |_HelloController.java/FooController.java
    |  |   |_resources
    |  |      |_application.properties
    |  |      |_templates
    |  |         |_hello.jsp
    |  |_test
    |      |_java
    |      |_resources
    |_pom.xml  
    

    (A)pom.xml是一个简单的spring-boot文件。 (二)pom.xml类似于(一)pom.xml只需添加一个依赖:

    <dependency>
        <groupId>X</groupId>
        <artifactId>A</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    

    application.properties:     spring.view.suffix:.jsp


    更新

    启动器:

    @Configuration
    @EnableAutoConfiguration
    @ComponentScan
    public class AStarter extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(
                SpringApplicationBuilder application) {
            return application.sources(AStarter.class);
         }
        public static void run(Class<? extends AStarter> c, String[] args) {
            SpringApplication.run(c, args);
        }
        public static void main(String[] args) throws Exception {
            run(AStarter.class, args);
        }
    }
    
    @Configuration
    @EnableAutoConfiguration
    @ComponentScan
    public class BStarter extends AStarter {
        public static void main(String[] args) {
            AStarter.run(BStarter.class, args);
        }
    }
    

    控制器:

    @Controller
    public class HelloController {
        private String message = "Hello World";
        @RequestMapping("/hello")
        public String welcome(Map<String, Object> model) {
            model.put("time", new Date());
            model.put("message", this.message);
            return "hello";
        }
    }
    
    @Controller
    public class FooController {
        @RequestMapping("/foo")
        public String foo(Map<String, Object> model) {
            model.put("foo", "foo!");
            return "foo";
        }
    }
    

    控制台:

    --- {main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/foo],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.x.FooController.foo(java.util.Map<java.lang.String, java.lang.Object>)
    --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.x.web.HelloController.welcome(java.util.Map<java.lang.String, java.lang.Object>)
    --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
    --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
    

    当我查看localhoust:8080 / hello时,出现错误 enter image description here

    提前致谢!

3 个答案:

答案 0 :(得分:1)

不要将WEB-INF与Spring应用程序一起使用;它总是很脆弱,而且Spring可以在类路径中查看罐子。将您的文件放在src/main/resources/templates中,不要指定任何spring.view.prefix

答案 1 :(得分:0)

你的pom.xml使用<package>war</package>?因为embbed tomcat不支持jsp

用于像freemarker这样的模板引擎,将* .ftl放入类路径的不同折叠中,并自定义FreeMarkerConfigurer

这样的多模块项目:

  Model-A
    src/main/java
    src/main/resources/templates1/hello.ftl
    pom.xml
  Model-B
    src/main/java
      org.lenic.sof.Application.java
    src/main/resources/templates2/index.ftl
    pom.xml(like Model-A additional dependent on Model-A)

和java配置如下:

@Configuration
@ComponentScan
@EnableAutoConfiguration
@Controller
public class Application {
    private String  message = "Hello World";

    @RequestMapping("/hello")
    public String welcome(Map<String, Object> model) {
        model.put("time", new Date());
        model.put("message", this.message);
        return "hello";
    }

    @RequestMapping("/index")
    public String index(Map<String, Object> model) {
        model.put("message", "this is index page");
        return "index";
    }

    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer() {
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        configurer.setDefaultEncoding("UTF-8");
        configurer.setTemplateLoaderPaths("classpath:/templates1/", "classpath:/templates2/");
        return configurer;
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
像这样的hello.ftl:

<html> 
    <head> 
        <title>welcome spring-boot!</title>
    </head> 
    <body>
        ${message}
    </body>
</html>
像这样的 index.ftl:

<html> 
    <head> 
        <title>welcome spring-boot!</title>
    </head> 
    <body>
        ${message}
    </body>
</html>

答案 2 :(得分:0)

您解决了这个问题吗?我遇到了同样的问题,并认为它是由使用maven子模块和JSP弹簧视图解析器组合引起的。

Thymleaf诱惑引擎工作正常,我可以将子项目移动到它自己的项目,它工作正常(不需要代码或配置更改)。