您的当前位置:首页正文

java注册功能实现

2024-11-26 来源:个人技术集锦

1.首先定义user这个实体类以及他的实现方式

public class User {

    private int id;
    private String username;
    private String password;
    private String salt;
    private String email;
    private int type;
    private int status;
    private String activationCode;
    private String headerUrl;
    private Date createTime;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getActivationCode() {
        return activationCode;
    }

    public void setActivationCode(String activationCode) {
        this.activationCode = activationCode;
    }

    public String getHeaderUrl() {
        return headerUrl;
    }

    public void setHeaderUrl(String headerUrl) {
        this.headerUrl = headerUrl;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", salt='" + salt + '\'' +
                ", email='" + email + '\'' +
                ", type=" + type +
                ", status=" + status +
                ", activationCode='" + activationCode + '\'' +
                ", headerUrl='" + headerUrl + '\'' +
                ", createTime=" + createTime +
                '}';
    }

}

2.访问注册页面
点击顶部区域的链接,要打开注册页面,该动态跳转路径

public Map<String, Object> register(User user){
        Map<String,Object>map = new HashMap<>();
        //空值处理
        if(user==null)
        {
            throw new IllegalArgumentException("参数不能为空!");
        }
        if(StringUtils.isBlank(user.getUsername()))
        {
            map.put("usernameMsg","账号不能为空");
            return map;
        }
        if(StringUtils.isBlank(user.getPassword()))
        {
            map.put("passwordMsg","密码不能为空");
            return map;
        }
        if(StringUtils.isBlank(user.getEmail()))
        {
            map.put("emailMsg","邮箱不能为空");
        }
        //判断是否已经存在
        User user1 = userMapper.selectByName(user.getUsername());
        if(user1!=null)
        {
            map.put("usernameMsg","用户账号已经存在!");
            return map;
        }
        User user2 = userMapper.selectByEmail(user.getEmail());
        if(user2!=null)
        {
            map.put("emailMsg","该邮箱已经注册!");
            return map;
        }
        //注册用户
        user.setSalt(CommunityUtil.generateUUID());
        user.setPassword(CommunityUtil.md5(user.getPassword()+user.getSalt()));
        user.setType(0);
        user.setStatus(0);
        user.setActivationCode(CommunityUtil.generateUUID());
        user.setHeaderUrl(String.format("http://image.nowcoder.com/head/%dt.png",new Random().nextInt(1000)));
        user.setCreateTime(new Date());


        Context context=new Context();
        context.setVariable("email",user.getEmail());
        String url=domain+contextPath+"/activation/"+user.getId()+"/"+user.getActivationCode();
        context.setVariable("url",url);
        String content=templateEngine.process("mail/activation.html",context);
        return map;
    }

4.Controller层
调用service层的register函数,如果返回的map为空,说明注册信息是合法的
如果map不为空,识别是什么类型的错误,通过model往前台传数据,通过el表达式 ${}可以获取到。

  @RequestMapping(path = "/register",method = RequestMethod.POST)
    public String register(Model model, User user){
        Map<String,Object>map = userService.register(user);
        if(map==null || map.isEmpty()){
            model.addAttribute("msg","注册成功,我们已经向您的邮箱发送了一封邮件");
            model.addAttribute("target","/index");
            return "/site/operate-result";
        }
        else{
            model.addAttribute("usernameMsg",map.get("usernameMsg"));
            model.addAttribute("passwordMsg",map.get("passwordMsg"));
            model.addAttribute("emailMsg",map.get("emailMsg"));
            return "/site/register";
        }
    }

5.前台代码,主要是register这个页面的代码,这是一些主要内容

	<!-- 内容 -->
	<div class="main">
		<div class="container pl-5 pr-5 pt-3 pb-3 mt-3 mb-3">
			<!-- 注册表单 -->
			<h3 class="text-center text-info border-bottom pb-3">&nbsp;&nbsp;</h3>
			<form class="mt-5" method="post" th:action="@{/register}">
				<!-- 账号 -->
				<div class="form-group row">
					<label for="username" class="col-sm-2 col-form-label text-right">账号:</label>
					<div class="col-sm-10">
						<input type="text"
							   th:class="|form-control ${usernameMsg!=null?'is-invalid':''}|"
							   th:value="${user!=null?user.username:''}"
							   id="username" name="username" placeholder="请输入您的账号!" required>
						<div class="invalid-feedback" th:text="${usernameMsg}" id="username-feedback">
							该账号已注册!
						</div>
					</div>
				</div>
				<!-- 密码 -->
				<div class="form-group row mt-4">
					<label for="password" class="col-sm-2 col-form-label text-right">密码:</label>
					<div class="col-sm-10">
						<input type="password"
							   th:class="|form-control ${passwordMsg!=null?'is-invalid':''}|"
							   th:value="${user!=null?user.password:''}"
							   id="password" name="password" placeholder="请输入您的密码!" required>
						<div class="invalid-feedback" th:text="${passwordMsg}" id="password-feedback">
							密码长度不能小于8位!
						</div>
					</div>
				</div>
				<!-- 确认密码 -->
				<div class="form-group row mt-4">
					<label for="confirm-password" class="col-sm-2 col-form-label text-right">确认密码:</label>
					<div class="col-sm-10">
						<input type="password"
							   class="form-control"
							   th:value="${user!=null?user.password:''}"
							   id="confirm-password" placeholder="请再次输入密码!" required>
						<div class="invalid-feedback" id="confirm-password-feedback">
							两次输入的密码不一致!
						</div>
					</div>
				</div>
				<!-- 邮箱 -->
				<div class="form-group row">
					<label for="email" class="col-sm-2 col-form-label text-right">邮箱:</label>
					<div class="col-sm-10">
						<input type="email"
							   th:class="|form-control ${emailMsg!=null?'is-invalid':''}|"
							   th:value="${user!=null?user.email:''}"
							   id="email" name="email" placeholder="请输入您的邮箱!" required>
						<div class="invalid-feedback" th:text="${emailMsg}" id="email-feedback">
							该邮箱已注册!
						</div>
					</div>
				</div>
				<!-- 立即注册 -->
				<div class="form-group row mt-4">
					<div class="col-sm-2"></div>
					<div class="col-sm-10 text-center">
						<button type="submit" class="btn btn-info text-white form-control">立即注册</button>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>

6.测试结果

显示全文