简单自定义MVC框架,并实现校验功能

2020-03-02  本文已影响0人  煗NUAN
public class UserBean {
    private String uid;
    private String name;
    private String age;
    private String sex;
    private String img;

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("UserBean{");
        sb.append("uid='").append(uid).append('\'');
        sb.append(", name='").append(name).append('\'');
        sb.append(", age='").append(age).append('\'');
        sb.append(", sex='").append(sex).append('\'');
        sb.append(", img='").append(img).append('\'');
        sb.append('}');
        return sb.toString();
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }
}
public class UserForm {
    private String uid;
    private String name;
    private String age;
    private String sex;
    private String img;
    
    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }
}
public interface Controller {

    String handleRequest(HttpServletRequest req, HttpServletResponse resp);
}
public class ToLoginController implements Controller {

    @Override
    public String handleRequest(HttpServletRequest req, HttpServletResponse resp) {
        String str="/WEB-INF/page/loginController.jsp";
        return str;
    }
}
public class ToUserInfoController implements Controller {
    @Override
    public String handleRequest(HttpServletRequest req, HttpServletResponse resp) {
        UserBean userBean=new UserBean();

        String uid= req.getParameter("uid");
        String name=req.getParameter("name");
        String age= req.getParameter("age");
        String sex=req.getParameter("sex");
        String img=req.getParameter("img");



        UserForm uf=new UserForm();  //错误的对象
        uf.setName(name);
        uf.setAge(age);
        uf.setSex(sex);
        uf.setImg(img);


        //添加校验功能
        UserValidata uv=new UserValidata();
        List<String> errors=uv.validata(uf);  //把错误对象放到list集合中
        if (errors!=null && !errors.isEmpty()){     //如果有错误信息,跳转到登陆页面
            req.setAttribute("error",errors);  //把错误信息放到req域中
            return "/WEB-INF/page/loginController.jsp";
        }else {
            userBean.setUid(uid);
            userBean.setName(name);
            userBean.setAge(age);
            userBean.setSex(sex);
            userBean.setImg(img);

            req.setAttribute("user",userBean);
        }
        return "/WEB-INF/page/userInfoController.jsp";
    }
}
@WebServlet({"/toLoginController","/toUserInfoController"})
public class DispatcherServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURL=req.getRequestURI();  //获取请求地址
        String action=requestURL.substring(requestURL.lastIndexOf("/")+1);
        //获取注解的名称

        Controller con=null;
        if ("toUserInfoController".equalsIgnoreCase(action)){
            con=new ToUserInfoController();
        }else if ("toLoginController".equalsIgnoreCase(action)){
            con=new ToLoginController();
        }

        String url=con.handleRequest(req,resp);
        //获取需要转发的对象
        req.getRequestDispatcher(url).forward(req,resp);
    }
}
public class UserValidata {
    /**
    * Description: 校验给定的UserForm对象
    * @param: uf : 需要校验的对象
    * @return:  如果校验不成功,则把错误信息存储到集合中
    */
    public List<String> validata(UserForm uf){

        List<String> errors=new ArrayList<String>();
        //定义list集合用来存放错误信息
        String name=uf.getName();
        String age=uf.getAge();
        String sex=uf.getSex();
        String img=uf.getImg();

        if (name==null || name.length()==0){
            errors.add("name值为空");
        }

        if (age==null || age.length()==0){
            errors.add("age值为空");
        }

        if (sex==null || sex.length()==0){
            errors.add("sex值为空");
        }

        if (img==null || img.length()==0){
            errors.add("img值为空");
        }

        return errors;
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>login</title>
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h3>欢迎你,大帅比</h3>
<form action="/toUserInfoController" method="post" class="form-horizontal" role="form">
    <div class="form-group">
        <lable class="col-sm-2 control-label" >编号</lable>
        <div class="col-sm-8">
            <input type="text" name="uid" class="form-control">
        </div>
    </div>

    <div class="form-group">
        <lable class="col-sm-2 control-label" >名称</lable>
        <div class="col-sm-8">
            <input type="text" name="name" class="form-control">
        </div>
    </div>

    <div class="form-group">
        <lable class="col-sm-2 control-label" >年龄</lable>
        <div class="col-sm-8">
            <input type="text" name="age" class="form-control">
        </div>
    </div>

    <div class="form-group">
        <lable class="col-sm-2 control-label" >性别</lable>
        <div class="col-sm-8">
            <input type="text" name="sex"  class="form-control">
        </div>
    </div>

    <div class="form-group">
        <lable class="col-sm-2 control-label" >图片</lable>
        <div class="col-sm-8">
            <input type="text" name="img"  class="form-control">
        </div>
    </div>

    <div class="form-group">
        <lable class="col-sm-2 control-label" ></lable>
        <div class="col-sm-8">
            <input type="submit" value="submit"  class="form-control">
        </div>
    </div>

</form>

<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>

<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户详情</title>
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h3>大帅比,你好</h3>
<div class="form-horizontal" role="form">
    <div class="form-group">
        <lable class="col-sm-2 control-label" >名称</lable>
        <div class="col-sm-8">
            <span class="form-control">${user.name}</span>
        </div>
    </div>

    <div class="form-group">
        <lable class="col-sm-2 control-label" >年龄</lable>
        <div class="col-sm-8">
            <span class="form-control">${user.age}</span>
        </div>
    </div>

    <div class="form-group">
        <lable class="col-sm-2 control-label" >性别</lable>
        <div class="col-sm-8">
            <span class="form-control">${user.sex}</span>
        </div>
    </div>

    <div class="form-group">
        <lable class="col-sm-2 control-label" >图片</lable>
        <div class="col-sm-8">
            <span class="form-control">${user.img}</span>
        </div>
    </div>

</div>

    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读