java 使用 <html:text /> 编写对象的字符串属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3724682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:10:15  来源:igfitidea点击:

Writing a string property of an object with <html:text />

javastrutsstruts-1html-inputstruts-html

提问by Buhake Sindi

I've got an object in my form that contains various string properties.

我的表单中有一个包含各种字符串属性的对象。

When I want to print it in my JSP form I could do it with

当我想在我的 JSP 表单中打印它时,我可以用

<c:out value="${form.company.address}" />

which works perfectly.

这完美地工作。

Now I want to create an HTML input field. But when I write

现在我想创建一个 HTML 输入字段。但是当我写

<html:text property="company.address" />

<html:text property="company.address" />

I get an error saying

我收到一条错误消息

Caused by: javax.servlet.jsp.JspException: No getter method for property company.address of bean org.apache.struts.taglib.html.BEAN

Do you know how I can create an HTML input field with my company's address?

您知道如何使用公司地址创建 HTML 输入字段吗?

My bean's got the necessary corresponding getters and setters.

我的 bean 有必要的相应 getter 和 setter。

回答by Buhake Sindi

The correct way of translating this:

正确的翻译方法:

<c:out value="${UFForm.company.address}" />

to Struts is,

对 Struts 来说,

<html:text name="UFForm" property="company.address">

It means that there's a request with name UFFormwith a bean that contains a method getCompany()(which I'm assuming returns a Companyobject) and that in turns has a getAddress()getter (if you understand what I mean). In a nutshell, the bean from request/session UFForm, the TagLib is accessing getCompany().getAddress();

这意味着有一个名称UFForm为 bean的请求,其中包含一个方法getCompany()(我假设它返回一个Company对象),而该方法又具有一个getAddress()getter(如果您理解我的意思)。简而言之,来自 request/session 的 bean UFForm,TagLib 正在访问getCompany().getAddress()

PSHope that getAddress()doesn't return a nullelse <html:text />will throw an exception.

PS希望getAddress()不返回null否则<html:text />会抛出异常。



EditTo explain what I did above:

编辑解释我在上面做了什么:

public class Company implements Serializable {

    private String address;

    //Setter
    public void setAddress(String address) {
        this.address = address;
    }

    //Getter
    public String getAddress() { return this.address; }
}

public class UFForm implements Serializable {

    private Company company;

    public void setCompany(Company company) {
        this.company = company;
    }

    public void getCompany() {
        if (this.company == null) {
            setCompany(new Company());
        }

        return this.company;
    }
}

What I did above in <html:text />is equivalent to

我在上面所做的<html:text />相当于

UFForm ufForm = ....;
String property = ufForm.getCompany().getAddress();

回答by Dheeraj Joshi

Your bean should have corresponding setter and getter methods.

你的 bean 应该有相应的 setter 和 getter 方法。

Html form

html格式

<html:text property="name" size="10" maxlength="10">

Corresponding bean.

对应豆。

public class AddressForm 
{
  private String name=null;

  public void setName(String name){
    this.name=name;
  }

  public String getName(){
    return this.name;
  }
}

回答by Dheeraj Joshi

When you are getting the value for the text box with:

当您通过以下方式获取文本框的值时:

<html:text property="company.address" />

You are in fact saying to Struts to do:

你实际上是在对 Struts 说:

formObject.getCompany().getAddress();

So you must have a getter for the company (which must not return null or the next operation will fail) and a setter for the address on the company object. The setters/getters must be public. This must already be the case since you can do the following with no error:

所以你必须有一个公司的 getter(它不能返回 null 否则下一个操作将失败)和公司对象上的地址的 setter。setter/getter 必须是公开的。这一定是这种情况,因为您可以毫无错误地执行以下操作:

<c:out value="${UFForm.company.address}" />

Now, the thing that bugs me is this part: ${UFForm.. When you use JSTL you are accessing the form explicitly. With the <html:text>you access a property on the form implicitly. This implicit form is provided by the enclosing <html:form>tag. Do you have the <html:text>inside a <html:form>?

现在,让我烦恼的是这部分:${UFForm.. 当您使用 JSTL 时,您是在显式访问表单。随着<html:text>您隐式访问表单上的属性。这种隐式形式由封闭<html:form>标签提供。你有<html:text>里面<html:form>吗?

The form bean is located/created/exposed based on the form bean specification for the associated ActionMappingso check your mapping also.

表单 bean 是根据关联的表单 bean 规范定位/创建/公开的,ActionMapping因此还要检查您的映射。