您的位置:首页 > 编程语言 > Java开发

Struts2.1.8 验证框架(validation)的使用中遇到的一些问题(直接访问jsp遇到的错误)

2011-02-19 11:55 951 查看
今天要写struts2.1.8中的validation的练习,于是乎凭着以前看过的记忆,边查文档边写……但是随后遇到的问题,令我很恼火,上网一查,原来遇到的人也不少……

问题描述:按照struts2.1.8的文档,我贴一下应有的代码

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />

<constant name="struts.devMode" value="true" />

<package name="default" namespace="/reg" extends="struts-default">

<default-action-ref name="quizAction" />

<action name="quizAction" class="com.linrui.action.QuizAction">

<result name="success">/Success.jsp</result>

<result name="input">/Input.jsp</result>

</action>

<action name="index">

<result>/index.jsp</result>

</action>

</package>

<!-- Add packages here -->

</struts>

QuizAction.java

package com.linrui.action;

import com.opensymphony.xwork2.ActionSupport;

public class QuizAction extends ActionSupport {

private String name;

private int age;

private String answer;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getAnswer() {

return answer;

}

public void setAnswer(String answer) {

this.answer = answer;

}

@Override

public String execute() throws Exception {

return SUCCESS;

}

}

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<% String path = request.getContextPath();

String basePath = request.getScheme() + "://"

+ request.getServerName() + ":" + request.getServerPort() + path + "/";

%>

<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>Validation - Basic</title>

<s:head />

</head>

<body>

<s:form method="post" action="quizAction" validate="true" namespace="/reg">

<s:textfield label="Name" name="name"/>

<s:textfield label="Age" name="age"/>

<s:textfield label="Favorite color" name="answer"/>

<s:submit/>

</s:form>

</body>

</html>

QuizAction-validation.xml (该文件和Action的类文件放在同一目录下,这里采用ActionName-validation.xml的命名方式)

<?xml version="1.0" encoding="UTF-8"?>
<!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="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">100</param>
<message>Only people ages 4 to 8 may take this quiz</message>
</field-validator>
</field>
</validators>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

按照上面的代码(官方文档也是这么写的),导入相应的包,然后发布之后,就可以用了。。。一开始我也是这么想的,但是发布以后开始报错!

报错信息如下:



后台报错数据如下:

严重:

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@1c01ba9

The problematic instruction:

----------

==> list tag.getValidators("${tagName}") as validator [on line 46, column 9 in template/xhtml/form-close-validate.ftl]

in include "/${parameters.templateDir}/xhtml/form-close-validate.ftl" [on line 25, column 1 in template/xhtml/form-close.ftl]

----------

Java backtrace for programmers:

----------

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@1c01ba9

at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:130)

at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)

..................

然后我便检查了我的代码,把能修改的地方都修改了……但是还是没有效果,其中包括把index.jsp里头的<s:form action=“xxxx” 把XXX后头加上.action (其实不加也行),或者是把namespace去掉并加到action里头(那样的话,就不是客户端验证了,而是服务器验证),一样都是没有用……

其实最根本的原因是我访问的时候,采用直接访问jsp的方式(也就是处理流是从 .jsp -> action -> .jsp)来进行,所以才报出那样的错误信息。最后我将index.jsp配置到struts.xml中,采用访问action的方式(action -> action or .jsp ->.jsp),就正常处理了……

总结:struts2在使用验证框架的时候,要让struts2来作为整个web application的URL访问总控制器(也就是所有的地址访问都要先经过拦截器),不然就会出现上述的错误……不过它的这种特性,使得程序的安全机制更高!

Allen
2011-02-19
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: