week 8.10-8.16
- | Study-update |
---|---|
-Mon | 项目实战 |
-Thu | 项目实战 |
-Wes | freemaker |
-Tue | - |
-Fri | Study-update |
-Sat | - |
-Sun | 文件上传,ajax文件上传 |
8.10 Monday
freemaker ftdl类型
Controller
package com.maaoooo.freemarker.controller;
import com.maaoooo.freemarker.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.jws.soap.SOAPBinding;
import java.util.ArrayList;
import java.util.List;
/**
* @author lzr
* @date 2020 08 12 21:08
* @description
*/
@Controller
public class TestController {
@GetMapping("/test")
public String test(Model model){
List<User> list = new ArrayList<>();
for (int i = 0; i < 6; i++) {
User user = new User();
user.setName("小刘"+i);
user.setPassword("123");
list.add(user);
}
model.addAttribute("users",list);
return "test";
}
}
test.ftdl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$Title$</title>
</head>
<body>
<table border="1">
<tr>
<td>名字</td>
<td>密码</td>
</tr>
<#list users as u>
<tr>
<td>${u.name}</td>
<td>${u.password}</td>
</tr>
</#list>
</table>
</body>
</html>
if
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$Title$</title>
</head>
<body>
<table border="1">
<tr>
<td>名字</td>
<td>密码</td>
<td>性别</td>
</tr>
<#list users as u>
<tr>
<td>${u.name}</td>
<td>${u.password}</td>
<td>
<#if u.gender==0>
男
<#elseif u.gender==1>
女
<#else>
未知
</#if>
</td>
</tr>
</#list>
</table>
</body>
</html>
8.15 Saturday
配置json日期格式化
package com.maaoooo.freemarker.comfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import java.text.SimpleDateFormat;
/**
* @author lzr
* @date 2020 08 15 20:14
* @description
*/
@Configuration
public class WebMvcConfig {
@Bean
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper om = new ObjectMapper();
om.setDateFormat(new SimpleDateFormat("YYYY-MM-dd"));
converter.setObjectMapper(om);
return converter;
}
}
也可直接提供objectMapper
package com.maaoooo.freemarker.comfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import java.text.SimpleDateFormat;
/**
* @author lzr
* @date 2020 08 15 20:14
* @description
*/
@Configuration
public class WebMvcConfig {
// @Bean
// MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
// MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
// ObjectMapper om = new ObjectMapper();
// om.setDateFormat(new SimpleDateFormat("YYYY-MM-dd"));
// converter.setObjectMapper(om);
// return converter;
// }
@Bean
ObjectMapper objectMapper(){
ObjectMapper om = new ObjectMapper();
om.setDateFormat(new SimpleDateFormat("YYYY-MM-dd"));
return om;
}
}
8.16 Sunday
文件上传
package com.maaoooo.fileupload.controller;
import javafx.scene.input.DataFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
* @author lzr
* @date 2020 08 16 12:29
* @description
*/
@RestController
public class HelloController {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd");
@PostMapping("/upload")
public String upload(MultipartFile file, HttpServletRequest request) {
String format = simpleDateFormat.format(new Date());
String realPath = request.getServletContext().getRealPath("/img") + format;
File folder = new File(realPath);
if (!folder.exists()) {
folder.mkdirs();
}
String oldName = file.getOriginalFilename();
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
try {
file.transferTo(new File(folder, newName));
String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/img" + format + "/" + newName;
return url;
} catch (IOException e) {
e.printStackTrace();
}
return "error";
}
}
请求表单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="提交">
</form>
</body>
</html>
ajax表单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="jquery/jquery-3.3.1.js"></script>
</head>
<body>
<div id="result"></div>
<input type="file" id="file">
<input type="button" value="上传" onclick="uploadFile()">
<a href="" id="test">点我</a>
<script>
'use strict';
function uploadFile() {
var file = $("#file")[0].files[0];
var formData = new FormData();
formData.append("file",file);
$.ajax({
type: 'post',
url: '/upload',
processData: false,//是否要处理成对象
contentType: false,
data: formData,
success: function (msg) {
console.log("success");
$("#result").html(msg);
}
})
}
$('#test').onclick(function () {
alert('hi');
})
</script>
</body>
</html>