1. 程式人生 > >springBoot(6):web開發-模板引擎jsp

springBoot(6):web開發-模板引擎jsp

spring boot

一、新建工程

技術分享

技術分享

註意新建的工程下沒有webapp目錄eclipse下會自動創建webapp目錄這裏我們需要自動創建一個webapp目錄並創建WEB-INF。

技術分享


對ServletInitializer.java進行說明

1、這個類相當於我們以前的web.xml

2、只有3.0以上才可以否則需要添加web.xml

二、配置

2.1、pom.xml配置

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>provided</scope>
</dependency>
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
</dependency>

2.2、配置前綴與後綴類似於sspringmvc

spring.profiles.active=dev
#配置前綴與後綴
spring.mvc.view.prefix=/WEB-INF/templates/
spring.mvc.view.suffix=.jsp

四、代碼

package com.example.demo.controller;

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

/**
 * Created by ly on 2017/6/16.
 */
@Controller
@RequestMapping("/demo1")
public class Index {
    @RequestMapping("/index")
    public String index(Model model) throws Exception {
        model.addAttribute("title"  ,"ceshi");
        return "index";
    }
}


idea這裏有點問題下面改用eclipse結構如下

技術分享


index.jsp

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

<!DOCTYPE html>
<html>
<head lang="en">
	<title>Spring Boot Demo - FreeMarker</title>
	
	<link href="/static/css/index.css" rel="stylesheet" />
	
</head>
<body>
	<h1 id="title">${title}</h1>
	
	<c:url value="http://www.roncoo.com" var="url"/>
	<spring:url value="http://www.roncoo.com" htmlEscape="true" var="springUrl" />
	
	Spring URL: ${springUrl}
	<br>
	JSTL URL: ${url}
	
	<!-- <script type="text/javascript" src="/static/webjars/jquery/2.1.4/jquery.min.js"></script>
	<script>
		$(function(){
			$(‘#title‘).click(function(){
				alert(‘1o‘);
			});
		})
	</script> -->
</body>
</html>

訪問http://localhost:8988/demo1/index

技術分享



本文出自 “我愛大金子” 博客,請務必保留此出處http://1754966750.blog.51cto.com/7455444/1939115

springBoot(6):web開發-模板引擎jsp