views:

550

answers:

0

Let's say I have the following model classes

package com.example.model;

public class Product {
    private int productId;
    private String productName;

    //Getters and setters

}


package com.example.model;

public class ImageTypes {
    private int imageTypeId;
    private String imageLocation;
    private String description;

    public ImageTypes(int id, String loc, String desc){
     imageTypeId = id;
     imageLocation = loc;
     description = desc;
    }

    //Getters and setters
}

package com.example.model;

public class ProductImages {
    private int productImageId;
    private int productId;
    private int imageTypeId;
    private String imageName;

    //Getters and setters
}

My action is as follows

public class ProductAction extends ActionSupport implements ModelDriven<Product>{

private Product product = new Product(); 
private List<ImageTypes> imgTypeList;
private List<File> imageFiles;
private List<String> imageFilesFileName;
private ImageTypesDAO imageTypesDAO = new ImageTypesDAO();

@Override
public String execute() throws Exception {
 imgTypeList = imageTypesDAO.getImageTypes();
 return super.execute();
}
    //Getters and setters
}

DAO

public class ImageTypesDAO {

    public List<ImageTypes> getImageTypes(){
     List<ImageTypes> l = new ArrayList<ImageTypes>();

     l.add(new ImageTypes(1, "images/small", "Small"));
     l.add(new ImageTypes(2, "images/large", "Large"));
     l.add(new ImageTypes(3, "images/thumb", "Thumb"));

     return l;

    }
}

add_product.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org   /TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Add Product</title>
</head>
<body>
<s:form action="ProductAction" method="post" enctype="multipart/form-data">
 <s:textfield name="productName" label="Product Name"></s:textfield>
 <s:iterator value="imgTypeList" var="imgType">
  <s:file name="imageFiles" label="%{#imgType.description}"></s:file>
 </s:iterator>
 <s:submit name="Save" align="left" cssStyle="font-size: 15px"></s:submit>
</s:form>

</body>
</html>

The images uploaded by the user has to go to their respective directories in the server (eg. file in 'small' has to go to 'images/small', 'large' to 'images/large'.. ). Now the problem is how can I associate the imageTypeId with each file ? I was able to solve this with some javascript trickery and hidden elements but is there any proper way to let struts2 handle this ?