书单推荐:成为Java顶级程序员架构师 ,这20来本(高薪)必看点击获取
关于springboot和jsp的说明
springboot不建议使用jsp。建议使用模板引擎。
你的html可以 直接使用jsp渲染
你也可以使用访问静态资源的方式去访问html
你也可以直接读取本地的html资源,使用
OutpuStream响应给客户端
默认情况下,springboot是不支持jsp的访问的。那么本项目提供以下支持:
SpringBoot同时访问html和jsp
SpringBoot以jar包运行
SpringBoot支持thymeleaf
项目结构:
. ├── 1.txt ├── pom.xml ├── springboot-jsp-html.iml └── src ├── main │ ├── java │ │ └── org │ │ └── dreams │ │ └── forepart │ │ ├── Application.java │ │ ├── ViewResolverConfiguration.java │ │ └── controller │ │ └── IndexController.java │ ├── resources │ │ └── application.properties │ └── webapp │ ├── html │ │ └── index.html │ ├── img │ │ └── orcHome.png │ └── jsp │ └── index.jsp └── test └── java
pom参数
<?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>org.dreams</groupId> <artifactId>springboot-jsp-html</artifactId> <version>1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- 热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <scope>test</scope> </dependency> <!--jsp页面使用jstl标签--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--Provided start--> <!--War包部署到外部的Tomcat中已经包含了这些,所以需要添加以下依赖 否则会和内嵌的Tomcat 容器发生冲突 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <!--用于编译jsp--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.2.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> <resources> <!-- 打包时将jsp文件拷贝到META-INF目录下--> <resource> <!-- 指定resources插件处理哪个目录下的资源文件 --> <directory>src/main/webapp</directory> <!--注意此次必须要放在此目录下才能被访问到--> <targetPath>META-INF/resources</targetPath> <includes> <include>**/**</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/**</include> </includes> <filtering>false</filtering> </resource> </resources> </build></project>
多视图实现的视图解析器
package org.dreams.forepart;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.ViewResolver;import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import org.springframework.web.servlet.view.InternalResourceViewResolver;import org.thymeleaf.spring5.SpringTemplateEngine;import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;import org.thymeleaf.spring5.view.ThymeleafViewResolver;import org.thymeleaf.templateresolver.ITemplateResolver;@Configurationpublic class ViewResolverConfiguration { @Configuration//用来定义 DispatcherServlet 应用上下文中的 bean
@EnableWebMvc
@ComponentScan("org.dreams.forepart") public class WebConfig implements WebMvcConfigurer { @Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/jsp/");
resolver.setSuffix(".jsp");
resolver.setViewNames("*");
resolver.setOrder(2); return resolver;
} @Bean
public ITemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setTemplateMode("HTML5");
templateResolver.setPrefix("/");
templateResolver.setSuffix(".html");
templateResolver.setCharacterEncoding("utf-8");
templateResolver.setCacheable(false); return templateResolver;
} @Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver()); // templateEngine
return templateEngine;
} @Bean
public ThymeleafViewResolver viewResolverThymeLeaf() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("utf-8");
viewResolver.setOrder(1);
viewResolver.setViewNames(new String[]{"html/*", "vue/*"}); return viewResolver;
} @Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
} @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/img/**").addResourceLocations("/img/");
registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/" + "/static/");
}
}
}页面跳转逻辑层
package org.dreams.forepart.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class IndexController { @RequestMapping("/jsp") public String jsp(Model model) {
model.addAttribute("message", "this is index jsp page!"); return "index";
} @GetMapping("/html") public String testThemleaf(Model model) {
model.addAttribute("message", "this is index html page!"); return "html/index";
}
}jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>
<title>Title</title></head><body><img src="/img/orcHome.png"/>${message}</body></html>html页面
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>Title</title></head><body><span th:text="${message}"></span></body></html>运行入口
package org.dreams.forepart;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}测试访问
http://localhost:8080/html
来源链接:https://www.orchome.com/1091
转载请注明:谷谷点程序 » springboot同时支持jsp和html,默认情况下,springboot是不支持jsp的访问的