Я использую netbeans 6.9, я новичок в стойках 1.3.8, и я не знал, как отобразить изображение продукта на странице jsp. Я добавил изображения в папку с изображениями в веб-папке и сохранил имена изображений в базе данных в виде BLOB-объектов, а когда я извлекаю изображение из базы данных, он просто печатает мне имя изображения.
это страница JSP:
<html:form action="/addToCartAction">
<span style='color:red'><html:errors property="SQL" />
<html:errors property="SQLException" /></span>
<table border="0">
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="6"><bean:write name="userHomeForm" property="itemImage" /></td>
<td><bean:write name="userHomeForm" property="itemName" />
</td>
</tr>
<tr>
<td>item in stock: <bean:write name="userHomeForm" property="itemQyt" />
</td>
</tr>
<tr>
<td>AED<bean:write name="userHomeForm" property="itemPrice" /></td>
</tr>
<tr>
<td><bean:write name="userHomeForm" property="itemDesc" /></td>
</tr>
<tr>
<td><html:text property="number" value="" size="3"/>
<input type="hidden" name="itemID" value="${userHomeForm.itemID}"/>
<input type="hidden" name="itemQyt" value="${userHomeForm.itemQyt}"/>
<html:submit value="add to cart"/></td>
</tr>
<tr>
<td><span style='color:red'><html:errors property="Qyt"/><html:errors property="OverQyt"/></span></td>
</tr>
</tbody>
</table>
</html:form>
это боб:
public class userHomeForm extends org.apache.struts.action.ActionForm {
private ArrayList result;
private String category;
private String brand;
//// item info
private Integer itemID;
private Integer categoryID;
private Integer brandID;
private Integer itemQyt;
private Integer itemPrice;
private String itemImage;
private String itemName;
private String itemDesc;
public Integer getBrandID() {
return brandID;
}
public void setBrandID(Integer brandID) {
this.brandID = brandID;
}
public Integer getCategoryID() {
return categoryID;
}
public void setCategoryID(Integer categoryID) {
this.categoryID = categoryID;
}
public String getItemDesc() {
return itemDesc;
}
public void setItemDesc(String itemDesc) {
this.itemDesc = itemDesc;
}
public Integer getItemID() {
return itemID;
}
public void setItemID(Integer itemID) {
this.itemID = itemID;
}
public String getItemImage() {
return itemImage;
}
public void setItemImage(String itemImage) {
this.itemImage = itemImage;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public Integer getItemPrice() {
return itemPrice;
}
public void setItemPrice(Integer itemPrice) {
this.itemPrice = itemPrice;
}
public Integer getItemQyt() {
return itemQyt;
}
public void setItemQyt(Integer itemQyt) {
this.itemQyt = itemQyt;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public ArrayList getResult() {
return result;
}
public void setResult(ArrayList result) {
this.result = result;
}
/**
*
*/
public userHomeForm() {
super();
// TODO Auto-generated constructor stub
}
это действие:
public class userHomeAction extends org.apache.struts.action.Action {
/* forward name="success" path="" */
private static final String SUCCESS = "success";
private final static String FAILURE = "failure";
// Initialize SQL connection equal null
private Connection con = null;
private ResultSet rst = null;
private ResultSet rs = null;
/**
* This is the action called from the Struts framework.
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
* @throws java.lang.Exception
* @return
*/
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//Initilized Action error
ActionErrors errors = new ActionErrors();
// set the session on in the page
HttpSession session = request.getSession(true);
//Call userHomeForm Instance here
userHomeForm formBean = (userHomeForm) form;
// extract user data
String category = formBean.getCategory();
String brand = formBean.getBrand();
//Set up flag message
boolean searchFlag = false;
// perform validation
if (category == null || // category parameter does not exist
category.equals("") || // category parameter is empty
category.length()< 1){ // category parameter is empty
errors.add("category", new ActionMessage("errors.category.required"));
}
if (brand != null && // brand parameter does not exist
brand.equals("") && // brand parameter is empty)
brand.length()< 1) { // brand parameter is empty)
errors.add("brand", new ActionMessage("errors.brand.required"));
}
//Save error messages keys into the appropriate HTTP request attribute for use by the <html:errors> tag
saveErrors(request, errors);
//Check- is errors empty
if (errors.isEmpty()) {
searchFlag = true;
// start Viewing userHome
System.out.println("////////////////////////// start Viewing userHome /////////////////////////");
} else {
searchFlag = false;
}
// Now Start communicate with database
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gsm", "root", "root");
//ArrayList<String[]> result = new ArrayList<String[]>();
PreparedStatement ps = (PreparedStatement) con.prepareStatement("select * from item where categoryID=? and brandID=?");
ps.setString(1, category);
ps.setString(2, brand);
rs = ps.executeQuery();
Blob img= null;
byte[]imgData= null;
int columnCount = rs.getMetaData().getColumnCount();
int num = 0;
if (rs != null) // Succes
{
while (rs.next()) {
num++;
searchFlag = true;
int categoryID = rs.getInt("categoryID");
int brandID = rs.getInt("brandID");
int itemID = rs.getInt("itemID");
int itemQyt = rs.getInt("itemQyt");
int itemPrice = rs.getInt("itemPrice");
String itemName = rs.getString("itemName");
String itemDesc = rs.getString("itemDesc");
String itemImage = rs.getString("itemImage");
formBean.setBrandID(brandID);
formBean.setCategoryID(categoryID);
formBean.setItemDesc(itemDesc);
formBean.setItemID(itemID);
formBean.setItemImage(itemImage);
formBean.setItemName(itemName);
formBean.setItemPrice(itemPrice);
formBean.setItemQyt(itemQyt);
//// check out
System.out.println("The item is from the category which its ID is : " + categoryID + " and brand with id :" + brandID + " and the item id is : " + itemID + " and with name : " + itemName + " and its price is : " + itemPrice + " with that much quantity : " + itemQyt + " and its description is : " + itemDesc);
}
}
} catch (Exception ex) {
errors.add("SQLException", new ActionMessage("errors.SQLException"));
throw new SQLException(ex.fillInStackTrace());
}
//Close SQL server connection
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
throw new SQLException(e.getSQLState() + e.fillInStackTrace());
}
//Save error messages keys into the appropriate HTTP request attribute for use by the <html:errors> tag
saveErrors(request, errors);
if (errors.isEmpty()) {
searchFlag = true;
} else {
searchFlag = false;
}
if (searchFlag) {
return mapping.findForward(SUCCESS);
} else {
return mapping.findForward(FAILURE);
}
}
}