Spring Boot集成JSP、Freemarker


pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>SpringBootExample3_Freemarker</groupId>
  <artifactId>SpringBootExample3_Freemarker</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>SpringBootExample3_Freemarker</name>
  <url>http://maven.apache.org</url>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
  </parent>
  <dependencies>  
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
	<!-- Spring Boot支持JSP的依赖 -->
    <dependency>
	  <groupId>org.apache.tomcat.embed</groupId>
	  <artifactId>tomcat-embed-jasper</artifactId>
	</dependency>
	<!-- Spring Boot支持Freemarker的依赖 -->
	<dependency>
      <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-starter-freemarker</artifactId>
	</dependency>
	<dependency>
	  <groupId>junit</groupId>
	  <artifactId>junit</artifactId>
	  <version>4.12</version>
	</dependency>
  </dependencies>
</project>
banner.txt
  
//                          _ooOoo_                               //  
//                         o8888888o                              //  
//                         88" . "88                              //  
//                         (| ^_^ |)                              //  
//                         O\  =  /O                              //  
//                      ____/`---'\____                           //  
//                    .'  \\|     |//  `.                         //  
//                   /  \\|||  :  |||//  \                        //  
//                  /  _||||| -:- |||||-  \                       //  
//                  |   | \\\  -  /// |   |                       //  
//                  | \_|  ''\---/''  |   |                       //  
//                  \  .-\__  `-`  ___/-. /                       //  
//                ___`. .'  /--.--\  `. . ___                     //  
//              ."" '<  `.___\_<|>_/___.'  >'"".                  //  
//            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                 //  
//            \  \ `-.   \_ __\ /__ _/   .-` /  /                 //  
//      ========`-.____`-.___\_____/___.-`____.-'========         //  
//                           `=---='                              //  
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //  
//            佛祖保佑       永不宕机     永无BUG                  //  
SpringBootMain.java
package cn.et;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/*
@SpringBootApplication继承了@EnableAutoConfiguration、@ComponentScan
    @EnableAutoConfiguration主要是用来开启自动配置是扫描jar包,配置了META-INF/spring.factories
里面的类和针对当前包以及子包下的自定义组件
	@ComponentScan扫描当前类所在包(包括子包)类的注解
 */
@SpringBootApplication
public class SpringBootMain {
   public static void main(String[] args) throws Exception {
	   SpringApplication.run(SpringBootMain.class, args);
   }
}
HelloController.java
package cn.et.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.Timestamp;
@Controller
public class HelloController {
    @RequestMapping("/ftl")
    public String welcome(ModelMap model) {
    	 model.put("time",new Timestamp(System.currentTimeMillis()));
         model.put("message","http://blog.csdn.net/phone13144830339");
    	 //跳转至index.ftl
         return "index";
    }
	
    @RequestMapping("/jsp")
    public String Jsp(ModelMap model) {
        //跳转至index.jsp
        return "/index.jsp";
    }
}
index.ftl
<!DOCTYPE html>
<html>
	<head>
		<meta content="text/html;UTF-8" />
	</head>
	<body>
		Spring Boot 中使用Freemarker
		<#--获取时间-->
		日期: ${time?date} 时间: ${time?time}
		<br>
		<#--获取信息-->
		信息: ${message}
	</body>
</html>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
    Spring Boot 中使用JSP
  </body>
</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值