【实用】JSP页面图片回显的几种方式

方式一   在Controller层中进行配置

@RequestMapping(value = "/StudentSideController_goWorksDetail.do", method = RequestMethod.GET)
	public String goWorksDetail(HttpSession session, ModelMap data, Integer worksId) throws Exception {
		String worksPath = CommonUtil.getConfig("common","STUDENT_WORK_FILE_PATH");
		StringBuffer url = request.getRequestURL();
		String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
		String realpath = tempContextUrl + "upload/studentwork";

		PageData param = getPageData();
		param.put("orgId", session.getAttribute("orgId"));
		param.put("studentId", session.getAttribute("studentId"));
		param.put("worksId", worksId);
		try {
			PageData works = studentWorkFacade.getWorksManage(param);
			if (null != works && null != works.get("url")) {
				String imgURL = works.get("url").toString();
				imgURL = realpath + imgURL.replaceFirst(worksPath, "");
				works.put("url", imgURL);
			}
			data.put("works", works);
		} catch (Exception e) {
			logger.debug("获取作品信息失败", e);
			throw new WeChatPageException("获取作品信息失败");
		}
		return "weChat/studentSide/worksDetail";
	}

 

方式二   在JSP页面进行配置

<%@ page import="com.douples.common.util.*"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%
	String path = request.getContextPath() + "/";
	String file_path = CommonUtil.getConfig("common","STUDENT_WORK_FILE_PATH");
	int file_path_length = file_path.length();
	StringBuffer url = request.getRequestURL();  
	String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
	String realpath=tempContextUrl+"upload/studentwork";
%>
function showImages(){
		var imagesShow ='${works.url}';
    	if(imagesShow!=null || imagesShow!=''){
    	var	filePath = imagesShow;
    	var path = filePath.substring(<%=file_path_length%>, filePath.length);
		var realpath="<%=realpath%>"+path;
		$("#showImages").append('images')
    	}
	}
	 mui.ready(function () {
		 showImages();
	 })

CommonUtil.java 公共类

   /**
	 * 根据properties文件名和key值获取配置信息
	 * @param properties
	 * @param key
	 * @return
	 */
	public static String getConfig(String properties,String key){
		return ResourceBundle.getBundle(properties).getString(key);
	}

common.properties  图片地址属性文件

STUDENT_WORK_FILE_PATH=D:/upload/studentwork

二选一即可

你可能感兴趣的:(Java基础知识,JavaScript)