本文专注于采用SpringBoot来开发JSP。
默认啊这个SB框架支持四个视图引擎,我们也都不会用,就会点JSP。所以采用JSP来做视图技术。
步骤:
第一步:创建一个应用,打包方式采用war。这个具体的步骤参考我的上一篇博文:https://blog.youkuaiyun.com/superfreak/article/details/115082381
第二步:外部配置修改application.properties文件,添加如下内容:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
该文件位置如图:
这么做有利于jsp文件的保密,外部不能访问,而控制器却能访问。
第三步:修改你的Controller ,代码如下:
package com.example.springbootwartest;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot,The APP will be packaged to a war!";
}
@RequestMapping("/test")
public String test() {
return "test";
}
@RequestMapping("/showDefaultViewResolver")
public ModelAndView doshowDefaultViewResolver() throws Exception {
ModelAndView showDefaultViewResolver = new ModelAndView();
showDefaultViewResolver.setViewName("test2");
System.out.println("hello springMVC in the showDefaultViewResolver url");
return showDefaultViewResolver;
}
@RequestMapping("/testMethod")
public ModelAndView testMethod() throws Exception{
ModelAndView mv = new ModelAndView();
mv.setViewName("test");
System.out.println("hello springMVC in the testMethod url");
return mv;
}
}
第四步:打包及部署。
程序打包:
打包是在STS中:
对话框中Goals处输入:clean package
点击Run,就会给你打包了。
war包得到后,放入tomcat的webapp目录下,启动你的tomcat,
然后打开浏览器,地址栏输入以下地址来看看结果
1、地址栏输入:http://localhost:8080/spring-boot-war-test-0.0.1-SNAPSHOT/
这样根据我们的controller中的代码,将调用这个方法:
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot,The APP will be packaged to a war!";
}
实际结果也是这样的。
2、地址栏输入:http://localhost:8080/spring-boot-war-test-0.0.1-SNAPSHOT/test
根据代码,调用这个方法:
@RequestMapping("/test")
public String test() {
return "test";
}
实际结果如下:
3、地址栏输入:http://localhost:8080/spring-boot-war-test-0.0.1-SNAPSHOT/testMethod
调用方法:
@RequestMapping("/testMethod")
public ModelAndView testMethod() throws Exception{
ModelAndView mv = new ModelAndView();
mv.setViewName("test");
System.out.println("hello springMVC in the testMethod url");
return mv;
}
这里用到了类ModelAndView ,返回的是一个 ModelAndView,最终它找到了位于WEB-INF中的test.jsp文件
结果如下:
4 地址栏输入:http://localhost:8080/spring-boot-war-test-0.0.1-SNAPSHOT/showDefaultViewResolver
根据代码会调用:
@RequestMapping("/showDefaultViewResolver")
public ModelAndView doshowDefaultViewResolver() throws Exception {
ModelAndView showDefaultViewResolver = new ModelAndView();
showDefaultViewResolver.setViewName("test2");
System.out.println("hello springMVC in the showDefaultViewResolver url");
return showDefaultViewResolver;
}
最终映射为了test2.jsp
结果如下:
我想在test2.jsp中显示一些SB框架给我们的创建的哪些Bean,但是目前没有成功。后续想试一试usebean。
项目中的两个jsp文件的位置和代码如下:
test.jsp内容:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
I am the test jsp page.
</body>
</html>
test2.jsp内容:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import ="org.springframework.context.ApplicationContext" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Show the DefaultViewResolver</title>
</head>
<body>
<p>
I am the test2 jsp
I want to show something of spring Boot Framework ,
Maybe ,I should Use usebean .
<p>
</body>
</html>
最后,对于想用SpringBoot 开发JSP的同志们来说,按照我的博客做以及可以了。
再次奥!