我想使用java类更改这些textareas的属性。如您所见,默认情况下禁用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的属性/属性。答案 0 :(得分: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现在都可以访问JSP确定textarea状态所需的信息。然后,在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标签来控制你是否写'#34;禁用&#34;到输出。您可以基于某些布尔值在这些标记中使用条件表达式,并且该布尔值必须以某种方式从您的业务类传递到JSP(通过请求属性或getter或其他东西)。
希望有所帮助。