【发布时间】:2021-08-10 09:57:08
【问题描述】:
我一直在做一个用 spring initialzr 初始化的 spring-boot 项目。生成的包没有/webapp目录,因此必须添加/webapp目录。我从 spring 文档中读到 spring 检测来自/static、resources 的静态文件。我放置了 3 个不同的 index.jsp 来测试我的控制器显示哪个。下面是代码sn-ps。
目录树:
├── HELP.md
├── mvnw
├── mvnw.cmd
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── databasedisplay
│ │ │ └── app
│ │ │ ├── AppApplication.java
│ │ │ ├── config
│ │ │ │ ├── WebAppInitializer.java
│ │ │ │ └── WebConfig.java
│ │ │ ├── controller
│ │ │ │ ├── HomeController.java
│ │ │ │ └── IndexController.java
│ │ │ ├── repository
│ │ │ └── ServletInitializer.java
│ │ ├── resources
│ │ │ ├── application.properties
│ │ │ ├── index.jsp
│ │ │ ├── static
│ │ │ │ └── index.jsp
│ │ │ └── templates
│ │ └── webapp
│ │ └── WEB-INF
│ │ └── views
│ │ └── index.jsp
│ └── test
│ └── java
│ └── com
│ └── databasedisplay
│ └── app
│ └── AppApplicationTests.java
└── target
├── classes
│ ├── application.properties
│ ├── com
│ │ └── databasedisplay
│ │ └── app
│ │ ├── AppApplication.class
│ │ ├── config
│ │ │ ├── WebAppInitializer.class
│ │ │ └── WebConfig.class
│ │ ├── controller
│ │ │ ├── HomeController.class
│ │ │ └── IndexController.class
│ │ └── ServletInitializer.class
│ ├── index.jsp
│ └── static
│ └── index.jsp
├── generated-sources
│ └── annotations
├── generated-test-sources
│ └── test-annotations
├── maven-status
│ └── maven-compiler-plugin
│ ├── compile
│ │ └── default-compile
│ │ ├── createdFiles.lst
│ │ └── inputFiles.lst
│ └── testCompile
│ └── default-testCompile
│ ├── createdFiles.lst
│ └── inputFiles.lst
└── test-classes
└── com
└── databasedisplay
└── app
└── AppApplicationTests.class
index.jsp(在“/resources”中)
<html>
<head></head>
<body>
<h1>This is the body of the sample view in /resources</h1>
</body>
index.jsp(在“/static”中)
<html>
<head></head>
<body>
<h1>This is the body of the sample view in /static</h1>
</body>
</html>
index.jsp(在'/WEB-INF/views/'中)
<html>
<head></head>
<body>
<h1>This is the body of the sample view in WEB-INF/views</h1>
</body>
</html>
控制器
@Controller
public class IndexController {
@RequestMapping(value = "/indexA", method = RequestMethod.GET)
public String index() {
return "index";
}
}
配置类
WebConfig.java
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver
= new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable("testServlet");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/").setCachePeriod(3600)
.resourceChain(true).addResolver(new PathResourceResolver());
}
}
WebInitializer.java
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.scan("com.databasedisplay.app");
container.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = container.addServlet("mvc", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
问题是当我使用mvn spring-boot:run 或mvn clean package spring-boot:run 运行时,目录树中显示的\target 目录没有来自\WEB-INF\views\ 的index.jsp(实际上目标目录没有@987654338 @ 目录)。但是当我 curl http://localhost:8080/indexA 时,我仍然得到以下输出:
This is the body of the sample view in WEB-INF/views
-
有人可以解释视图解析器如何将视图名称映射到相应的视图吗? (我已经研究了
InternalResourceViewResolver以及如何设置前缀和后缀,但这仍然不能解释它如何在 jsp 不在目标中时呈现它) -
有人能指出
mvn spring-boot:run和mvn clean package spring-boot:run之间的区别吗,因为后者在目标中有WEB-INF 目录。 -
为什么我得到的
index.jsp对应于/WEB-INF而不是curl上的其他视图?
【问题讨论】:
-
为什么要使用 JSP 而不是 .HTML 和 JavaScript 模板?这就是 src/main/resources/static 目录的用途。根据一些文档:“尽管传统应用程序的 Boot 仍然支持 Java 服务器页面 (JSP) 等长期建立的标准,但大多数当前应用程序要么利用仍在不断发展和维护的模板引擎支持的更强大的视图技术,要么转移前端开发到 HTML 和 JavaScript 的组合。甚至可以成功地混合这两个选项并发挥各自的优势。"
-
好点@kenneth,将按照建议进行更改,但仍然对应用程序的行为感到好奇。
标签: spring-boot maven jsp