<img src='E.do'> </img>
这样放在网页里图片会页面加载就会显示
我们要做一个效果为需要显示图片的时候才去显示
要求很简单,但是实现起来却没那么容易
需要注意的是:图片为在数据库中读取出来且未保存在硬盘中,src为.do,所以就不能用id.src=''的方式改变src,也不能用id.style.visibility='hidden'; 或者id.style.display='none'; 来隐藏实现,况且这样也不是业务所要求的,要求就是选择一个种类再去显示这个种类的图片
好,下面说实现办法
用到的标签为
<logic:present name="tianjin">
</logic:present>
解释一下这个标签
<logic:present name="tianjin">
如果在你的action中设置了tianjin就执行到这儿。如:request.setAttribute("tianjin","天津")或session.setAttribute("tianjin","天津")
<bean:write name="tianjin"/>
</logic:present> 跟这个标签差不多的有个<logic:equal>
<logic:equal name="tianjinForm" property="tianjinAction" value="tianjin">
如果tianjinForm里的属性tianjinAction的值等于tianjin就执行这儿。
</logic:equal>
下边把代码帖上(部分)
<html:form action="/EnrolRushRedLightManageInfo.do" method="post">
图片路径:<html:select property="enrolRushRedLightPhotoManagePhotoPath" οnclick="enrolRushRedLightPhotoManagePhotoDisplay()">
<html:option value=""></html:option>
<html:option value="齐齐哈尔">齐齐哈尔</html:option>
<html:option value="齐齐哈尔">天津</html:option>
</html:select>
<logic:present name="tianjin">
<img src="EnrolRushRedLightPhotoManagePhotoDisplay.do"></img>
</logic:present>
<script language="javascript" type="text/javascript">
function enrolRushRedLightPhotoManagePhotoDisplay()
{
document.forms[0].action='EnrolRushRedLightPhotoManagePhotoDisplay.do' ;
document.forms[0].submit();
}
</script>
action(半成品,但可通过)
public class EnrolRushRedLightPhotoManagePhotoDisplayAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)
{
EnrolRushRedLightPhotoManagePhotoDisplayForm enrolRushRedLightPhotoManagePhotoDisplayForm = new EnrolRushRedLightPhotoManagePhotoDisplayForm();
String enrolRushRedLightPhotoManagePhotoPath = enrolRushRedLightPhotoManagePhotoDisplayForm.getEnrolRushRedLightPhotoManagePhotoPath() ;
@SuppressWarnings("unused")
String qiqihaerSQL ;
@SuppressWarnings("unused")
String tianjinSQL ;
HttpSession session = request.getSession() ;
if(enrolRushRedLightPhotoManagePhotoPath.equals("齐齐哈尔"))
{
//查询齐齐哈尔的图片
qiqihaerSQL = "查询齐齐哈尔的SQL语句" ;
session.setAttribute("qiqihaer", "qiqihaerSQL") ;
}
if(enrolRushRedLightPhotoManagePhotoPath.equals("天津"))
{
//查询天津的图片
tianjinSQL = "查询天津的SQL语句" ;
session.setAttribute("tianjin", "tianjinSQL") ;
}
@SuppressWarnings("unused")
String SQL = (String)session.getAttribute("tianjin") ;
Connection conn = null;
Statement stmt = null;
BufferedInputStream inputimage=null;
DataBaseConnection dbc = new DataBaseConnection() ;
ResultSet rs = null ;
try
{
try
{
conn = dbc.getConnection();
stmt = conn.createStatement();
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
//在下面括号里换上SQL变为动态SQL语句
rs = stmt.executeQuery("SELECT img FROM dzjc_img");
while (rs.next())
{
Blob blob = (Blob)rs.getBlob("img");
inputimage = new BufferedInputStream(blob.getBinaryStream());
}
conn.setAutoCommit(defaultCommit);
}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("blobRead()'s exception"+e);
try
{
conn.rollback();
throw e;
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
}
catch (Exception e1)
{
e1.printStackTrace();
}
BufferedImage image = null;
try
{
image = ImageIO.read(inputimage);
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(image, "jpg", sos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);
inputimage.close();
sos.flush();
}
catch(Exception e)
{
System.out.println(e);
}
return mapping.findForward("abc");
}
}
form
package struts.form;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class EnrolRushRedLightPhotoManagePhotoDisplayForm extends ActionForm {
private static final long serialVersionUID = 1L;
private FormFile errorRushRedLightPhotoManagePhotoDisplayPhoto;
private String enrolRushRedLightPhotoManagePhotoPath ;
private List errors ;
public void setErrors(List errors)
{
this.errors = errors ;
}
public List getErrors()
{
return this.errors ;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors() ;
// 判断用户是否登陆,没有的话返回到登陆页面
if (request.getSession().getAttribute("sessionUserName")==null||"".equals(request.getSession().getAttribute("sessionUserName")))
{
mapping.findForward("sessionEnded") ;//mapping是转发到页面的,不是提交
}
else
{
}
return errors;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
}
public FormFile getErrorRushRedLightPhotoManagePhotoDisplayPhoto() {
return errorRushRedLightPhotoManagePhotoDisplayPhoto;
}
public void setErrorRushRedLightPhotoManagePhotoDisplayPhoto(
FormFile errorRushRedLightPhotoManagePhotoDisplayPhoto) {
this.errorRushRedLightPhotoManagePhotoDisplayPhoto = errorRushRedLightPhotoManagePhotoDisplayPhoto;
}
public String getEnrolRushRedLightPhotoManagePhotoPath() {
return enrolRushRedLightPhotoManagePhotoPath;
}
public void setEnrolRushRedLightPhotoManagePhotoPath(
String enrolRushRedLightPhotoManagePhotoPath) {
this.enrolRushRedLightPhotoManagePhotoPath = enrolRushRedLightPhotoManagePhotoPath;
}
}