Struts2无法检索包含在操作类中用作属性的Object

时间:2014-09-12 12:40:10

标签: java jsp struts2

您好我已经创建了一个动作类&#34; MyClass&#34;在Struts2中,我想获取它的实例变量&#34; validationResult&#34;在我的jsp文件中,但我通过另一个实例变量&#34;版本&#34;正在填充。当我使用<s:iterator>标签显示其内容时,它显示实例变量&#34; validationResult&#34;的内容。还

行动类

package my.com;
public class MyClass extends ActionSupport{
    private String version;
    private List<VersionTO> validationResult;

    @Override
    public String execute() throws Exception {
        validationResult=Arrays.asList(new VersionTO ("abc","def","ghi","jkl"), new VersionTO("mno","pqr","stu","vwx"));
        version="212";
        return SUCCESS;
    }    

    public String getVersion() {
        return Version;
    }    
    public void setVersion(String version) {
        this.version = version;
    }           
    public List<VersionTO> getValidationResult() {
        return validationResult;
    }       
    public void setValidationResult(List<VersionTO> validationResult) {
        this.validationResult = validationResult;
    }
}

pack my.be;
public class VersionTO {
    private String server;
    private String version;
    private String versionOn;
    private String compared;

    public VersionTO() {
      super();
    }

    public VersionTO(String server, String version,
            String versionOn, String compared) {
        this.server = server;
        this.version = version;
        this.versionOn = versionOn;
        this.compared = compared;
    }

    public String getServer() {
        return server;
    }       
    public void setServer(String server) {
        this.server = server;
    }       
    public String getVersion() {
        return version;
    }       
    public void setVersion(String version) {
        this.version = version;
    }       
    public String getVersionOn() {
        return versionOn;
    }       
    public void setVersionOn(String versionOn) {
        this.versionOn = versionOn;
    }       
    public String getCompared() {
        return compared;
    }       
    public void setCompared(String compared) {
        this.comparedVersion = compared;
    }    
}

JSP

<jsp:useBean id="ver" class="MyClass" scope="page">
    <jsp:setProperty name="ver" property="*" />
</jsp:useBean>
<jsp:getProperty name="ver" property="vdVersion"/> <!--here i am getting proper output ie 212-->
    <jsp:getProperty name="ver" property="validationResult"/> <!--here i am getting null in the output -->
<%
    if (ver.getValidationResult() != null && ver.getValidationResult().isEmpty()) {
%>
    <!--  logic for Presentation not working as each time I am getting null in validationResult -->
<%
    }
%>

<!-- working fine -->
<s:iterator value="validationResult">
    <tr>
        <td><s:property value="server" /></td>
        <td><s:property value="version" /></td>
        <td><s:property value="versionOn" /></td>
        <td><s:property value="compared" /></td>
    </tr>
</s:iterator>

1 个答案:

答案 0 :(得分:0)

您需要在对象上设置默认的无参数构造函数,否则Struts2将无法实例化它。

public VersionTO() {
   /* ... stuff ... eg set all to =""; */    
}

public VersionTO(String server, String version,
        String versionOn, String compared) {
   /* ... stuff ... */
}

Read the whole story


修改

您不需要所有这些标签和操作......操作属性已经在ValueStack上。只需使用Struts标记而不是JSP标记和邪恶的scriptlet。转动您的JSP:

<jsp:useBean id="ver" class="MyClass" scope="page">
    <jsp:setProperty name="ver" property="*" />
</jsp:useBean>
<jsp:getProperty name="ver" property="vdVersion"/> <!--here i am getting proper output ie 212-->
    <jsp:getProperty name="ver" property="validationResult"/> <!--here i am getting null in the output -->
<%
    if (ver.getValidationResult() != null && ver.getValidationResult().isEmpty()) {
%>
    <!--  logic for Presentation not working as each time I am getting null in validationResult -->
<%
    }
%>

进入:

<s:property name="version"/> <!--WARNING ! In your Action class it is version... "vdVersion" may be a typo-->
<s:if test=%{validationResult!=null">
    <!--  now the logic for Presentation will work, for example : -->
    <s:iterator value="validationResult" status="stat">
        <tr>
            <td><s:property value="server" /></td>
            <td><s:textfield name="validationResult[%{#stat.index}].version" /></td>
            <td><s:property value="versionOn" /></td>
            <td><s:property value="compared" /></td>
        </tr>
    </s:iterator>
</s:if>