SpringBoot使用jsp作为视图模板&常规部署

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;
}
}
验证请求响应

原文地址:https://www.cnblogs.com/jpfss/p/9719515.html