Can we call methods with parameters in Struts2 fieldexpression?

大憨熊 提交于 2020-01-04 02:32:09

问题


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>

回答1:


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




回答2:


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;

    }

}


来源:https://stackoverflow.com/questions/7549063/can-we-call-methods-with-parameters-in-struts2-fieldexpression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!