gpt4 book ai didi

validation - 我们可以在Struts2字段表达式中调用带有参数的方法吗?

转载 作者:行者123 更新时间:2023-12-02 04:06:02 26 4
gpt4 key购买 nike

我已经将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 @ method路径是否正确,但是不起作用
因为我已经添加了登录功能,但是什么也没出现,所以我认为验证器无法访问功能。

另外,这是否可能,即允许还是我过于野心勃勃。

谢谢

附言:我已更正了该消息,我不买卖女友;-)
**** 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类
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>

最佳答案

为了在表达式中调用必须为OGNL的静态方法,应通过在struts.xml文件中添加以下常量来启用struts.ognl.allowStaticMethodAccess:

<常量名=“struts.ognl.allowStaticMethodAccess”值=“true” />

关于validation - 我们可以在Struts2字段表达式中调用带有参数的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7549063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com