Re: Does StrutsCX supports File/Image Upload???

Nitin Gulati <[email protected]> Wed, 07 Sep 2005 19:46:52 +0530
Newsgroups gmane.comp.java.strutscx.user
Message-ID <[email protected]>

When I try to access the property in the ActionForm corresponding to the 
form field in XSL, I get null.
That means my property in ActionForm is not initialized by the value.
I am enclosing the XSL File and ActionForm and Action class.
Can you please have a look at it and tell me where am I going wrong.

Thanks for the help.
Nitin


Yayo wrote:

>Sure it does! And works same as Struts on that. Use the multipart
>request header as allways.
>
>2005/9/7, Nitin Gulati <[email protected]>:
>  
>
>>Hi All,
>>    I am new to using StrutsCX library.
>>For one of my projects I require to upload an image from my client to
>>server.
>>Does StrutsCX supports the image upload part. I was going through the
>>strutscx samples, but it handles all types of form elements other than
>>type = file.
>>I need it urgently.
>>I will be very greatful if someone can guide me on how to do it.
>>
>>Thanks,
>>Nitin Gulati
>>
>>
>>
>>-------------------------------------------------------
>>SF.Net email is Sponsored by the Better Software Conference & EXPO
>>September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
>>Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
>>Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
>>_______________________________________________
>>Strutscx-user mailing list
>>[email protected]
>>https://lists.sourceforge.net/lists/listinfo/strutscx-user
>>
>>    
>>
>
>
>  
>
studentPic.xsl (text/xml, 4.7 KB)
<?xml version = "1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version="1.0">

<!--

****************************************
Author : Vinay
****************************************

****************************************
Student Contact Info Template
****************************************
-->

<xsl:include href="/WEB-INF/xsl/layout.xsl"/>
<xsl:template name="content">
<!--  <form name="studentpic" action="/{$projectname}/savespicinfo.do" method="post" enctype="multipart/form-data">  -->
<form name="studentpic" action="/{$projectname}/savespicinfo.do" method="post">
<script language="JavaScript" type="text/javascript" src="/{$projectname}/include/student.js"></script>
<TD width="584" valign="top">
      <table width="100%" border="0" cellspacing="1" cellpadding="1">
		<xsl:if test="//jsstudent/error">
	    <tr>
	    	<td  colspan="4" align="left">
				<table width="62%" height="10" border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td>
						<font color="#ff0000" size="2" face="arial">
						Error : <xsl:value-of select="//jsstudent/error"/>
						</font>
						</td>
					</tr>
					<tr>
						<td ><img src="{$img.table-spacer}" height="4"/></td>
					</tr>
	  			</table>
			</td>
		</tr>
	    </xsl:if>
        
        <tr>
          <td><a href="/{$projectname}/studentinfo.do">
          	<xsl:value-of select="/root/variables/menu/profilemenu/studentinfo"/></a>
          		<xsl:value-of select="$spacer"/><strong>|</strong>
          		<xsl:value-of select="$spacer"/>
          	<a href="/{$projectname}/scontactinfo.do">
          	<xsl:value-of select="/root/variables/menu/profilemenu/contactinfo"/></a>
          		<xsl:value-of select="$spacer"/><strong>|</strong>
          		<xsl:value-of select="$spacer"/><strong>
          	<xsl:value-of select="/root/variables/page/studenthome/picture"/></strong>
          		<xsl:value-of select="$spacer"/><strong>|</strong>
          		<xsl:value-of select="$spacer"/>
          	<a href="/{$projectname}/spreferenceinfo.do">
          	<xsl:value-of select="/root/variables/menu/profilemenu/preferences"/></a>
          </td>
        </tr>
      </table>
      <hr>
      <div align="center">
        <table width="95%" border="0" cellspacing="1" cellpadding="1">
          <tr>
            <td width="36%" align="center" valign="top">
            	<img src="{//jsstudent/student/imgsrc}" width="200" height="251"/><br/>
              <font size="-1">
              	<a href="#">
              		<xsl:value-of select="/root/variables/page/studentpicture/deleteimage"/>
              	</a>
              </font>            
             </td>
            <td width="1%" height="18">
            	<font size="-1">
            		<img src="{$img.table-spacer}" width="10" height="17"/>
            	</font>
            </td>
            <td width="63%" valign="top"><table width="100%" border="0" cellspacing="1" cellpadding="1">
          <tr>
          	<td bgcolor="e5ecf9"><font size="-1"><xsl:value-of select="/root/variables/page/studentpicture/uploadimage"/></font></td>
          </tr>
          <tr>
                <td><font size="-1"><xsl:value-of select="/root/variables/page/studentpicture/supportedimage"/></font></td>
          </tr>
          <tr>
                <td><font size="-1">
                  <input type="checkbox" name="haveImgRights" value="checkbox"/>
      <xsl:value-of select="/root/variables/page/studentpicture/legalright"/>.<br/>
      <input type="checkbox" name="isImgPorn" value="checkbox"/>
      <input type="hidden" name="stdImgPorn" value=""/>
      <input type="hidden" name="stdImgRights" value=""/>  
      <input type="hidden" name="action" value="savestudentpic"/>
      <xsl:value-of select="/root/variables/page/studentpicture/legalimage"/>.</font><font size="-1"></font></td>
		  </tr>
  		  <tr>
                <td><font size="-1">
                  <input name="stdImage" type="file" size="40"/>
                </font></td>
      	  </tr>
              <tr>
                <td>
                	<input type="submit" name="Submit" value="{/root/variables/button/upload}" onClick="setHiddenVar(studentpic);"/>
                </td>
              </tr>
              <tr>
                <td height="21"></td>
              </tr>
              <tr>
                <td height="21"><p><font color="555555" size="-2"><xsl:value-of select="/root/variables/page/studentpicture/imagesize"/>.</font></p>
                  <p><font color="555555" size="-2"> If
                    <xsl:value-of select="/root/variables/page/studentpicture/smallimage"/>.</font></p></td>
              </tr>
            </table></td>
		  </tr>
        </table>
      </div>
      </hr>
      </TD>
</form>
</xsl:template>
</xsl:stylesheet>
PictureForm.java (text/plain, 1.5 KB)
package com.ists.jsserver.web.picture.actionform;

import org.apache.struts.upload.FormFile;

import com.cappuccinonet.strutscx.validator.StrutsCXValidatorForm;

public class PictureForm extends StrutsCXValidatorForm
{
    /**
     * 
     */
    private static final long serialVersionUID = 6481243203128258224L;
    private FormFile  mStdImg;
    private String  mAction        		= null;
    private boolean mStdImgPorn;
    private boolean mStdImgRights;
	/**
	 * @return Returns the action.
	 */
	public String getAction() {
		return this.mAction;
	}
	/**
	 * @return Returns the stdImg.
	 */
	public FormFile getStdImg() {
		return this.mStdImg;
	}
	/**
	 * @return Returns the stdImgPorn.
	 */
	public boolean getStdImgPorn() {
		return this.mStdImgPorn;
	}
	/**
	 * @return Returns the stdImgRights.
	 */
	public boolean getStdImgRights() {
		return this.mStdImgRights;
	}
	/**
	 * @param pAction The action to set.
	 */
	public void setAction(String pAction) {
		this.mAction = pAction;
	}
	/**
	 * @param pStdImg The stdImg to set.
	 */
	public void setStdImg(FormFile pStdImg) {
        System.out.println("in picture form image file="+pStdImg);
		this.mStdImg = pStdImg;
	}
	/**
	 * @param pStdImgPorn The stdImgPorn to set.
	 */
	public void setStdImgPorn(boolean pStdImgPorn) {
		this.mStdImgPorn = pStdImgPorn;
	}
	/**
	 * @param pStdImgRights The stdImgRights to set.
	 */
	public void setStdImgRights(boolean pStdImgRights) {
		this.mStdImgRights = pStdImgRights;
	}

    

}
PictureXSLAction.java (text/plain, 5.7 KB)
/**
 * 
 */
package com.ists.jsserver.web.picture.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jdom.Document;
import org.jdom.Element;

import com.cappuccinonet.strutscx.util.StrutsCXConstants;
import com.ists.jsserver.ISTSException;
import com.ists.jsserver.api.IStudent;
import com.ists.jsserver.api.StudentImpl;
import com.ists.jsserver.dto.StudentImage;
import com.ists.jsserver.dto.User;
import com.ists.jsserver.util.ClientConstant;
import com.ists.jsserver.util.ClientHelper;
import com.ists.jsserver.util.ClientXMLConstant;
import com.ists.jsserver.util.ServerConstant;
import com.ists.jsserver.util.StatusContants;
import com.ists.jsserver.web.controller.JSBaseAction;
import com.ists.jsserver.web.picture.actionform.PictureForm;

/**
 * @author Administrator
 * 
 */
public class PictureXSLAction extends JSBaseAction
{
    public ActionForward execute(ActionMapping pMapping, ActionForm pForm, HttpServletRequest pRequest, HttpServletResponse pResponse) throws Exception
    {
        executeAtStart(pMapping, pForm, pRequest, pResponse);
        String lAction = pRequest.getParameter("action");
        PictureForm lPictureForm = null;
        // check the Session
        HttpSession lSession = getValidSession(pRequest);
        Document lDocument = new Document();
        String lXslFile = null;
        ClientHelper lClientHelper = new ClientHelper();
        try
        {
            
            if(lSession == null)
            {
                lXslFile = "Login";
                lClientHelper.setError(ServerConstant.SESSION_LOST_ERROR);
                lDocument = lClientHelper.getLoginDocument();
                lDocument = lClientHelper.getDocumentWithError(lDocument);
                return (pMapping.findForward(ClientConstant.FORWARD_SUCCESS));
            }
            if (lAction != null)
            {
                if (lAction.equalsIgnoreCase(ClientConstant.FORWARD_SAVE_STUDENT_PIC))
                {
                    lXslFile = "studentpic";
                    lPictureForm = (PictureForm) pForm;

                    Element lRootElement = new Element(ClientXMLConstant.ROOT_NODE);
                    lDocument.setRootElement(lRootElement);
                    // * retreiving user object from session and setting
                    // document *
                    User lUser = (User) lSession.getAttribute(ClientConstant.USER_OBJ_KEY);
                    String lUserLoginName = null;
                    if (lUser != null)
                    {
                        lUserLoginName = lUser.getUsrLogin();
                        lDocument = lClientHelper.setDocumentWithUserLoginName(lDocument, lUserLoginName);
                        StudentImage lStudentImage = new StudentImage();
                        if(lPictureForm != null)
                        {
                            lStudentImage.setStdId((lUser.getStudentInfo()!=null)?lUser.getStudentInfo().getStdId():null);
                            lStudentImage.setStdImgPorn((lPictureForm.getStdImgPorn())?StatusContants.StudentIMagePorn:StatusContants.StudentIMageNotPorn);
                            lStudentImage.setStdImgRights((lPictureForm.getStdImgRights())?StatusContants.StudentImageRight:StatusContants.StudentImageNotRight);
                        }
                        IStudent lStudentImpl = new StudentImpl(); 
                        boolean isStudentImageUpdated = lStudentImpl.updateStudentImage(lStudentImage);
                    }
                    else
                    {
                        lXslFile = "studentpic";
                        lClientHelper.setError(ServerConstant.USER_NOT_FOUND_ERROR);
                        lDocument = lClientHelper.getDocumentWithError(lDocument);

                        return (pMapping.findForward(ClientConstant.FORWARD_SUCCESS));
                    }

                    pRequest.setAttribute("PictureForm", lPictureForm);

                }
            }
            else
            {
                Element lRootElement = new Element(ClientXMLConstant.ROOT_NODE);
                lDocument.setRootElement(lRootElement);
                // * retreiving user object from session and setting document *
                User lUser = (User) lSession.getAttribute(ClientConstant.USER_OBJ_KEY);
                String lUserLoginName = null;
                if (lUser != null)
                {
                    lUserLoginName = lUser.getUsrLogin();
                    lDocument = lClientHelper.setDocumentWithUserLoginName(lDocument, lUserLoginName);
                }

                lXslFile = "studentpic";
            }

        }
        catch (ISTSException exception)
        {
            lXslFile = "studentpic";
            lClientHelper.setError(exception.getUserMessageText());
            lDocument = lClientHelper.getDocumentWithError(lDocument);
            
            return (pMapping.findForward(ClientConstant.FORWARD_SUCCESS));
        }
        catch(Exception exception)
        {
            lXslFile = "studentpic";
            lClientHelper.setError(exception.getMessage());
            lDocument = lClientHelper.getDocumentWithError(lDocument);
            
            return (pMapping.findForward(ClientConstant.FORWARD_SUCCESS));
        }
        finally
        {
           pRequest.setAttribute(StrutsCXConstants.XML_KEY, lDocument);

            // update request with the the XSL file
           pRequest.setAttribute(StrutsCXConstants.XSL_KEY, lXslFile);
        }

        return (pMapping.findForward(ClientConstant.FORWARD_SUCCESS));
    }
}