0

i am calling a struts2 action by passing parameter from a dynamic image url

<img src="<s:url action='ImageAction?imageId=logo.jpg' />"/>

With this my action is calling properly but the parameter imageId=logo.jpg is not passing to my action class.

But if i manully pass parameter from the browser url then, parameter is correctly showing into my java page eg. http://localhost:8080/mypoject/jspHomepage/bookstransaction/secure/ImageAction?imageId=logo.jpg

What could be reason for this? Please help me.

struts.xml

          `
    `<package name="Image"   extends="struts-default,json-default"> 
            <result-types>
        <result-type name="imageResult"
            class="v.esoft.actions.changetheme.CustomImageBytesResult" />
        </result-types>
          <action name="updatethemeimageform"  class="v.esoft.actions.changetheme.ThemedetailsEditAction" method="updateThemesImage">  
            <result name="success" type="json"/>
            <result name="input" type="json"/>     
        </action>  
       <action name="Display" class="v.esoft.actions.changetheme.DisplayAction">
            <result name="success" type="json"/>
      </action> 
      <action name="ImageAction" class="v.esoft.actions.changetheme.ImageAction">
        <result name="success" type="imageResult">
        </result>
     </action> 
         </package>`

ImageAction.java

public class ImageAction extends ActionSupport implements ServletRequestAware {

byte[] imageInByte = null;
String imageId;

private HttpServletRequest servletRequest;

public String getImageId() {
    return imageId;
}

public void setImageId(String imageId) {
    this.imageId = imageId;
}

public ImageAction() {
    System.out.println("ImageAction");
}

public String execute() {
    return SUCCESS;
}

public byte[] getCustomImageInBytes() {
        System.out.println("imageId" + imageId);
    }
}
Aleksandr M
  • 23,647
  • 12
  • 63
  • 129
  • How do you check whether `imageId` is set or not? – Aleksandr M Dec 17 '12 at 22:23
  • @AleksandrM What do you mean? –  Dec 17 '12 at 22:25
  • @AleksandrM I am sure the problem is with `?` because of this my parameter is not sending to my action class `"` –  Dec 17 '12 at 22:27
  • How do you know that? Have you tried to print it inside action method? – Aleksandr M Dec 17 '12 at 22:27
  • @AleksandrM Yes i tried to print it `System.out.println("imageId" + imageId);` but its coming null.. and even if i right click on my null img tag from my jsp page i.e (`` ) and open it in new browser tab then its giving me only action name wothout parameter name. i.e (`http://localhost:8080/projectName/ImageAction`) –  Dec 17 '12 at 22:33

1 Answers1

1

Following is untested.

Use param tags to add parameters.

<s:url package="Image" action="ImageAction" var="myUrl">
   <s:parm name="imageId" value="'logo.jpg'"/>
</s:url>

<img src="<s:property value="#myUrl"/>"/>

Note: I suspect in the final line myUrl should be sufficient (without the #) but don't remember at the moment.

Quaternion
  • 10,302
  • 6
  • 45
  • 98