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. 【AtCoder Regular Contest 082】Derangement

    [链接]点击打开链接 [题意] 在这里写题意 [题解] 贪心. 连续一块的p[i]==i的话,对答案的贡献就应该为(这个连续块的长度+1)/2; 长度为1的也正确. (也即两两相邻的互换位置.) [错 ...

  2. MySQL系列之七:主从复制(转)

    一:实验环境 IP 操作系统 mysql版本号 master 192.168.25.11 CentOS7 5.6.35 slave 192.168.25.10 win10 5.7.18 slave版本 ...

  3. Ext常用控件

    多选下拉框 var workname = new Ext.form.MultiSelect({ store: pointComboBoxStore, fieldLabel: '工作面', labelS ...

  4. VMware Ubuntu安装具体过程

    不是每个程序猿都必须玩过linux,仅仅是博主认为如今的非常多server都是linux系统的,而自己属于那种前端也搞.后台也搞,对框架搭建也感兴趣,可是非常多生产上的框架和工具都是安装在server ...

  5. [D3] Basic Interactivity with D3 v4

    Data visualizations are a lot more interesting when they’re interactive. Whether it’s clicks, roll o ...

  6. 仿招商银行载入loading效果

    在招商银行android手机app中.有例如以下图所看到的的loading载入效果: 实现这个效果还是比較简单,就是自己定义dialog,设置自己想要的布局.然后设置旋转动画. 主要步骤: 1,写布局 ...

  7. [慕课笔记] node+mongodb建站攻略

    如何利用node+mongodb来快速搭建一个电影网站? 一:后端部分 整个网站的后端是由node.js来驱动的,所以在后端需要安装node.js,以及在这个基础之上的框架express,它能够帮助我 ...

  8. jQuery实现多种切换效果的图片切换的五款插件

    1:Nivo SliderNivoslider:丰富的图片切换效果 官方网址:https://themeisle.com/plugins/nivo-slider 查看演示:https://www.he ...

  9. JSP自己定义标签

    JSP自己定义标签 API文档: http://docs.oracle.com/javaee/7/api/ watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ ...

  10. 段的创建表user_segments 分类: H2_ORACLE 2013-08-10 11:13 714人阅读 评论(0) 收藏

    1.段的定义及类型 Oracle中的段(segment)是占用磁盘空间的一个对象,最常见的段类型包括: l  聚簇cluster l  表table l  表分区 tablepartition l  ...