SpingBoot 入门 web (二)

1.配置端口

application.properties 添加,如图

#配置服务器端口
server.port=8070

2. 配置context-path, (配置上下文访问)

# 配置context-path, 一般来说这个配置在正式发布的时候不配置
server.context-path=/spingboot

3. 配置 session最大超时时间(分钟)

# session最大超时时间(分钟),默认为30分钟
server.session-timeout=60

4. 热部署配置 详解

  配置好了,从新启动,保存文件,项目就会重新启动

4.1 在pom.xml 添加 如图

         <!-- 热部署 -->
	 <!-- devtools可以实现页面热部署(即页面修改后会立即生效,
			这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false来实现) -->
		<!-- 实现类文件热部署(类文件修改后不会立即生效),实现对属性文件的热部署。 -->
		<!-- 即devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),
			注意:因为其采用的虚拟机机制,该项重启是很快的 -->
		<!-- (1)base classloader (Base类加载器):加载不改变的Class,例如:第三方提供的jar包。 -->
		<!-- (2)restart classloader(Restart类加载器):加载正在开发的Class。 -->
		<!-- 为什么重启很快,因为重启的时候只是加载了在开发的Class,没有重新加载第三方的jar包。 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<!-- optional=true, 依赖不会传递, 该项目依赖devtools; 
				之后依赖boot项目的项目如果想要使用devtools, 需要重新引入 -->
			<optional>true</optional>
		</dependency>

4.2 在 application.properties 添加

#关闭缓存,及时刷新
#spring.freemarker.cache=false
spring.thymeleaf.cache=true

5. 整合模板引擎

由于 jsp 不被 SpringBoot 推荐使用,所以模板引擎主要介绍 Freemarker 和 Thymeleaf。

在pom.xml 中添加Freemarker

<!--添加 Freemarker 依赖  -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>
在 application.properties 中添加如下内容:
############################################################
#
# freemarker 静态资源配置
#
############################################################
#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
# 关闭缓存, 即时刷新, 上线生产环境需要改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl

代码如下 center.ftl


控制端代码如下

package com.zlp.controller;

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

import com.zlp.entity.Resource;


@Controller
@RequestMapping("ftl")
public class FreemarkerController {

	@Autowired
	private Resource resource;
	
	@RequestMapping("/index")
    public String index(ModelMap map) {
		
       /* Resource resource = new Resource();
        resource.setName("SpingBott");
        resource.setLanguage("中文");
        resource.setWebsite("study spingboot");*/
		map.addAttribute("resource", resource );
        return "freemarker/index";
    }
	
	@RequestMapping("center")
    public String center() {
		System.err.println("222");
        return "freemarker/center/center";
    }

}

结果如下 如图

在pom.xml添加thymeleaf jar

 <!-- 引入 thymeleaf 模板依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

在 application.properties 中添加如下内容:

############################################################
#
# thymeleaf 静态资源配置
#
############################################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
# 关闭缓存, 即时刷新, 上线生产环境需要改为true
spring.thymeleaf.cache=false
控制端代码如下
package com.zlp.controller;

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

@Controller
@RequestMapping("/html")
public class ThymeleafController {
	
	@RequestMapping("/index")
	public String index(ModelMap modelMap){
		modelMap.addAttribute("name", "张三");
		 return "thymeleaf/index";
		
	}

}
index.html 代码如下
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
Thymeleaf模板引擎
<h1 th:text="${name}"></h1>
<h1>hello world~~~~~~~</h1>
</body>
</html>

结果如下 如图


实体类注解如下

package com.zlp.entity;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

public class User {
	
	// 忽略,在页面不显示
	//@JsonIgnore
	private String userName;
	
	private String password;
	
	private String stuNo;
	// 如果是日期字段 设置日期格式
	@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss")
	private Date birthday;
	//字段显示为空,就不显示
	@JsonInclude(Include.NON_NULL)
	private String Desc;

       // 省略get和set 

在html页面展现controller对象取值

html 页面 如下

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>

<!-- 	<script th:src="@{/static/js/test.js}"></script> -->

</head>
<body>
    <!-- thymleaf th 标签使用 -->
	<div>
		用户姓名:<input th:id="${user.userName}" th:name="${user.userName}" th:value="${user.userName}" /> 
	    <br /> 
	           学号:<input th:value="${user.stuNo}" />
		<br /> 
		用户生日:<input th:value="${user.birthday}" />
		<!-- 日期格式要单独格式 -->
		 <br /> 
		 用户生日:<input th:value="${#dates.format(user.birthday, 'yyyy-MM-dd')}" /> <br />
	</div>
    <!-- 采用对象模式,取值对象 -->
    <hr />
	<br />
	<div>
		用户姓名:<input th:id="${user.userName}" th:name="${user.userName}" th:value="${user.userName}" /> 
	    <br /> 
	           学号:<input th:value="${user.stuNo}" />
		<br /> 
		用户生日:<input th:value="${user.birthday}" />
		<!-- 日期格式要单独格式 -->
		 <br /> 
		 用户生日:<input th:value="${#dates.format(user.birthday, 'yyyy-MM-dd')}" /> <br />
	</div>
	<br />


</body>
</html>

controller 代码 如下

package com.zlp.controller;


import java.util.Date;

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

import com.zlp.entity.User;



@Controller
@RequestMapping("th")
public class ThymeleafController {

	@RequestMapping("/index")
    public String index(ModelMap map) {
        map.addAttribute("name", "张三");
        return "thymeleaf/index";
    }
	
	@RequestMapping("center")
    public String center() {
        return "thymeleaf/center/center";
    }
	
	/**
	 * @desc : html 页面th取值使用
	 * @date :2018年6月3日16:44:09
	 * @param map
	 * @return string
	 */
	@RequestMapping("test")
    public String test(ModelMap map) {
		User user = new User();
		user.setStuNo("110");
		user.setUserName("张三");
		user.setPassword("1234");
		user.setBirthday(new Date());
		user.setDesc("xxxxx001");
		map.addAttribute("user", user);
        return "thymeleaf/center/test";
    }
	
	
}



猜你喜欢

转载自blog.csdn.net/zouliping123456/article/details/80382256