user3291853
user3291853

Reputation: 17

Struts2 not able to retrieve contained Object used as an attribute in action class

Hi I have created an action class "MyClass" in Struts2 and i want to fetch its instance variable "validationResult" in my jsp file but i am getting null though another instance variable "version" is getting populated.When I am displaying their contents using <s:iterator> tag it is displaying the contents of instance variable "validationResult" also.

Action Class

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;
    }
}

Bean

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>

Upvotes: 1

Views: 252

Answers (1)

Andrea Ligios
Andrea Ligios

Reputation: 50281

You need to set a default no args constructor on your object, otherwise Struts2 won't be able to instantiate it.

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

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

Read the whole story


EDIT

You don't need all those tags and operations... the action attributes are already on the ValueStack. Just use Struts tags instead of JSP tags and evil scriptlets. Turn your JSP from :

<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 -->
<%
    }
%>

into :

<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>

Upvotes: 0

Related Questions