我已将sturts2应用程序配置为使用验证xml进行操作。我也有fieldexpression工作。
是否可以从表达式中的动作调用方法。 例如:
<field name="myField">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[
@com.test.MyClass@isCaptchaOk(keyString, userResponce)
]]></param>
<message>My credit limit should be MORE than my girlfriend's</message>
</field-validator>
</field>
这是我的实际测试代码,简单的fieldexpression工作,但函数调用没有(参见tbox1)。 我不确定@ class @方法路径是否正常,但是不起作用 因为,我已经在函数中添加了日志,但没有任何内容出现,所以我认为验证器无法达到函数。
此外,这是否可能,即是允许还是我过于雄心勃勃。
由于
PS我已经纠正了这条消息,我没有交易我的女朋友;-) * ** * validation.xml
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="tbox1">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[@uk.co.nhbc.userRegistration.action.Test2Action@getString()]]></param>
<message>function call message here</message>
</field-validator>
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[@uk.co.nhbc.userRegistration.action.Test2Action@isCaptchaOk(tbox1, user.username)]]></param>
<message>function call message here</message>
</field-validator>
</field>
<field name="tbox2">
<field-validator type="stringlength">
<param name="maxLength">5</param>
<message>length messssage here</message>
</field-validator>
</field>
<field name="user.username">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[(!(tbox2 == "aa" && user.username.equals("")))]]></param>
<message>tbox2 eq aa and username is empty messssage2 here</message>
</field-validator>
</field>
</validators>
** * ** * * java class
package uk.co.nhbc.userRegistration.action;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import uk.co.nhbc.common.action.BaseAction;
import uk.co.nhbc.userRegistration.model.Users;
public class Test2Action extends BaseAction {
private String tbox1;
private String tbox2;
private Users user;
private static final Log log = LogFactory.getLog(Test2Action.class);
public String execute() {
return SUCCESS;
}
public String getTbox2() {
return tbox2;
}
public void setTbox2(String tbox2) {
this.tbox2 = tbox2;
}
public String getTbox1() {
return tbox1;
}
public void setTbox1(String tbox1) {
this.tbox1 = tbox1;
}
public Users getUser() {
log.debug("get user called");
return user;
}
public void setUser(Users user) {
log.debug("set user called");
this.user = user;
}
public boolean isCaptchaOk(String challenge, String response) {
//dummy test function
log.debug("captcha function called");
if (response.equals("true"))
return true;
return false;
}
public String getString (){
log.debug("getString function called");
return "hello";
}
}
** * ** * ** *和jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form name="formtest" action="Test2Action">
<s:actionerror/>
<s:fielderror></s:fielderror>
<s:textfield name="tbox1" label="box1"></s:textfield>
<s:textfield name="tbox2" label="box1"></s:textfield>
<s:textfield name="user.username" label="boxuser"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
答案 0 :(得分:1)
为了在表达式中调用静态方法(必须是OGNL),您应该通过在struts.xml文件中添加以下常量来启用struts.ognl.allowStaticMethodAccess:
&LT;常量名=&#34; struts.ognl.allowStaticMethodAccess&#34;值=&#34;真&#34; /&GT;
答案 1 :(得分:0)
请参阅我更新的工作验证程序文件。对于fieldexpression中的字段tbox1,我直接引用该方法,因为它是我的动作,它将在VS上。 tbox1和user.username是jsp页面上的项目(也存在于操作中)
我尝试使用静态方法进行实验,但这不起作用,(现在没时间调查)。
希望这有助于
并感谢戴夫的投入。
** *更新验证xml
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="tbox1">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[isCaptchaOk(tbox1, user.username)]]></param>
<message>function call message here</message>
</field-validator>
</field>
<field name="tbox2">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[(@uk.co.nhbc.userRegistration.action.Test2Action@isFuncOk(tbox2))]]></param>
<message>func okk function call message here</message>
</field-validator>
</field>
<field name="user.username">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[(!(tbox2 == "aa" && user.username.equals("")))]]></param>
<message>tbox2 eq aa and username is empty messssage2 here</message>
</field-validator>
</field>
</validators>
** *更新了java类
package uk.co.nhbc.userRegistration.action;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import uk.co.nhbc.common.action.BaseAction;
import uk.co.nhbc.userRegistration.model.Users;
public class Test2Action extends BaseAction {
private String tbox1;
private String tbox2;
private Users user;
private static final Log log = LogFactory.getLog(Test2Action.class);
public String execute() {
return SUCCESS;
}
public String getTbox2() {
return tbox2;
}
public void setTbox2(String tbox2) {
this.tbox2 = tbox2;
}
public String getTbox1() {
return tbox1;
}
public void setTbox1(String tbox1) {
this.tbox1 = tbox1;
}
public Users getUser() {
log.debug("get user called");
return user;
}
public void setUser(Users user) {
log.debug("set user called");
this.user = user;
}
public boolean isCaptchaOk(String challenge, String response) {
//dummy test function
log.debug("captcha function called");
log.debug("captcha function called"+challenge+response);
if (response.equals("true"))
return true;
return false;
}
public String getString (){
log.debug("getString function called");
return "hello";
}
public static boolean isFuncOk (String response){
log.debug("isFuncOk function called"+response);
if (response.equals("true"))
return true;
return false;
}
}