操作类struts 2中的值为null

时间:2013-07-10 15:18:09

标签: jsp struts2 action

我有一个如下的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>

在actionCallType,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属性。

为什么其他属性为空?

1 个答案:

答案 0 :(得分:2)

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

其次,由于禁用了这些文本字段,因此不会提交其值。因此,要么在动作方法中启用它,要么从会话中获取数据。

N.B。:禁用元素永远不会被提交。如果您不希望任何人修改这些字段,请将其设为只读,而不是禁用它。