springboot其实并不推荐使用jsp作为视图模板,其默认采用Thymeleaf作为模板,出于对其没有研究,故考虑目前阶段仍然使用jsp作为视图模板。下面就展开实践案例过程:

1、首先创建一个jsp页面:
<!DOCTYPE html>

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html lang="en">

<body>
<c:url value="/resources/text.txt" var="url"/>
<spring:url value="/resources/text.txt" htmlEscape="true" var="springUrl" />
Spring URL: ${springUrl} at ${time}
<br>
JSTL URL: ${url}
<br>
Message: ${message}
</body>

</html>

2、在springmvc中我们也需要定义InternalResourceViewResolver来描述相关页面的存放地址等属性,springboot中同样需要进行描述,在application.properties中配置如下:
# viewpage path
spring.mvc.view.prefix=/WEB-INF/jsp/
# suffix of view
spring.mvc.view.suffix=.jsp
# message
application.message=Hello Angel From application

3、如此我们1中新建的页面存放路径为:


4、此时我们需要新增一个控制器类,通过控制控制类转发至我们的请求页面:
package com.shf.springboot.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class WelcomeController {

@Value("${application.message:Hello World}")
private String message = "Hello World";
@GetMapping("/welcome")
public String welcome(Map<String, Object> model) {
model.put("time", new Date());
model.put("message", this.message);
return "welcome";
}
}

5、启动服务通过设定的请求地址访问:

注:可以发现打印的message消息为application.properties中的配置的message内容。
@Value("${application.message:Hello World}"):如果在当前类读取的资源文件中存在对应的key属性,则通过@Value能够获取其值。

6、下面删除application.properties中的message配置,查验结果:

打印信息:

验证发现,没有配置的情况下,则直接读取@Value注解中定义的值。

7、此时发现,开发环境下我们能够正常的访问我们的请求并转发至对应的jsp请求页面,但是我们部署环境如何呢,首先尝试通过打包成jar验证:
直接通过java -jar 方式启动jar服务

通过浏览器访问,

无法正常打开jsp页面请求。非jsp页面转发请求正常


8、通过maven直接打包成war包,然后部署至常规tomcat下:

验证发现404错误,没有找到对应的请求处理,通过继续了解发现启动类可以继承SpringBootServletInitializer类。

9、修改启动类继承自SpringBootServletInitializer,重新打包验证:
package com.shf.SpringBoot1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.shf.springboot.controller.ServerConfig2;

@SpringBootApplication
@EnableConfigurationProperties({ServerConfig.class,ServerConfig2.class})
@ComponentScan(basePackages={"com.shf.SpringBoot1","com.shf.springboot.*"})
public class App
extends SpringBootServletInitializer //这个类的作用与在web.xml中配置负责初始化Spring应用上下文的监听器作用类似,如果需要打成war部署在tomcat下则需要
{
@Autowired
ServerConfig serverConfig;

public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(App.class);
}
}
再次启动服务正常访问:


注:说明SpringBootServletInitializer对比传统的web项目构建,可以理解为web.xml的作用,集成后tomcat容器能够将其作为web项目进行加载。
通过跟踪源码可以发现,其实SpringBootServletInitializer实现了WebApplicationInitializer接口。

10、以上我们采用的是application.properties中配置相关jsp视图解析对应的参数值,那么我们是否可以通过一个普通的Java配置来实现呢,答案是肯定的,首先注释掉application.properties中的配置:
# viewpage path
#spring.mvc.view.prefix=/WEB-INF/jsp/
# suffix of view
#spring.mvc.view.suffix=.jsp

然后新增一个java配置类:
package com.shf.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
public class ViewResolverConfiguration {

@Bean
public InternalResourceViewResolver getJspViewResolver(){
InternalResourceViewResolver jspViewResolver=new InternalResourceViewResolver();
jspViewResolver.setPrefix("/WEB-INF/jsp/");
jspViewResolver.setSuffix(".jsp");
jspViewResolver.setViewClass(JstlView.class);
return jspViewResolver;
}
}
验证请求响应

SpringBoot使用jsp作为视图模板&常规部署的更多相关文章

  1. springboot整合jsp模板

    springboot整合jsp模板 在使用springboot框架里使用jsp的时候,页面模板使用jsp在pom.xnl中需要引入相关的依赖,否则在controller中无法返回到指定页面 〇.搭建s ...

  2. 2016/5/6 thinkphp ①框架 ② 框架项目部署 ③MVC模式 ④控制器访问及路由解析 ⑤开发和生产模式 ⑥控制器和对应方法创建 ⑦视图模板文件创建 ⑧url地址大小写设置 ⑨空操作空控制器 ⑩项目分组

    真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维护项目,十分困难,代码风格不一样) 项目稳 ...

  3. SpringBoot整合Jsp和Thymeleaf (附工程)

    前言 本篇文章主要讲述SpringBoot整合Jsp以及SpringBoot整合Thymeleaf,实现一个简单的用户增删改查示例工程.事先说明,有三个项目,两个是单独整合的,一个是将它们整合在一起的 ...

  4. [Spring MVC] - JSP + Freemarker视图解释器整合

    Spring MVC中如果只使用JSP做视图,可以使用下面这段即可解决: <!-- 视图解释类 --> <bean class="org.springframework.w ...

  5. SpringBoot Web开发(4) Thymeleaf模板与freemaker

    SpringBoot Web开发(4) Thymeleaf模板与freemaker 一.模板引擎 常用得模板引擎有JSP.Velocity.Freemarker.Thymeleaf SpringBoo ...

  6. SpringBoot入门篇--Thymeleaf引擎模板的基本使用方法

    我们在使用SpringBoot框架的时候在前面已经介绍了Thymelea引擎模板,因为SpringBoot对JSP惨不忍睹的支持.那我们在使用引擎模板对前端页面进行渲染能够返回的情况下我们怎么才能在静 ...

  7. SpringBoot入门篇--使用Thymeleaf模板引擎进行页面的渲染

    在做WEB开发的时候,我们不可避免的就是在前端页面之间进行跳转,中间进行数据的查询等等操作.我们在使用SpringBoot之前包括我在内其实大部分都是用的是JSP页面,可以说使用的已经很熟悉.但是我们 ...

  8. [Spring MVC] - JSP + Freemarker视图解释器整合(转)

    Spring MVC中如果只使用JSP做视图,可以使用下面这段即可解决: <!-- 视图解释类 --> <bean class="org.springframework.w ...

  9. 【7】Django网页视图模板处理

    天下难事必作於易.天下大事必作於细.是以圣人终不为大,故能成其大 --老子<道德经> 本节内容 HTML页面的渲染 使用页面模板 异常处理 超链接路径处理 路由命名空间 1. HTML页面 ...

随机推荐

  1. 关于Altium Designer的BOM,元件清单

    在生成BOM列表的时候,要记得调整BOM的表格的宽度,以免显示不全, 还有就是BOM列表一共有 comment栏 ,description栏,designator栏,footprint栏,libref ...

  2. 从零开始使用git第三篇:git撤销操作、分支操作和常见冲突

    从零开始使用git 第三篇:git撤销操作.分支操作和常见冲突 第一篇:从零开始使用git第一篇:下载安装配置 第二篇:从零开始使用git第二篇:git实践操作 第三篇:从零开始使用git第三篇:gi ...

  3. anaconda中实现双spyder版本

    1)先在conda中创建一个名为python2的环境,并下载对应版本python2.7 conda create --name python27 python=2.7 2)激活python2环境 ac ...

  4. 29、从零写USB摄像头驱动之通过urb接受数据后上报数据是函数中fid的作用

    原因分析如下: 视频数据是由一帧一帧数据组成,为了防止数据错乱,会给每一帧数据分配一个frameid,从第0帧开始,接着是第1帧,接着又是第0帧这样交错进行的,对usb摄像头来说每一帧数据来源于多个包 ...

  5. 得到INI文件所有Section(所有节点名称)

    char SectionNames[MAX_PATH],*pSectionName; ZeroMemory(SectionNames,MAX_PATH); GetPrivateProfileSecti ...

  6. 【SPOJ QTREE】树链剖分模板

    用线段树求解,这里注意因为求的是路径最大值,注意一下细节. #include<cstdio> #include<cstring> #include<algorithm&g ...

  7. 关于JavaScript概念的总结

    原文 https://www.jianshu.com/p/1e8d8a691aa8 大纲 1.JavaScript的概念 2.JavaScript 特点 3.JavaScript是弱类型语言 4.Ja ...

  8. 特征点提取之Harris角点提取法

    1. 特征点提取的意义 2.角点 3. Harris角点检測的基本原理 4.Harris角点检測算法的步骤 5.Harris角点提取算法设计 <span style="font-siz ...

  9. Sphinx+MySQL5.1x+SphinxSE+mmseg中文分词

    什么是Sphinx Sphinx 是一个全文检索引擎,一般而言,Sphinx是一个独立的搜索引擎,意图为其它应用提供快速.低空间占用.高结果相关度的全文搜索功能.Sphinx能够很easy的与SQL数 ...

  10. jquery-12 jquery常用动画效果有哪些

    jquery-12 jquery常用动画效果有哪些 一.总结 一句话总结:jquery可以用户animate()自定义动画,也可以slide和fade系列方法来设置动画. 1.动画效果如何设置执行时间 ...