gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:22:42 25 4
gpt4 key购买 nike

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

项目 A,项目 B,...

B依赖于A

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

这是我的问题:

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

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

结构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 的文件。(B)pom.xml 与 (A)pom.xml 类似,只是添加一个依赖项:

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

应用程序属性: spring.view.后缀:.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 :

@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)

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

提前致谢!

最佳答案

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

关于java - 如何使用maven多模块springboot告诉spring从自定义目录加载资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26559913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com