`

Struts2的服务端验证(validation)

阅读更多

最近在做Struts2的服务端验证小demo时,遇到一个错误:

 

ognl.MethodFailedException: Method "setAge" failed for object demo.demo_11_6.lee.RegistAction@d2b116 [java.lang.NoSuchMethodException: demo.demo_11_6.lee.RegistAction.setAge([Ljava.lang.String;)]
	at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1265)
	at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1454)
	at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
	at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
	at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:28)
	at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2225)
	at com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.setProperty(CompoundRootAccessor.java:65)
	at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2225)
	at ognl.ASTProperty.setValueBody(ASTProperty.java:127)
	at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
	at ognl.SimpleNode.setValue(SimpleNode.java:301)
	at ognl.Ognl.setValue(Ognl.java:737)
	at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:198)
	at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:161)
	at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:149)
	at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:276)
	at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:187)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
	at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
	at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:835)
	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:640)
	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1286)
	at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NoSuchMethodException: demo.demo_11_6.lee.RegistAction.setAge([Ljava.lang.String;)
	at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1206)
	... 63 more
/-- Encapsulated exception ------------\
java.lang.NoSuchMethodException: demo.demo_11_6.lee.RegistAction.setAge([Ljava.lang.String;)
	at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1206)
	at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1454)
	at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
	at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
	at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:28)
	at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2225)
	at com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.setProperty(CompoundRootAccessor.java:65)
	at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2225)
	at ognl.ASTProperty.setValueBody(ASTProperty.java:127)
	at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
	at ognl.SimpleNode.setValue(SimpleNode.java:301)
	at ognl.Ognl.setValue(Ognl.java:737)
	at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:198)
	at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:161)
	at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:149)
	at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:276)
	at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:187)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
	at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
	at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
	at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:835)
	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:640)
	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1286)
	at java.lang.Thread.run(Thread.java:619)
\--------------------------------------/

 

搜素资料后发现这里的Action中的age 给的是int型,就会报此异常。

解决办法就是讲int型改为Integer, 同时修改get,set方法即可解决~

 

分享到:
评论

相关推荐

    struts2 登陆注册 以及验证

    在XML验证中,会在struts-plugin.xml或struts.xml中配置Validator的引用,然后在单独的validation.xml文件中定义每个Action的验证规则。例如,对于用户名和密码,可以设置必填、长度限制等规则。 使用注解验证则更...

    struts2验证框架

    本文将详细介绍Struts2验证框架的基本原理、配置方法以及如何使用它进行客户端和服务端的验证。 #### 二、Struts2验证框架概述 Struts2框架内置了一套灵活且强大的验证机制,能够支持多种类型的验证规则,包括但不...

    struts2验证笔录

    其中,Struts2的验证机制是其核心功能之一,能够帮助开发者轻松实现客户端和服务端的数据验证,确保数据的有效性和安全性。 ### 验证框架与Validation #### 验证框架概述 在Struts2中,验证主要分为两大类:...

    struts2 简单数据校验

    - 配置Struts2的validation.xml文件,导入Hibernate Validator的约束声明。 4. **自定义校验逻辑**: - 当预定义的注解不能满足需求时,可以创建自定义校验器实现`ConstraintValidator`接口。 - 使用`@...

    基于struts2的留言板

    Struts2提供了一套强大的表单验证机制,可以实现客户端和服务端的验证。在ActionForm或Action类中,我们可以定义校验规则,并在用户提交表单时自动执行验证。 7. **国际化与本地化** Struts2支持多语言环境,我们...

    Struts2的检证框架详解

    要深入了解Struts2的检证框架,可以阅读其源码,特别是`org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor`和`org.apache.struts2.util.FieldHelper`等关键类,它们是验证过程的核心。...

    struts2 校验框架

    Struts2框架提供了一套强大的校验机制,可以帮助开发者轻松实现客户端和服务端的数据验证。本文将通过一个具体的示例——`LoginAction-validation.xml`配置文件,来详细解析Struts2中的校验框架。 #### 二、Struts2...

    Struts2资料

    9. **表单验证(Validation)**:Struts2提供了强大的表单验证机制,可以在客户端和服务端进行数据验证。 10. **插件机制**:Struts2的插件机制允许开发者轻松扩展框架功能,如Spring插件可以实现依赖注入,...

    Struts2 part6:数据校验

    5. **整合JSR 303/JSR 349 Bean Validation**:Struts2也支持JSR 303/349标准,允许在实体类上直接应用Bean Validation的注解,如`@NotNull`, `@Size`, `@Pattern`等,实现跨框架的校验。 6. **动态字段校验**:...

    struts 2.3.20中提取的jars文件,用于导入在项目中.rar

    10. **Validation框架**:内置了强大的表单验证机制,可以在客户端和服务端进行数据校验。 下载这个压缩包后,开发人员可以直接将包含的jar文件导入到自己的项目中,避免了逐一查找和下载每个依赖的繁琐过程。这些...

    通用表单验证formvalidator4.1.3 后端验证

    8. **性能优化**:FormValidator通常在服务端进行验证,可以避免无效的数据库查询和操作,提高系统的响应速度和安全性。 9. **文档和示例**:FormValidator项目通常会提供详尽的文档和示例代码,帮助开发者快速上手...

    基于prototype的validation.js发布2.3.4新版本,让你彻底脱离表单验证的烦恼

     欢迎大家提出改进,以便框架完成的更好如果需要,我会在将来开发出与服务端结合的开发框架,如struts and struts2 并且感谢原作者这么棒的Idea: http://tetlaw.id.au/view/blog/really-easy-field-validation-w

    ⑨strutstest210-1.1_2.3.zip

    7. **Validation(验证)**:Struts2提供了强大的表单验证机制,可以在客户端和服务端进行数据校验。 8. **国际化(i18n)**:Struts2支持多语言,通过资源包提供不同语言的文本。 9. **插件支持**:Struts2拥有...

    struts-1.2.9-lib、spring2.5.jar包

    Spring 2.5的事务管理功能使得开发者可以轻松地管理数据库事务,同时,它还提供了对JSR-303(Bean Validation)的支持,实现了数据验证。 这两个框架在实际项目中经常结合使用。Struts提供了一种有效的MVC架构,...

    Java Struts教程.pdf

    - **Validation Framework**:用于实现客户端和服务端的数据验证。 - **Struts TagLib**:一组自定义标签,简化了JSP页面的编写。 - **BeanUtils**:提供了一些实用工具方法来操作Bean。 - **Collections**:...

    commons-validator-1.3.1.zip

    4. **配置文件**:如`conf`目录下的`validation.xml`,这是Validator的核心配置文件,用于定义验证规则和验证组。开发者可以根据需求自定义或扩展这些配置。 5. **JAR文件**:压缩包中通常会包含编译后的`commons-...

    整合Flash Builder4.0+MyEclipse 8.6+BlazeDS+Sping 3.0+Hibernate3.3+MySQL5.5+Tomcat+JDK1.6.docx

    - **MyEclipse 8.6**:MyEclipse是一款针对Java EE的集成开发环境,它是Eclipse的扩展,支持包括Java、JSP、JavaScript、SQL、Struts、Spring、Hibernate等多种技术。 - **BlazeDS**:BlazeDS是Adobe提供的一个...

    JSP中springmvc配置validator的注意事项

    2. **使用场景**:如果你的项目不使用Spring MVC作为控制器,而是使用如Struts2这样的框架,那么`applicationContext.xml`可以单独配置所有Spring相关的bean。但当Spring MVC和`applicationContext.xml`一起使用时,...

Global site tag (gtag.js) - Google Analytics