【问题标题】:Access POJO properties using ModelDriven in Struts 2?在 Struts 2 中使用 ModelDriven 访问 POJO 属性?
【发布时间】:2014-01-07 07:05:24
【问题描述】:

我在 Struts2 中使用ModelDriven,这样我的模型对象内部就有另一个具有属性的对象。我正在执行 AJAX 调用,并希望我的模型对象由用户填充。

JSP:

<s:select list="#session.circleIdNameMap"
  headerKey="-1" headerValue="Select Circle"
  name="id.circleId" id="selectCircleDropDown"
  onchange="findTspNameIdMap(this.value)">
</s:select>

JS:

 $.ajax({   
    type: 'POST',
    url: '/gma/findTspNameIdMap.action',
    data: 
    { 
        id.circleId: circleId,
        minNumberOc: $("[name='minNumberOc']").val(),
        minDurationOc: $("[name='minDurationOc']").val(),
    },

但是,在萤火虫中我得到一个错误

SyntaxError: missing : after property id
id.circleId: circleId,  

但是像minNumberOc 这样的其他直接属性可以正常工作,但id.anything 不行。我正在发布我的模型对象和 Action 类。

GmaThresholdParameter:

public class GmaThresholdParameter implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private GmaThresholdParameterPK id;
   //getter/setters of id

GmaThresholdParameterPK:

public class GmaThresholdParameterPK implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name="CIRCLE_ID")
    private int circleId;

    @Column(name="TSP_ID")
    private int tspId;

    private String flag;
   //getter/setters

动作类:

public class ConfigureTspThresholdAction extends ActionSupport implements SessionAware, ModelDriven<GmaThresholdParameter>{

    private Map<String,String> circleIdNameMap;
// MODEL object
    GmaThresholdParameter gmaThresholdParameters = new GmaThresholdParameter();
   .....
   ...
   public GmaThresholdParameter getGmaThresholdParameters() {
    return gmaThresholdParameters;
}


public void setGmaThresholdParameters(
        GmaThresholdParameter gmaThresholdParameters) {
    this.gmaThresholdParameters = gmaThresholdParameters;
}

@Override
public GmaThresholdParameter getModel() {
    return gmaThresholdParameters;
} 

如何设置id对应的属性?为什么在 Firebug 中会报错?

【问题讨论】:

    标签: java javascript ajax jsp struts2


    【解决方案1】:

    为什么在 Firebug 中会报错?

    因为你在下面的语句后面有一个逗号

    minDurationOc: $("[name='minDurationOc']").val(),
    

    【讨论】:

    • 我检查了我的代码,我没有把','放在最后,但它仍然给出了错误。确定 id.circleId: circleId 有效吗?即使在 AJAX 中,struts 也能选择 getter 吗?
    • 正如错误所说,在 id.cricleId:circleId 处缺少 :(colon) 在 id 之后。所以它期望在 id 之后有一个冒号。那么 id.anthying: xyz 真的有效吗?
    • id.circleId 应该用引号括起来,没有它这样的名字是无效的。
    猜你喜欢
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多