Use JavaScript scripts-dynamically specify the SRC path of the IMG object-jsp-javascript-j2ee-hibernate-struts-2.0

Source: Internet
Author: User

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>

......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.