【问题标题】:Values are null in action class struts 2动作类struts 2中的值为null
【发布时间】:2013-07-08 14:39:03
【问题描述】:

我有一个如下的jsp页面(ConfigureRate.jsp):

<s:form action="modifyrate.action" method="post">
                    <s:textfield label="Country" name="selectedCountry" value="%{#session.selectedCallRates.country}" size="20" disabled="true" />
                    <s:textfield label="Country Code" name="countrycode" value="%{#session.selectedCallRates.countrycode}" size="20" disabled="true"/>
                    <s:textfield label="Call Direction" name="selectedCallType" value="%{#session.selectedCallRates.callType}" disabled="true"/>
                    <s:textfield label="Device Type" name="selectedDeviceType" value="%{#session.selectedCallRates.deviceType}" disabled="true"/>
                    <s:textfield name="rate" key="label.rate" size="20" />
                    <s:submit method="modifyRate" key="Update" align="center" />
                </s:form>

并且在动作类中为 selectedCallType、selectedDeviceType 等设置了 setter 和 getter .....

操作代码:

public class ConfigureRatesAction extends ActionSupport {

private List<CallRates> callRates;
private String selectedCountryRow;
private String selectedCallType;
private String selectedDeviceType;
private String country;
private int countrycode;
private double rate;
private String selectedCountry;
private static final Logger log = Logger.getLogger(ConfigureRatesAction.class);

public String displayModify() {

    Boolean loggedin = (Boolean) ActionContext.getContext().getSession().get("loggedin");
    CallRatesDAO callRatesDAO = new CallRatesDAO();

    if (loggedin == null || !loggedin) {
        addActionError(getText("error.loggedin"));
        return "loggedout";
    }

    if (selectedCountryRow == null) {
        callRates =
                callRatesDAO.getCallRates();
        addActionError("Select a row to modify");
        return ERROR;
    }

    CallRates selectedCallRates = buildCallRates(selectedCountryRow);

    if (selectedCallRates == null) {
        callRates = callRatesDAO.getCallRates();
        addActionError("Select a row to modify");
        return ERROR;
    } else {
        ActionContext.getContext().getSession().put("selectedCallRates", selectedCallRates);
        return SUCCESS;
    }

}

public String modifyRate() {



    Boolean loggedin = (Boolean) ActionContext.getContext().getSession().get("loggedin");

    if (loggedin == null || !loggedin) {
        addActionError(getText("error.loggedin"));
        return "loggedout";
    }

    CallRatesDAO callRatesDAO = new CallRatesDAO();

    log.info("Values :"+selectedCountry+" "+countrycode+" "+selectedCallType
            +" "+selectedDeviceType+" "+rate);


    if (callRatesDAO.updateRate(selectedCountry, rate, Integer.toString(countrycode),
            selectedCallType, selectedDeviceType)) {
        callRates = callRatesDAO.getCallRates();
        addActionMessage(getText("success.modifyrate"));
        return SUCCESS;
    } else {
        addActionError(getText("errors.modifyrate"));
        return ERROR;
    }
}

private CallRates buildCallRates(String selectedCountryRow) {

    StringTokenizer token = new StringTokenizer(selectedCountryRow, "*");
    CallRates cR = new CallRates();
    int i = 1;

    while (token.hasMoreTokens()) {
        if (i == 1) {
            cR.setCountry(token.nextToken());
        } else if (i == 2) {
            cR.setCountrycode(Integer.parseInt(token.nextToken()));
        } else if (i == 3) {
            cR.setCallType(token.nextToken());
        } else if (i == 4) {
            cR.setDeviceType(token.nextToken());
        }
        i++;
    }

    return cR;
}

public List<CallRates> getCallRates() {
    return callRates;
}

public void setCallRates(List<CallRates> callRates) {
    this.callRates = callRates;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public int getCountrycode() {
    return countrycode;
}

public void setCountrycode(int countrycode) {
    this.countrycode = countrycode;
}

public double getRate() {
    return rate;
}

public void setRate(double rate) {
    this.rate = rate;
}

public String getSelectedCallType() {
    return selectedCallType;
}

public void setSelectedCallType(String selectedCallType) {
    this.selectedCallType = selectedCallType;
}

public String getSelectedCountry() {
    return selectedCountry;
}

public void setSelectedCountry(String selectedCountry) {
    this.selectedCountry = selectedCountry;
}

public String getSelectedCountryRow() {
    return selectedCountryRow;
}

public void setSelectedCountryRow(String selectedCountryRow) {
    this.selectedCountryRow = selectedCountryRow;
}

public String getSelectedDeviceType() {
    return selectedDeviceType;
}

public void setSelectedDeviceType(String selectedDeviceType) {
    this.selectedDeviceType = selectedDeviceType;
}

}

struts.xml:

  <action name="modifyAction"
            class="com.thrupoint.brg.callrates.configurerates.ConfigureRatesAction" method="displayModify">
        <result name="success">jsp/ConfigureRate.jsp</result>
        <result name="error">jsp/modifyrates.jsp</result>
        <result name="loggedout">jsp/Login.jsp</result>
    </action>
    <action name="modifyrate"
            class="com.thrupoint.brg.callrates.configurerates.ConfigureRatesAction" method="modifyRate">
        <result name="success">jsp/modifyrates.jsp</result>
        <result name="input">jsp/ConfigureRate.jsp</result>
        <result name="error">jsp/ConfigureRate.jsp</result>
        <result name="loggedout">jsp/Login.jsp</result>
    </action>

当我在动作类中打印这些属性的值时,我得到 null,除了上面 jsp 表单中没有 value 属性的 rate 属性。

为什么其他属性都是空的?

【问题讨论】:

  • 发布操作代码。
  • 嗨 Roman,用上面的操作代码更新了帖子。
  • 你同意我的观点吗?为什么要引用null 会话属性,为什么不在结果之前从会话中填充bean,或者至少使用会话范围。
  • 会话属性不为空,它们确实显示在页面上,但在提交表单后,属性在 modifyRate 方法中为“空”。
  • struts.xml 需要,请更新。

标签: jsp struts2 action


【解决方案1】:

当您加载该 JSP 时,这些文本字段是否显示任何数据或保持为空。

其次,由于这些文本字段被禁用,因此不会提交其值。所以,要么启用它,要么在 action 方法中从 session 中获取数据。

注意:禁用的元素永远不会被提交。如果您不希望任何人编辑这些字段,请将它们设为 Readonly 而不是禁用它。

【讨论】:

  • 啊,这就是我要找的......禁用的字段不会被提交吗?是的,文本字段显示保存在会话中的数据。我不希望用户编辑这些字段,这就是我禁用它们的原因。
  • 谢谢 Nitesh ...这很有帮助...我会尝试的。
  • 欢迎您,如果您需要进一步说明,请告诉我。
  • 这是一种享受。删除了 disabled 属性并添加了 readOnly 属性。再次感谢!
  • 我投票赞成这个重要的观点Disabled element never get submitted。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多