我有一个index.jsp,我从中调用一个动作类TestAction(单击超链接时),该类具有方法(显示)从数据库中加载组合框的值以及execute方法,以显示在页面test.jsp上.
在test.jsp上,我有一些输入框以及组合框.单击test.jsp上的按钮后,我想验证输入字段,但是问题是当我来自index.jsp时,只有验证在进行,将打开test.jsp并显示错误消息.
我尝试使用< ActionName> -validator.xml进行客户端验证,以及通过覆盖validate方法使用服务器端验证.如何避免在单击index.jsp的超链接时进行验证,以及如何在单击test.jsp的按钮时进行验证.
这是我的代码:
index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Data</title>
</head>
<body>
<s:form>
<a href="<s:url action="displayAction.action"/>" >Display Data</a><br>
</s:form>
</body>
</html>
test.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Data</title>
<script type="text/javascript">
function openPopUp(){
document.getElementById('testForm').setAttribute('target', '_blank');
document.getElementById('testForm').setAttribute('action', 'testAction.action');
}
</script>
<s:head theme="ajax" debug="true"/>
</head>
<body bgcolor="white">
<s:form validate="true" id="testForm">
<div>
<s:actionerror/>
</div>
<table>
//Mapping of data from database
</table>
<s:submit id="submitButton" value="Display Chart" align="center" onclick="openPopUp();"/>
</s:form>
</body>
</html>
struts.xml
<action name="testAction"
class="testAction"
method="execute">
<result name="success" type="chart">
<param name="value">chart</param>
<param name="type">jpeg</param>
<param name="width">600</param>
<param name="height">400</param>
</result>
</action>
<action name="displayAction"
class="testAction"
method="display">
<result name="success">test.jsp</result>
</action>
解决方法:
I tried both client side validation using
-validator.xml
as well as used server side validation by overriding validate method.
它们都是服务器端验证,均由验证拦截器执行,并且均为can be tweaked to be run for an action alias only.
然后使用您的struts.xml:
<action name="testAction"
class="testAction"
method="execute">
...
</action>
<action name="displayAction"
class="testAction"
method="display">
...
</action>
并假设您只想验证第一个操作,则可以执行以下操作:
>使用testAction-testAction-validation.xml(动作名称动作别名),而不仅仅是testAction-validation.xml;
>使用validateTestAction(){}方法,而不仅仅是validate().
同样是take a look at how the whole thing works,因为您尚未定义任何INPUT结果,这很可疑:)