-4

我想使用 java 类更改这些文本区域的属性。如您所见,textareas 默认情况下是禁用的。如果用户在数据库中找不到注册的 TIN,我希望启用此功能。这是我的代码:

jsp:

<label style="font-size: 17px;">Registered Name</label><br><br>         
<textarea disabled id="tpName" name="tpName" style="margin-top: -9px; width: 275px; height: 40px;">${name}</textarea>   
<br><br>
<label style="font-size: 17px;">Address</label><br><br>         
<textarea disabled id="tpAddress" name="tpAddress" style="margin-top: -9px; width: 275px; height: 40px;">${address}</textarea>

班级:

private String tpTin;
private String tpName;  
private String tpAddress;
private TblTaxPayment tbltaxpayment;

public String getTpTin() {
    return tpTin;
}
public String getTpName() {
    return tpName;
}
public String getTpAddress() {
    return tpAddress;
}
public void setTpTin(String tpTin) {
    this.tpTin = tpTin;
}
public void setTpName(String tpName) {
    this.tpName = tpName;
}
public void setTpAddress(String tpAddress) {
    this.tpAddress = tpAddress;
}

private HttpServletRequest request;

public void setServletRequest(HttpServletRequest request){
    this.request = request;
}
public TblTaxPayment getTblTaxPayment() {
    return tbltaxpayment;
}
public void setTblTaxPayment(TblTaxPayment tbltaxpayment) {
    this.tbltaxpayment = tbltaxpayment;
}

@SuppressWarnings("unused")
public String execute() throws Exception {

    Debugger.border();
    Debugger.startDebug(this.getClass().toString());

    String tax = request.getParameter("tpTin");

    TblTaxPaymentDAO tdao = DAOFactory.getDaoManager(TblTaxPayment.class);
    TblTaxPayment t = null;
    t = tdao.findbyTIN(tax.replace("-", ""));
    tbltaxpayment = (TblTaxPayment) t;

    try{

            Debugger.print("TIN : "+tax);
            if(tax != null) {

                tpTin = tbltaxpayment.getTpTin();                                       
                tpName = tbltaxpayment.getTpName();
                tpAddress = tbltaxpayment.getTpAddress();

                String tin = tpTin;
                String name = tpName;
                String address = tpAddress;

                request.setAttribute("tin", tin);
                request.setAttribute("name", name);
                request.setAttribute("address", address);

                return SUCCESS;
            }else{
                request.setAttribute("name", true);
                request.setAttribute("address", true);              
                return SUCCESS;
            }


    }catch(Exception e){
        e.printStackTrace();
        Debugger.endDebug(this.getClass().toString());
        Debugger.border();
        return ERROR;
    }

}

}

我做了什么。我将属性设置为 true 以便能够更改 jsp 中 textareas 的属性/属性。

4

1 回答 1

1

如果您的 Java 类包含确定是否启用 textareas 的逻辑,您必须首先让您的 Java 类存储 JSP 可以访问的某处的状态。您有多种选择:您可以将其设置为请求属性,例如

request.setAttribute("nameRequired", true /* or false */); 
request.setAttribute("addressRequired", true /* or false */);

您可以在 Java 类中存储标志并通过 getter 使它们可用:

boolean nameRequired;
boolean addressRequired;

public String execute () throws Exception {
    ...
    nameRequired = true /* or false */;
    addressRequired = true /* or false */;
    ...
}

public boolean isNameRequired () {
    return nameRequired;
}

public boolean isAddressRequired () {
    return addressRequired;
}

对于上述任何一种情况,您还可以通过让类返回业务状态并让 UI 决定是否需要名称或地址来进一步将业务逻辑与 UI 隔离,例如;

request.setAttribute("haveTaxInfo", false /* or true */); 

或者其他什么——语义。

在任何情况下,JSP 确定 textarea 状态所需的信息现在都可由 JSP 访问。然后,在您的 JSP 中,您只需根据需要编写disabled属性:

<%

// option 1: if you used request attributes:
boolean nameRequired = Boolean.TRUE.equals(request.getAttribute("nameRequired")); 
boolean addressRequired = Boolean.TRUE.equals(request.getAttribute("addressRequired"));

// option 2: or, if you used class fields:
boolean nameRequired = businessClass.isNameRequired(); 
boolean addressRequired = businessClass.isAddressRequired();

// option 3: or,if you used the business state in a request attribute: 
boolean haveTaxInfo = Boolean.TRUE.equals(request.getAttribute("haveTaxInfo"));
boolean nameRequired = !haveTaxInfo;    // if we don't have the tax info then the name
boolean addressRequired = !haveTaxInfo; // and address are required.

%>

<textarea <%=(nameRequired?"":"disabled")%> id="tpName" name="tpName" style="margin-top: -9px; width: 275px; height: 40px;">${name}</textarea>   
<textarea <%=(addressRequired?"":"disabled")%> id="tpAddress" name="tpAddress" style="margin-top: -9px; width: 275px; height: 40px;">${address}</textarea>

这里要带回家的关键点是,您可以使用<%= ... %>JSP 标记来控制是否将“禁用”写入输出。您可以基于某个布尔值在这些标记中使用条件表达式,并且该布尔值必须以某种方式从您的业务类传递到 JSP(通过请求属性或 getter 或其他方式)。

希望有帮助。

于 2013-08-12T06:44:56.257 回答