All Projects → devefx → Validator Web

devefx / Validator Web

Licence: apache-2.0
Validator-Web基于Servlet的一款验证框架,其核心设计目的是开发迅速、代码量少、学习简单、功能强大、易扩展

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Validator Web

Flips
Repository for feature flip library which provides various annotations to flip any feature. Works with Java8, Spring, Spring Boot
Stars: ✭ 48 (-32.39%)
Mutual labels:  spring-mvc
Csv File Validator
🔧🔦 Validation of CSV file against user defined schema (returns back object with data and invalid messages)
Stars: ✭ 60 (-15.49%)
Mutual labels:  validator
Vee Validate
✅ Form Validation for Vue.js
Stars: ✭ 8,820 (+12322.54%)
Mutual labels:  validator
Psychological Counseling System
简易心理咨询预约系统Based On SSM
Stars: ✭ 49 (-30.99%)
Mutual labels:  spring-mvc
Node Har Validator
Extremely fast HTTP Archive (HAR) validator using JSON Schema
Stars: ✭ 52 (-26.76%)
Mutual labels:  validator
X Springboot
X-SpringBoot是一个轻量级的Java快速开发平台,能快速开发项目并交付【接私活利器】
Stars: ✭ 1,117 (+1473.24%)
Mutual labels:  spring-mvc
Ssm Demo
⚡️利用 maven 来构建一个多模块基于 SpringMVC + Spring + MyBatis 三大框架的脚手架,为 Java 初学者了解多模块的 maven 工程做个参考
Stars: ✭ 45 (-36.62%)
Mutual labels:  spring-mvc
Springboot Learn By Example
SpringBoot Learn By Example Book
Stars: ✭ 68 (-4.23%)
Mutual labels:  spring-mvc
Springboard
Spring Boot based production grade starter kit.
Stars: ✭ 59 (-16.9%)
Mutual labels:  spring-mvc
Eshop Soa
EShop基于Dubbo实现SOA服务化拆分,并基于RocketMQ解决了分布式事务(新版SpringBootSOASkeleton)
Stars: ✭ 65 (-8.45%)
Mutual labels:  spring-mvc
Tianti
java轻量级的CMS解决方案-天梯。天梯是一个用java相关技术搭建的后台CMS解决方案,用户可以结合自身业务进行相应扩展,同时提供了针对dao、service等的代码生成工具。技术选型:Spring Data JPA、Hibernate、Shiro、 Spring MVC、Layer、Mysql等。
Stars: ✭ 1,053 (+1383.1%)
Mutual labels:  spring-mvc
Translation Spring Mvc 4 Documentation
Spring MVC 4.2.4 RELEASE 中文文档完整翻译稿
Stars: ✭ 1,062 (+1395.77%)
Mutual labels:  spring-mvc
Inspector
A tiny class validation library.
Stars: ✭ 64 (-9.86%)
Mutual labels:  validator
Market
Simple web-market: Spring, JSP, REST, Hibernate (under modernization)
Stars: ✭ 47 (-33.8%)
Mutual labels:  spring-mvc
Schemasafe
A reasonably safe JSON Schema validator with draft-04/06/07/2019-09 support.
Stars: ✭ 67 (-5.63%)
Mutual labels:  validator
Brazilian Utils
Utils library for specific Brazilian businesses
Stars: ✭ 1,023 (+1340.85%)
Mutual labels:  validator
Is
Micro check library in Golang.
Stars: ✭ 61 (-14.08%)
Mutual labels:  validator
Unicorn
Unicorn - W3C's Unified Validator
Stars: ✭ 70 (-1.41%)
Mutual labels:  validator
Kotlin Spring Boot Jpa Rest Api Demo
Build a Restful API with Kotlin, Spring Boot, Mysql, Jpa and Hibernate
Stars: ✭ 67 (-5.63%)
Mutual labels:  spring-mvc
Stylelint Validator
Stylelint plugin to validate CSS syntax
Stars: ✭ 64 (-9.86%)
Mutual labels:  validator

Build Status

validator-web

特性

  • 前后端验证框架,设计精巧、使用简单
  • 支持SpringMVC、Struts2、Servlet
  • Java验证代码自动生成JavaScript前端验证代码
  • 统一前后端验证规范
  • 支持自定义验证规则
  • 支持对自定义数据验证,默认支持:form、json、xml
  • 支持分组验证
  • 支持BeanValidation扩展(提供HibernateValidation实现)
  • 支持国际化消息模板,模板支持EL表达式

安装

git clone https://github.com/devefx/validator-web.git
cd validator-web
mvn clean install -Dmaven.test.skip

如果你使用 Maven,那么在 pom.xml 中加入下面的代码即可:

<dependency>
    <groupId>org.devefx</groupId>
    <artifactId>validator-web</artifactId>
    <version>1.0.1-release</version>
</dependency>

配置验证器

以下配置均使用默认配置,为了确保代码在使用模型前已经被执行,请将代码放在ServletContextListener.contextInitialized中保证容器启动完成时被调用(推荐使用Spring进行配置)

ValidatorConfig validatorConfig = new ValidatorConfig();
		
ValidatorFactoryImpl validatorFactory = new ValidatorFactoryImpl();
validatorFactory.setValidatorConfig(validatorConfig);

Validator validator = validatorFactory.buildValidator();

ValidatorUtils.setValidator(validator);

创建验证模型

import org.devefx.validator.Validation;
import org.devefx.validator.ValidationContext;
import org.devefx.validator.constraints.Email;
import org.devefx.validator.constraints.Length;
import org.devefx.validator.constraints.NotEmpty;
import org.devefx.validator.script.annotation.ScriptMapping;

@ScriptMapping("login")
public class LoginValidation implements Validation {
    @Override
    public void initialize(ValidationContext context) {
        context.constraint("email", new NotEmpty());
        context.constraint("email", new Email());
        context.constraint("password", new NotEmpty());
        context.constraint("password", new Length(4, 20));
    }
}

Java中使用验证模型

SpringMVC 示例(需要配置SpringValidatorInterceptor拦截器)

@Controller
public class LoginController {
    @Valid(value=LoginValidation.class)
    @RequestMapping("/login")
    public void login() {
        // ...
    }
}

Struts 示例(需要配置Struts2ValidatorInterceptor拦截器)

public class LoginAction extends ActionSupport {
    @Valid(value=LoginValidation.class)
    public void login() {
       // ...
    }
}

Servlet示例(Servlet需要继承AbstractValidatorHttpServlet)

@Valid(value=LoginValidation.class)
@WebServlet(name="loginServlet", urlPatterns="/login")
public class LoginServlet extends AbstractValidatorHttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // ...
    }
}

HTML中使用验证模型

如果要在HTML中使用,需要在web.xml中进行配置。 scan-package:扫描包里面的 Validation 类,映射出JavaScript验证模型(使用@ScriptMapping注解的Validation才会进行映射)

(springmvc可以简写配置,请参考SpringMVC Configuration

<servlet>
    <servlet-name>scriptSupport</servlet-name>
    <servlet-class>org.devefx.validator.web.servlet.ScriptSupportServlet</servlet-class>
    <init-param>
        <param-name>scan-package</param-name>
        <param-value>org.my.validation</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>scriptSupport</servlet-name>
    <url-pattern>/va/*</url-pattern>
</servlet-mapping>

在HTML代码中加入以下代码,form在提交时若不满足验证模型的约束将会阻止提交并自动提示错误

validate的使用方法,请参考JavaScript Documentation

<!-- 依赖库 -->
<script type="text/javascript" src="/va/lib/jquery.js"></script>
<script type="text/javascript" src="/va/lib/jquery.form.js"></script>
<!-- 核心库 -->
<script type="text/javascript" src="/va/validator.js"></script>
<!-- 验证器 -->
<script type="text/javascript" src="/va/validation-js/login.js?locale=cn"></script>
<script type="text/javascript">
    $(function() {
        $("form").validate({
            language: 'cn'
        });
    });
</script>

演示效果

(以下为 SpringMVC Example 的运行效果)

example

配置说明

文档

示例

https://github.com/devefx/validator-web/tree/master/example

协议

https://www.apache.org/licenses/LICENSE-2.0.txt

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].