Javascript, IMG, SRC, JSP, J2EE, hibernate, Struts-2.0
Environment: Eclipse, struts2, hibernate3.0
JSP, J2EE, JavaScript [write: drs163@163.com www.newsoftstudio.com]
The struts2 action on the background server can return a list set to the JSP page. one project is a string of the file path. when the actual JSP page is displayed, each line of record is preceded by a small icon determined based on the file type suffix.
Assume that the type icons like GIF, rar.gif, and doc.gif have been saved to the images/myfiletype directory.
The JSP page is mylist. jsp, so we can write two script functions and a piece of script code to implement this function (of course, it can also be achieved through server segment judgment and processing)
First Script Function: returns some types of strings of the specified URL.
Function showfiletypeimg (strurl)
{
// By Drs-2007-6-2 obtain its suffix type based on the specified URL
VaR Pos = strurl. lastindexof (".");
If (Pos <= 0)
{
Return "unknow.gif ";
}
VaR lastname = strurl. substring (Pos + 1, strurl. Length); // file suffix
Return lastname + ". GIF ";
}
The Second Script Function:
// Specifies the SRC of the dynamic IMG object
// The img id must be unique and different Based on the Resource ID in the current list.
// Write <SCRIPT> getimg (IMG Object ID, URL) immediately after the IMG object on the page </> to complete this function
// You can specify the SRC path dynamically.
// Call the showfiletypeimg (urlfilename) Function
Function getimg (imgid, urlfilename ){
VaR oimg = Document. getelementbyid (imgid); // obtain the IMG object based on the ID
VaR apppath;
Apppath = "<% = pagecontent. getapppath () %> ";
VaR mysrc;
Mysrc = apppath + "/images/myfiletype/" + showfiletypeimg (urlfilename );
Oimg. src = mysrc; // set its SRC
}
Put a piece of JavaScript code behind the IMG object (after it is followed, you can skip its onload event)
Note: The onerror event specifies that the specified icon is displayed when an unknown type exists, that is, the corresponding icon of this type is not found in the filetype directory.
==== Id = "myfileimg <s: property value =" FID "/>== indicates the dynamically generated IMG Object ID <s:...> is the property label of struts2.0. Here it belongs to iterator and returns the id value, so that the ID of IMG is unique.
Then pass the img id name to getimg in the string format, and pass the file path to getimg ()
=========== Daima ======================================
......
... <S: iterator value = "mylistitems" status = "currecord">
...
/images/myfiletype/unknow.gif '; "border =" 0 "src =" A "id =" myfileimg <s: property value = "RID"/> ">
<SCRIPT type = "text/JavaScript">
Getimg ("myfileimg <s: property value =" FID "/>", "<s: property value =" ffilenameurl "/> ")
</SCRIPT>
......
... </S: iterator>
......