Giter Site home page Giter Site logo

1401142681 / validator-web Goto Github PK

View Code? Open in Web Editor NEW

This project forked from devefx/validator-web

0.0 1.0 0.0 2.35 MB

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

License: Apache License 2.0

Java 85.14% CSS 2.86% JavaScript 12.00%

validator-web's Introduction

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

validator-web's People

Contributors

devefx avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.