简体   繁体   English

哪个java Web框架可供客户端和服务器端验证选择?

[英]Which java web framework to choose for client side and server side validation?

我们正在寻找一个java框架,它在服务器端轻松进行验证,客户端,Spring,Hibernate,Play都是我们正在搜索的框架选择,我们正在使用基于注释的开发,这个框架也将决定我们的javascript选择。在客户端和服务器端的验证操作中哪个是更好的框架(架构)?

The new Spring 3 (which is at RC2 and should soon be finalized) has a number of goodies to help your cause when matched up with Hibernate. 新的Spring 3(在RC2上,很快就会完成)有很多好东西可以帮助你的事业与Hibernate相匹配。 It is common to validate a model after binding user input to it. 在将用户输入绑定到模型之后验证模型是很常见的。 Spring 3 provides support for declarative validation with JSR-303. Spring 3为JSR-303的声明性验证提供支持。 This support is enabled automatically if a JSR-303 provider, such as Hibernate Validator, is present on your classpath. 如果类路径中存在JSR-303提供程序(如Hibernate Validator),则会自动启用此支持。 When enabled, you can trigger validation simply by annotating a Controller method parameter with the @Valid annotation: 启用后,只需使用@Valid注释注释Controller方法参数即可触发验证:

@RequestMapping(value = "/appointments", method = RequestMethod.POST)
public String add(@Valid AppointmentForm form, BindingResult result) {
    ....
}

public class AppointmentForm {

    @NotNull @Future
    private Date date;
}

After binding incoming POST parameters, the AppointmentForm will be validated; 绑定传入的POST参数后,将验证AppointmentForm ; in this case, to verify the date field value is not null and occurs in the future. 在这种情况下,要验证日期字段值不为空并且将来发生。

So this makes validation against your domain model pretty easy and you are free to use any Javascript library in the front end whether Jquery or Extjs etc. I've used Extjs's widgets extensively with Spring with out any lack of flexibility, I expect the same of Jquery and any other for that matter. 因此,这使得对您的域模型的验证非常简单,并且您可以自由地使用前端的任何Javascript库,无论是Jquery还是Extjs等。我已经使用Extjs的小部件广泛地使用Spring而没有任何灵活性,我期望相同的Jquery和其他任何事情。 There is also Spring-js which you can look into and assess its merits for your use case. 还有Spring-js ,您可以查看并评估其用例的优点。

Client-side validation (assuming by "client-side" you mean javascript-based) is a myth. 客户端验证(假设“客户端”是指基于javascript的)是一个神话。 It makes for a nicer UI - no question - but it can't be called "validation" because anything that comes from the client can not be assumed valid; 它创造了一个更好的UI - 毫无疑问 - 但它不能被称为“验证”,因为来自客户端的任何东西都不能被认为是有效的; not until it's validated on the server. 直到它在服务器上验证。

Server-side validation is not a monolithic piece either - there are at least 3 components to it: 服务器端验证也不是单片 - 至少有3个组件:

  1. Data storage constraints (eg not null, max length, uniqueness, referential integrity, etc... specified at the database level). 数据存储约束(例如,在数据库级别指定的非空,最大长度,唯一性,参照完整性等)。
  2. Domain model validation (ensuring your entities are valid) 域模型验证(确保您的实体有效)
  3. Client input validation (UI and, to lesser extent, API- based validation) 客户端输入验证(UI,在较小程度上,基于API的验证)

It's possible to derive #1 from #2 - Hibernate Validatior does an excellent job at that assuming you're using Hibernate as your JPA provider. 可以从#2派生#1 - Hibernate Validatior可以很好地完成这项任务,假设您使用Hibernate作为JPA提供程序。

It's also possible to derive client-side checks from #3. 也可以从#3派生客户端检查。 If you intend to use GWT then using GWT VF recommended by Jeff is a good approach as it's based on the same spec (JSR-303) as Hibernate Validator. 如果您打算使用GWT,那么使用Jeff推荐的GWT VF是一个很好的方法,因为它基于与Hibernate Validator相同的规范(JSR-303)。 If you're going to use something else, it's reasonably straightforward to write code generating necessary scriptlets from either annotations or XML-based validation rules. 如果您打算使用其他东西,编写从注释或基于XML的验证规则生成必要的scriptlet的代码是相当简单的。 I've done it for ExtJS controls in the past. 我过去为ExtJS控件做过。

The biggest problem is bridging #2 and #3 - the same domain entity may be represented by many different views in UI, each with their own validation rules; 最大的问题是桥接#2和#3 - 相同的域实体可能由UI中的许多不同视图表示,每个视图都有自己的验证规则; said validation rules may be conditional upon entity state and change dynamically, etc... AFAIK there's no good way to do it automatically unless your UI is of very simplistic 1-to-1 CRUD type. 所述验证规则可以以实体状态为条件并动态变化等等... AFAIK没有好的方法自动执行它,除非你的UI是非常简单的1对1 CRUD类型。

Adding to ChssPly76, You can use your Hibernate validation (or javax.validation in the last version) annotated entities directly in your UI, applying the same validation rules automatically also if you use RichFaces . 添加到ChssPly76,您可以直接在UI中使用您的Hibernate验证(或上一版本中的javax.validation)带注释的实体,如果您使用RichFaces,也会自动应用相同的验证规则。 It has a component called beanValidator, which reads the aforementioned annotations. 它有一个名为beanValidator的组件,它读取上述注释。

RichFaces (and JSF), unlike GWT, allows for more document-style web-pages, rather than application-style. 与GWT不同,RichFaces(和JSF)允许更多的文档样式的Web页面,而不是应用程序样式。

There's GWT Validation . GWT验证 It's designed to bridge client-side and server-side validation. 它旨在桥接客户端和服务器端验证。

You could also look for a JSF component library with an ajaxical sauce. 您还可以查找带有ajaxical酱的JSF组件库。 They however mostly do server side validation only, but uses ajax for that. 然而,他们大多只做服务器端验证,但使用ajax。 Examples are RichFaces (also included in Seam ), PrimeFaces and IceFaces . 例如RichFaces (也包括在Seam中 ), PrimeFacesIceFaces

I would say Spring framework is the best. 我会说Spring框架是最好的。 We have been using Spring - Hibernate combination for a while now. 我们一直在使用Spring-Hibernate组合一段时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM