eclipsejava,SpringBoot支持JSP教程

 2023-10-06 阅读 32 评论 0

摘要:項目背景 一些前端還是JSP的老項目,需要改造為springboot,所以需要springboot能支持JSP。 項目結構 afei-demo├──src/main???????????????????????????????????????????├???├──java?├???├???├──com.afei.test.demo├???├???├???├──controller?

項目背景

一些前端還是JSP的老項目,需要改造為springboot,所以需要springboot能支持JSP。

項目結構

afei-demo├──src/main???????????????????????????????????????????├???├──java?├???├???├──com.afei.test.demo├???├???├???├──controller?├???├???├???├──service??├???├???├???├──mapper├???├???├???├──Application.java??springboot項目main方法所在的主類?????????????├???├──resources????????????├???├???├──css?存放css文件的地方├???├???├──images?存放.jpg,.png等圖片資源的地方├???├???├──js?存放js文件的地方├???├???├──application.properties?springboot配置文件├???├──webapp????├???├???├──WEB-INF/jsp?這個路徑和視圖配置中的spring.mvc.view.prefix要對應├???├???├???├──index.jsp?├???├???├???├──monitor?監控相關jsp頁面├???├???├???├──warning?告警相關jsp頁面

依賴組件

springboot支持JSP的主要依賴組件如下:

  • spring-boot-starter-web版本為1.5.9.RELEASE;

  • jstl版本為1.2;

  • eclipsejava,tomcat-embed-jasper版本為8.5.23;

  • spring-boot-maven-plugin版本為1.4.2.RELEASE;

核心POM

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--springboot?tomcat?jsp?支持開啟?start--><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId><version>8.5.23</version></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!--springboot?tomcat?jsp?支持開啟?end-->
</dependencies>

插件配置

<build><plugins><plugin><groupId>org.springframework.boot</groupId><!--springboot-maven不能使用1.5.9.RELEASE版本,?否則會導致WEB-INF下的jsp資源無法訪問--><artifactId>spring-boot-maven-plugin</artifactId><version>1.4.2.RELEASE</version><configuration><!--指定springboot的main方法類,否則會由于某些測試類中也有main方法而導致構建失敗--><mainClass>com.yyfax.fdfsfront.Application</mainClass></configuration><executions><execution>?<goals>?<goal>repackage</goal>?</goals>?</execution></executions></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.8</source><target>1.8</target></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>${project.basedir}/lib</directory><targetPath>BOOT-INF/lib/</targetPath><includes><include>**/*.jar</include></includes></resource><resource><directory>src/main/resources</directory><includes><include>**/**</include></includes><filtering>false</filtering></resource></resources></build>

再次強調,spring-boot-maven-plugin的版本是1.4.2.RELEASE,而spring-boot-starter-web的版本是1.5.9.RELEASE。否則會導致即使能成功構建JAR包,也無法訪問JSP資源。這里坑浪費了我不少時間!

靜態資源

resources標簽下的配置非常重要,其作用是將webapp目錄下所有的資源(主要是JSP資源,因為css,js,圖片等資源放在resoures目錄下)拷貝到最終springboot的JAR包中的META-INF/resources目錄下。

  • 為什么是/META-INF/resources目錄?

Spring入門、這個與springboot的配置spring.resources.static-locations有關,其默認賦值在源碼ResourceProperties.java中,由源碼可知,classpath:/META-INF/resources/是其中一個默認的靜態資源存放路徑:

@ConfigurationProperties(prefix?=?"spring.resources",?ignoreUnknownFields?=?false)
public?class?ResourceProperties?implements?ResourceLoaderAware?{private?static?final?String[]?SERVLET_RESOURCE_LOCATIONS?=?{?"/"?};private?static?final?String[]?CLASSPATH_RESOURCE_LOCATIONS?=?{"classpath:/META-INF/resources/",?"classpath:/resources/","classpath:/static/",?"classpath:/public/"?};private?static?final?String[]?RESOURCE_LOCATIONS;static?{RESOURCE_LOCATIONS?=?new?String[CLASSPATH_RESOURCE_LOCATIONS.length+?SERVLET_RESOURCE_LOCATIONS.length];System.arraycopy(SERVLET_RESOURCE_LOCATIONS,?0,?RESOURCE_LOCATIONS,?0,SERVLET_RESOURCE_LOCATIONS.length);System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS,?0,?RESOURCE_LOCATIONS,SERVLET_RESOURCE_LOCATIONS.length,?CLASSPATH_RESOURCE_LOCATIONS.length);}/***?Locations?of?static?resources.?Defaults?to?classpath:[/META-INF/resources/,*?/resources/,?/static/,?/public/]?plus?context:/?(the?root?of?the?servlet?context).*/private?String[]?staticLocations?=?RESOURCE_LOCATIONS;...?...
}

至于其他的css,圖片,js等靜態資源文件,放到resources目錄下即可,并結合如下核心代碼:

@Configuration
@Slf4j
public?class?FdfsWebMvcConfigurerAdapter?extends?WebMvcConfigurerAdapter?{@Beanpublic?SecurityInterceptor?getSecurityInterceptor()?{return?new?SecurityInterceptor();}@Overridepublic?void?addInterceptors(InterceptorRegistry?registry)?{//?除了login登陸訪問路徑和error頁面,其他都要經過SecurityInterceptor攔截器(SecurityInterceptor?extends?HandlerInterceptorAdapter)InterceptorRegistration?addInterceptor?=?registry.addInterceptor(getSecurityInterceptor());//?排除配置addInterceptor.excludePathPatterns("/error");addInterceptor.excludePathPatterns("/login**");//?攔截配置addInterceptor.addPathPatterns("/**");}@Overridepublic?void?addViewControllers(ViewControllerRegistry?registry)?{//?配置welcome默認首頁registry.addViewController("/").setViewName("forward:/main/index.shtml");registry.setOrder(Ordered.HIGHEST_PRECEDENCE);super.addViewControllers(registry);}@Beanpublic?ServletRegistrationBean?servletRegistrationBean(DispatcherServlet?dispatcherServlet)?{ServletRegistrationBean?bean?=?new?ServletRegistrationBean(dispatcherServlet);//?RequestMapping路徑都以.shtml結尾,例如:http://localhost/main/login.shtmlbean.addUrlMappings("*.shtml");//?其他文件該是什么后綴就是什么后綴bean.addUrlMappings("*.html");bean.addUrlMappings("*.css");bean.addUrlMappings("*.js");bean.addUrlMappings("*.png");bean.addUrlMappings("*.gif");bean.addUrlMappings("*.ico");bean.addUrlMappings("*.jpeg");bean.addUrlMappings("*.jpg");return?bean;}@Overridepublic?void?addResourceHandlers(ResourceHandlerRegistry?registry)?{//?這里的配置很重要,由于我們把css,圖片,js等靜態資源文件放在resources目錄下,所以必須增加這些ResourceHandler,才能訪問到這些資源registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/favicon.ico");registry.addResourceHandler("/css/**").addResourceLocations("classpath:/css/");registry.addResourceHandler("/images/**").addResourceLocations("classpath:/images/");registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");registry.addResourceHandler("/500.html").addResourceLocations("classpath:/500.html");registry.addResourceHandler("/index.jsp").addResourceLocations("classpath:/index.jsp");super.addResourceHandlers(registry);}}

視圖配置

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

這里的配置要非常注意,以筆者這兩個配置為例,如果我在controller中return new ModelAndView("main/index");,那么它訪問的資源路徑是/WEB-INF/jsp/main/index.jsp(prefix+ModelAndView+suffix)。這里強烈建議spring.mvc.view.prefix的配置以/結尾,即配置/WEB-INF/jsp/而不要配置/WEB-INF/jsp。否則如果return new ModelAndView("main/index");就會拋出如下錯誤:

File?[/WEB-INF/jspmain/index.jsp]?not?found
  • 總結

  1. 配置為/WEB-INF/jsp/,那么return new ModelAndView("/main/index");return new ModelAndView("main/index");都可以訪問。

  2. eclipse怎么運行javaweb項目。配置為/WEB-INF/jsp,那么只有return new ModelAndView("/main/index");才可以訪問。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/120798.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息