href=“https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css”
rel=“stylesheet”>
修改员工
method=“post” enctype=“multipart/form-data”>
id
姓名:
当前头像:
选择新头像:
工资:
生日
返回员工列表
配置MvcConfig
通过这里配置:不需要再为每一个访问thymeleaf模板页面单独开发一个controller请求了跳转页面了
package com.blb.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
//通过这里配置:不需要再为每一个访问thymeleaf模板页面单独开发一个controller请求了
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController(“login”).setViewName(“login”);
registry.addViewController(“register”).setViewName(“register”);
registry.addViewController(“addEmp”).setViewName(“addEmp”);
}
}
用户模块
实体类
User实体类
package com.blb.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Accessors(chain = true)
public class User {
private Integer id;
private String username;
private String realname;
private String password;
private Boolean gender;
}
注册
验证码实现
核心步骤:
-
生成随机字符串
-
放入session
-
生成图片并响应
验证码工具类
package com.blb.utils;
import org.apache.log4j.Logger;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.SecureRandom;
import java.util.Arrays;
public class VerifyCodeUtils {
private static final Logger logger = Logger.getLogger(VerifyCodeUtils.class);
private VerifyCodeUtils() {
}
public static final String VERIFY_CODES = “0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”;
private static SecureRandom random = new SecureRandom();
/**
-
使用系统默认字符源生成验证码
-
@param verifySize 验证码长度
-
@return
*/
public static String generateVerifyCode(int verifySize){
return generateVerifyCode(verifySize, VERIFY_CODES);
}
/**
-
使用指定源生成验证码
-
@param verifySize 验证码长度
-
@param sources 验证码字符源
-
@return
*/
public static String generateVerifyCode(int verifySize, String sources){
String _sources = sources;
if(_sources == null || _sources.length() == 0){
_sources = VERIFY_CODES;
}
int codesLen = _sources.length();
SecureRandom rand = new SecureRandom();
StringBuilder verifyCode = new StringBuilder(verifySize);
for(int i = 0; i < verifySize; i++){
verifyCode.append(_sources.charAt(rand.nextInt(codesLen-1)));
}
return verifyCode.toString();
}
/**
-
生成随机验证码文件,并返回验证码值
-
@param w
-
@param h
-
@param outputFile
-
@param verifySize
-
@return
-
@throws IOException
*/
public static String outputVerifyImage(int w, int h, File outputFile, int verifySize) throws IOException {
String verifyCode = generateVerifyCode(verifySize);
outputImage(w, h, outputFile, verifyCode);
return verifyCode;
}
/**
-
输出随机验证码图片流,并返回验证码值
-
@param w
-
@param h
-
@param os
-
@param verifySize
-
@return
-
@throws IOE