我们可以使用Struts2 fieldexpression中的参数调用方法吗? [英] Can we call methods with parameters in Struts2 fieldexpression?

查看:142
本文介绍了我们可以使用Struts2 fieldexpression中的参数调用方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经配置了我的sturts2应用程序来使用验证xml作为我的操作。我也有fieldexpression工作。



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

 < field name =myField> 
< field-validator type =fieldexpression>
< param name =expression><![CDATA [
@ com.test.MyClass @ isCaptchaOk(keyString,userResponce)
]]>< / param>
< message>我的信用额度应该超过我女朋友的< / message>
< / field-validator>
< / field>

这是我的实际测试代码,简单的fieldexpression是有效的,但是函数调用一个没有(见tbox1)。
我不知道@ class @方法路径是否正常,但不工作
coz,我添加了登录功能,但没有出现,所以我认为验证器可以



此外,这是否可能,即是否允许或我太雄心勃勃。



谢谢



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>函数调用消息在这里< / message>
< / field-validator>
< field-validator type =fieldexpression>
< param name =expression><![CDATA [@ uk.co.nhbc.userRegistration.action.Test2Action @ isCaptchaOk(tbox1,user.username)]]>< / param>
< message>函数调用消息在这里< / message>
< / field-validator>
< / field>
< field name =tbox2>
< field-validator type =stringlength>
< param name =maxLength> 5< / param>
< message>这里的长度消息< / 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和username是空的messssage2这里< / message>
< / field-validator>

< / field>

< / validators>

** * * * java类

 包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 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);
返回用户;
}

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;
返回false;

}
public String getString(){
log.debug(getString function called);
returnhello;

}

}

** * ** * ** *和jsp页面

 <%@ page language =javacontentType =text / html; charset = ISO-8859-1
pageEncoding =ISO-8859-1% >
<%@ taglib prefix =suri =/ struts-tags%>
<%@ taglib prefix =sjuri =/ struts-jquery-tags%>
<!DOCTYPE html PUBLIC - // W3C // DTD HTML 4.01 Transitional // ENhttp://www.w3.org/TR/html4/loose.dtd\">
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = ISO-8859-1>
< title>在此插入标题< / title>
< / head>
< body>
< s:form name =formtestaction =Test2Action>
< s:actionerror />
< s:fielderror>< / s:fielderror>
< s:textfield name =tbox1label =box1>< / s:textfield>
< s:textfield name =tbox2label =box1>< / s:textfield>
< s:textfield name =user.usernamelabel =boxuser>< / s:textfield>
< s:submit>< / s:submit>
< / s:form>
< / body>
< / html>


解决方案

查看我更新的工作验证器文件。对于fieldexpression中的字段tbox1,我直接指向该方法,因为它是我的操作,它将在VS上。
tbox1和user.username是jsp页面上的项目(也存在于操作中)



我试图尝试一个静态方法,但是没有(没有时间现在调查)。
希望这有助于

,感谢dave输入。


** *更新验证xml

 <! DOCTYPE验证器PUBLIC 
- // OpenSymphony组// 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>函数调用消息在这里< / 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和username是空的messssage2这里< / message>
< / field-validator>

< / field>

< / validators>



** *更新java类



 包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 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);
返回用户;
}

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;
返回false;

}
public String getString(){
log.debug(getString function called);
returnhello;

}

public static boolean isFuncOk(String response){
log.debug(isFuncOk函数调用+响应);
if(response.equals(true))
return true;
返回false;

}

}


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>

解决方案

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;

    }

}

这篇关于我们可以使用Struts2 fieldexpression中的参数调用方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆