Jodd是一个开源的公用Java基础类库

源代码在线查看: import.java

软件大小: 1478 K
上传用户: asd66335065
关键词: Jodd Java 开源
下载地址: 免注册下载 普通下载 VIP

相关代码

				package jodd.servlet.tags.imports;
				
				import java.io.IOException;
				import java.util.ArrayList;
				
				import javax.servlet.RequestDispatcher;
				import javax.servlet.ServletException;
				import javax.servlet.http.HttpServletRequest;
				import javax.servlet.jsp.tagext.TagSupport;
				
				/**
				 * Includes local resource into the current page and organize templates. It
				 * can be used for including local content into the current page.
				 *
				 * Import tag can be used for managing web templates, macros or tiles,
				 * however it is called. There are 2 different types of usage:
				 *
				 * 
				 *
				 * Without template page. In this case every must have import for each
				 * part of the page that will be loaded. When it is repeated on many pages,
				 * this may become hard to maintain.
				 * 
				 *
				 * With templates. In this case, template is specified in an external jsp
				 * file that is imported in every page that will use that template. By using
				 * import parameters it is possible to manage included content.
				 *
				 * 
				 *
				 * Import tag is about 2x faster then default jsp:include (tomcat v5.)
				 *
				 */
				public class Import extends TagSupport {
				
					private static String SERVLET_PATH = "jodd.servlet.tags.include.servlet_path";
				
					private boolean isPageRelative;
				
					private String page = "";
				
					public void setPage(String v) {
						page = v;
						isPageRelative = (page.startsWith("/") == false);
					}
					protected String getPage() {
						return page;
					}
				
					private ArrayList attrs;
					public void addAttributeName(String s) {
						attrs.add(s);
					}
				
					/**
					 * Evaluate tag content.
					 *
					 * @return EVAL_BODY_INCLUDE
					 */
					public int doStartTag() {
						attrs = new ArrayList();
						return EVAL_BODY_AGAIN;
					}
				
				
					/**
					 * Finds and renders imported resource. A resource (page) can be specified
					 * relatively (when it doesn't start with a slash) or absolutely from the
					 * application root (when it starts with the slash). 
					 *
					 * This method will be called after tag process any existing param
					 * parameters.
					 *
					 * @return EVAL_PAGE
					 */
					public int doEndTag() {
						HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
						try {
							RequestDispatcher rd = null;
							boolean first = false;						// indicates topmost servlet
							String sp = "";
							String targetUrl = new String(page);
							
							if (isPageRelative == true) {
								sp = (String) request.getAttribute(SERVLET_PATH);
								if (sp == null) {
									first = true;
									sp = request.getServletPath();
									sp = sp.substring(0, sp.lastIndexOf('/'));
								}
								targetUrl = sp + '/' + page;
							}
							
							rd = request.getRequestDispatcher(targetUrl);
							ImportResponseWrapper irw = new ImportResponseWrapper(pageContext.getResponse(), pageContext.getOut());
				
							String previous_sp = sp;
							sp = targetUrl.substring(0, targetUrl.lastIndexOf('/'));
							request.setAttribute(SERVLET_PATH, sp);
							rd.include(request, irw);
							if (first == true) {
								request.removeAttribute(SERVLET_PATH);
							} else {
								request.setAttribute(SERVLET_PATH, previous_sp);
							}
						} catch (IOException ioe) {
						} catch (ServletException sex) {
						}
				
						// end of the page, remove not persistent params....
						for (int i = 0; i < attrs.size(); i++) {
							request.removeAttribute((String)attrs.get(i));
						}
						return EVAL_PAGE;
					}
				}
							

相关资源