1

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

为什么其他属性为空?

4

1 回答 1

2

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

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

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

于 2013-07-10T15:45:53.777 回答