【问题标题】:key attribute OGNL issue in Struts2Struts2 中的关键属性 OGNL 问题
【发布时间】:2015-09-11 04:02:17
【问题描述】:

我与Struts2 合作了很长时间。

当我使用 <s:textfield label="Product Id" key="%{productId}"/><s:textfield label="Product Id" key="[0].productId"/> 时,它不起作用。 即,如果我使用第一个带有指向 OGNL 表达式 %{productId} 的关键属性的标签,那么结果 html input tag's 名称属性将为空。即<input type="text" name="" value="" id="Product_Save_"/>。如果我使用<s:textfield label="Product Id" key="[0].productId"/> 标签,那么结果html input tag<input type="text" name="[0].productId" value="" id="Product_Save__0__productId"/>

在这两种情况下,Struts 都无法将我的请求参数填充到我的产品类模型对象中,如下所示:

public class Product{

private String productId;
private String productName;
private int price;
private ProductOwner productOwner;

public Product() {}

public Product(String productId, String productName, int price) {
    this.productId = productId;
    this.productName = productName;
    this.price = price;
}

public void setProductOwner(ProductOwner productOwner) {

    this.productOwner = productOwner;
}

public ProductOwner getProductOwner() {
    return productOwner;
}

public String getProductId() {
    return productId;
}

public void setProductId(String productId) {
    this.productId = productId;
}

public String getProductName() {
    return productName;
}

public void setProductName(String productName) {
    this.productName = productName;
}

public int getPrice() {
    return price;
}

public void setPrice(int price) {
    this.price = price;
}

}

如果我使用<s:textfield label="Product Id" key="productId"/>,那么 Struts 能够将productId 填充到ProductproductId 中。

但是当我使用<s:property value="%{[0].productId}"/> 时,它工作正常。

现在我的问题是为什么我不能使用像%{[0].productId} 这样的完整OGNL 表达式,但是在<s:property /> 标签中为什么我可以使用像%{[0].productId} 这样的完整OGNL 表达式。

【问题讨论】:

    标签: struts2 ognl struts-tags


    【解决方案1】:

    key="%{productId} 不起作用,因为使用 %{} 会强制表达式求值,而 productId 中没有值,因此 name 属性为空。

    key="[0].productId" 是一个正确的表达式,通常可以在 OGNL 中使用,但是由于它具有一定的安全风险,因此不允许使用该表达式在 Struts2 中设置值。当然可以通过这个表达式获取值,所以<s:property value="[0].productId"/> 可以工作。

    【讨论】:

    • 我从你的回答中理解了一些东西。但是,如果您能详细说明,那将对我有很大帮助。
    • @Jagadeesh:具体说明什么?你不明白什么?
    • if key="%{productId} 如果我有 productId 那么会发生什么?如果 key="[0].productId" 那么我将 name 属性设为 "[0].productId" 为什么是这样的吗?
    • @Jagadeesh:productId 的值将放在生成的 html 中。
    • keynamevaluelabel
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 2014-05-06
    相关资源
    最近更新 更多