struts2 validation校验框架

IT_xinxiu 2010-09-24 01:34:52
初学struts2,今天按照书上配置了一个使用validation框架校验的项目,都已经很认真的看着书来配置了,也在网上看了别人的配置方法,也是一样,可是居然出现下面的问题,真是出师不利,头痛死了%>_<%,希望各位大虾能帮帮忙。万分感激!
一共有四个文件(web.xml的就不贴出来了)。
ValidAction.java

package com.struts2.app;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ValidAction extends ActionSupport {
// 下面是Action内用于封装用户请求参数的两个属性
private String name;
private String pass;
// name属性对应的getter方法
public String getName() {
return name;
}
// name属性对应的setter方法
public void setName(String name) {
this.name = name;
}

// pass属性对应的getter方法
public String getPass() {
return pass;
}

// pass属性对应的setter方法
public void setPass(String password) {
this.pass = password;
}
// 处理用户请求的execute方法
public String execute() throws Exception {
// 当用户请求参数的username等于admin,密码请求参数为12345时,返回success字符串
// 否则返回error字符串
if ("admin".equals(getName()) && "12345".equals(getPass())) {
ActionContext.getContext().getSession().put("userinfo", getName());
System.out.println(this.SUCCESS);
return this.SUCCESS;
} else
return this.ERROR;
}
}

ValidAction-validation.xml

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="name">
<field-validator type="requiredstring">
<message>You must enter a name</message>
</field-validator>
</field>
<field name="pass">
<field-validator type="requiredstring">
<param name="min">4</param>
<param name="max">19</param>
<message>password 4 to 19 may take </message>
</field-validator>
</field>
</validators>

struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2app" extends="struts-default">

<action name="ValidAction" class="com.struts2.app.ValidAction">

<result name="success">/welcome.jsp</result>
<result name="error">/error.jsp</result>
<result name="input">/validinput.jsp</result>
</action>

</package>
</struts>

validinput.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head><title>Action Validation</title>

</head>
<body>
<s:form action="ValidAction" validate="true">
<s:textfield label="用户名:" name="name"/>
<s:password label="口令:" name="pass"/>
<s:submit/>
</s:form>
</body>
</html>

打开validinput.jsp就会出现下面错误了,所以应该不是我的配置问题,不知道是不是tomcat要设置一下才行

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@39be68 - Class: freemarker.ext.beans.SimpleMethodModel
File: SimpleMethodModel.java
Method: exec
Line: 130 - freemarker/ext/beans/SimpleMethodModel.java:130:-1
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:527)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)


root cause

Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@39be68 - Class: freemarker.ext.beans.SimpleMethodModel
File: SimpleMethodModel.java
Method: exec
Line: 130 - freemarker/ext/beans/SimpleMethodModel.java:130:-1
org.apache.struts2.components.UIBean.end(UIBean.java:515)
org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
org.apache.jsp.validinput_jsp._jspx_meth_s_005fform_005f0(validinput_jsp.java:133)
org.apache.jsp.validinput_jsp._jspService(validinput_jsp.java:74)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)


root cause

freemarker.template.TemplateModelException: Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@39be68
freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:130)
freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
freemarker.core.IteratorBlock.accept(IteratorBlock.java:94)
freemarker.core.Environment.visit(Environment.java:209)
freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
freemarker.core.Environment.visit(Environment.java:416)
freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
freemarker.core.Environment.visit(Environment.java:209)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:209)
freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
freemarker.core.Environment.visit(Environment.java:209)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:209)
freemarker.core.Environment.include(Environment.java:1482)
freemarker.core.Include.accept(Include.java:169)
freemarker.core.Environment.visit(Environment.java:209)
freemarker.core.MixedContent.accept(MixedContent.java:92)
freemarker.core.Environment.visit(Environment.java:209)
freemarker.core.Environment.process(Environment.java:189)
freemarker.template.Template.process(Template.java:237)
org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:182)
org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:559)
org.apache.struts2.components.UIBean.end(UIBean.java:513)
org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
org.apache.jsp.validinput_jsp._jspx_meth_s_005fform_005f0(validinput_jsp.java:133)
org.apache.jsp.validinput_jsp._jspService(validinput_jsp.java:74)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
...全文
772 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
IT_xinxiu 2010-09-29
谢谢各位帮忙
回复
IT_xinxiu 2010-09-29
问题解决了,在struts.xml中加入
<action name="*">
<result>{1}.jsp</result>
<action>
通过action来访问实现客户端校验的页面,不直接访问就OK了。
回复
redlotus_lyn 2010-09-25
<field-validator type="requiredstring">
<param name="min">4</param>
<param name="max">19</param>
<message>password 4 to 19 may take </message>
</field-validator>

requiredstring校验器,不支持min和max参数。
回复
dd_zhouqian 2010-09-25
这样试试
<s:form action="ValidAction" validate="true">
=》

<s:form action="Valid.action" validate="true">
回复
feifei694893239 2010-09-25
500错误应该是代码的问题.exec方法里为什么要用“admin”.equals(getName)呢,直接写成name就行啊。不知道是不是这里出的问题。
回复
redlotus_lyn 2010-09-25
QQ:75425364
回复
IT_xinxiu 2010-09-24
还是解决不了,跪求高手指教了%>_<%
回复
弑忆 2010-09-24
刚学这个要多注意一下细节,比如那几个XML所放的位置是否正确。报500是服务器端出错了,但不一定是tomcat,我以前写的时候从没遇到过tomcat出错的。还有你的命名最好不要用关键字
回复
jnhcd 2010-09-24
呃呃,用jQuery validate多好啊。。
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-09-24 01:34
社区公告
暂无公告