struts2 表单验证

先看下效果:(这个后台验证,不是js验证)
这里写图片描述
实现这个效果需要用到 OGNL 表达式。

Struts2 的验证比较简单:继承 ActionSupport 类:
并实现:validate方法就可以实现验证的拦截:
如果要验证单独的某法方法可以使用 validate + 方法名(首字母大写) 的规则来实现。
上代码:
UserAction

package com.zll.shop.action;

import com.opensymphony.xwork2.ModelDriven;
import com.zll.shop.BaseAction;
import com.zll.shop.entity.Address;
import com.zll.shop.entity.User;
import com.zll.shop.service.UserService;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UserAction extends BaseAction implements ModelDriven<User>{

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    private User user = new User();

    @Override
    public User getModel() {
        return user;
    }

    public String registPage(){
        return "registPage";
    }


    public String regist(){
        String address = ServletActionContext.getRequest().getParameter("address");
        user.getAddresses().add(new Address(null,address,true));
        System.out.println(user.toString());
        System.out.println(userService.regist(user));

        return NONE;
    }

    public String checkUserNameExists() throws IOException {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("UTF-8");
        PrintWriter pw = response.getWriter();

        if(userService.checkUserNameExist(user.getUsername())){
            pw.print("<font color='red'>用户名已存在~!</font>");
        }else{
            pw.print("<font color='green'>用户名可以使用~!</font>");
        }
        return NONE;
    }



    //校验注册方法。
    public void validateRegist() {
        //用户名
        String username = user.getUsername();
        if(username==null||username.trim().length()==0){
            addFieldError("username", "用户名不能为空");
        }
        if(userService.checkUserNameExist(user.getUsername())){
            addFieldError("username", "用户名已存在");
        }

        //密码
        String password = user.getPassword();
        if(password==null||username.trim().length()==0){
            addFieldError("password", "密码不能为空");
        }else if(password.length()<6){
            addFieldError("password", "密码不能小于6位");
        }

        //校验邮箱
        String regEmail = "^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\\.[a-zA-Z0-9_-]{2,3}){1,2})$";
        String email = user.getEmail();
        if(regEmail==null||regEmail.trim().length()==0){
            addFieldError("email", "邮箱不能为空");
        }else
        if(!Pattern.compile(regEmail).matcher(email).matches()){
            addFieldError("email", "邮箱格式不正确");
        }
    }
}

界面:
*用户名:
*密  码:
*确认密码:
*E-mail:
姓名:
性别:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值