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

发布于 2024-12-06 17:05:41 字数 4665 浏览 0 评论 0原文

我已将 Sturts2 应用程序配置为对我的操作使用验证 xml。我也有 fieldexpress 工作。

是否可以从表达式中的操作中调用方法。 例如:

<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>

这是我的实际测试代码,简单的字段表达式可以工作,但函数调用一却不能(请参阅 tbox1)。 我不确定 @class@method 路径是否正常,但不起作用 因为,我已经在函数中添加了登录,但没有任何结果,所以我认为验证器无法访问这些函数。

另外,这是否可能,即是否允许,或者我是否过于雄心勃勃。

谢谢

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 类

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>

I've configured my sturts2 application to use the validation xml for my actions. I also have fieldexpression working.

Would it be possible to call a method from my action in the expression.
eg:

<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>

Here is my actual test code, the simple fieldexpression works, but function call one does not (see the tbox1).
I'm not sure if the @class@method path is ok or not, but is not working
coz, I've added log in the functions but nothing comes up, so i presume the validator can't reach the functions.

Also, Is this possible, ie is it allowed or am i being too ambitious.

Thanks

PS I've corrected the message, I'm not trading my girlfriend ;-)
**** 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";

    }

}

*********and jsp page

<%@ 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东北女汉子 2024-12-13 17:05:41

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

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

In order to calling a static method in expression, which must be OGNL, you should enable struts.ognl.allowStaticMethodAccess by adding below constant to struts.xml file:

< constant name="struts.ognl.allowStaticMethodAccess" value="true"/>

猫烠⑼条掵仅有一顆心 2024-12-13 17:05:41

请参阅我更新的工作验证器文件。对于字段表达式中的字段 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;

    }

}

See my updated working validator file. for field tbox1 in the fieldexpression I'm referring to the method directly as it is my action, which would be on the VS.
tbox1 and user.username are items on the jsp page (and also exist in the action)

I tried to experiment with a static method, but that didn't work, (no time to investigate now).
Hope this helps
and thanks dave for the input.

***updated 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[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>

***updated 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");
        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;

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文