springmvc的标签库和和Jsr303验证

springmvc的标签库和和Jsr303验证

Spirngmvc的便签库
1.导入相对于的jar,此时我就不多说了,就是springmvc和sping的几个核心包
2.将对象存入到Model转发到页面上
3.引入相对于标签的头标签

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

4.编写相关的标签的库中的标签,即可以完成标签库的查询

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

     <%--
         springmvc的标签库已经其验证的执行的流程
         1.首先引入相关的标题头
         2.form.jsp页面的相关的写法
         3.请求相关的后台的方法的书写

     --%>

     <form:form action="${pageContext.request.contextPath}/user/testJsr303" modelAttribute="user">
         ID:<form:hidden path="uid"/><br>
         姓名:<form:input path="uname"/><br>
         <form:label path="pwd">密码:</form:label>
         <form:password path="pwd"/><br/>
         详情:<form:textarea path="remark"/><br>
             <form:checkbox path="hobbies" value="1"/>打篮球
             <form:checkbox path="hobbies" value="1"/>游泳
             <form:checkbox path="hobbies" value="1"/>健身<br>
         职务:<form:checkboxes path="jobs" items="${wlist}" itemLabel="wname" itemValue="wid"/><br>
         <form:button>保存</form:button>

     </form:form>
</body>
</html>

Jsr303验证
1.先再其结果上进行Valid验证
在这里插入图片描述
2.2.然后在实体的属性上对每一个属性进行验证

package cn.ujiuye.domain;

import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author liugang
 * @date 2019/10/17
 * User对象的实体类
 */
public class User {

    private int uid;
    @NotNull(message = "用户名不能为空")
    private String uname;
    @Length(min = 3,max = 8,message = "密码的长度介于3到6之间")
    private String pwd;
    private String remark;
    private String hobbies;
    @NotNull(message = "职务不能为空")
    private List<Integer> jobs;

    public User() {

    }

    public User(int uid, String uname) {
        this.uid = uid;
        this.uname = uname;
    }

    public int getUid() {
        return uid;
    }

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

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getHobbies() {
        return hobbies;
    }

    public void setHobbies(String hobbies) {
        this.hobbies = hobbies;
    }

    public List<Integer> getJobs() {
        return jobs;
    }

    public void setJobs(List<Integer> jobs) {
        this.jobs = jobs;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", uname='" + uname + '\'' +
                ", pwd='" + pwd + '\'' +
                ", remark='" + remark + '\'' +
                ", hobbies='" + hobbies + '\'' +
                ", jobs=" + jobs +
                '}';
    }
}

3.在处理器的方法中通过BindingResult接收验证的错误信息,同时将通过@ModelAttribute注解将实体和验证后的错误信息返回到原页面上

在这里插入图片描述
4.在处理器方法中进行逻辑的判断,验证没有错误则跳转到其他的页面,如果错误将将错误的信息转发到当前的页面

@RequestMapping("/testJsr303")
    private String testJsr303(Model model, @ModelAttribute("user") @Valid User user, BindingResult br){

        if (br.hasErrors()){
            //做逻辑的判读,如果有错误将跳转到本页面
            List<Work> list = new ArrayList<Work>();
            list.add(new Work(1,"程序员"));
            list.add(new Work(2,"项目经理"));
            list.add(new Work(3,"产品经理"));
            model.addAttribute("wlist",list);
            return "form";
        }else {
            //如果成功,则跳转到成功的页面
            return "success";
        }


    }

5.5.错误信息通过error.jsp标签

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

     <%--
         springmvc的标签库已经其验证的执行的流程
         1.首先引入相关的标题头
         2.form.jsp页面的相关的写法
         3.请求相关的后台的方法的书写

     --%>

     <form:form action="${pageContext.request.contextPath}/user/testJsr303" modelAttribute="user">
         ID:<form:hidden path="uid"/><br>
         姓名:<form:input path="uname"/><form:errors path="uname"/><br>
         <form:label path="pwd">密码:</form:label>
         <form:password path="pwd"/><form:errors path="pwd"/><br/>
         详情:<form:textarea path="remark"/><br>
             <form:checkbox path="hobbies" value="1"/>打篮球
             <form:checkbox path="hobbies" value="1"/>游泳
             <form:checkbox path="hobbies" value="1"/>健身<br>
         职务:<form:checkboxes path="jobs" items="${wlist}" itemLabel="wname" itemValue="wid"/><form:errors path="jobs"/><br>
         <form:button>保存</form:button>

     </form:form>


</body>
</html>

6.对结果的信息的显示
springmvc的标签库和和Jsr303验证_第1张图片

你可能感兴趣的:(框架)