Java Blob图像未显示在jsp页面[struts 1]中
我使用的是netbeans 6.9,我是struts 1.3.8的初学者,不知道如何在jsp页面中显示产品图像。我在web文件夹下的images文件夹中添加了图像,并将图像名称作为blob存储在数据库中,当我从数据库中检索图像时,它只打印图像的名称 这是jsp页面:Java Blob图像未显示在jsp页面[struts 1]中,java,jsp,struts,blob,Java,Jsp,Struts,Blob,我使用的是netbeans 6.9,我是struts 1.3.8的初学者,不知道如何在jsp页面中显示产品图像。我在web文件夹下的images文件夹中添加了图像,并将图像名称作为blob存储在数据库中,当我从数据库中检索图像时,它只打印图像的名称 这是jsp页面: <html:form action="/addToCartAction"> <span style='color:red'><html:errors property="SQL" /&g
<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 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);
}
}
}如果要显示图像,必须使用img标签: 图像的名称在表单中,因此您必须根据此属性动态生成路径:
<img src="${pageContext.request.contextPath}/${userHomeForm.itemImage}"/>
我不知道为什么要使用blob来存储图像名,而不是使用varchar。我刚刚添加了路径
<img src="thePathOfTheImage" />
<img src="${pageContext.request.contextPath}/${userHomeForm.itemImage}"/>