1 18 19 package org.apache.struts.taglib.logic; 20 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.tagext.TagSupport ; 25 26 import org.apache.struts.action.ActionForward; 27 import org.apache.struts.config.ModuleConfig; 28 import org.apache.struts.taglib.TagUtils; 29 import org.apache.struts.util.MessageResources; 30 31 37 public class ForwardTag extends TagSupport { 38 39 41 44 protected static MessageResources messages = 45 MessageResources.getMessageResources( 46 "org.apache.struts.taglib.logic.LocalStrings"); 47 48 52 protected String name = null; 53 54 public String getName() { 55 return (this.name); 56 } 57 58 public void setName(String name) { 59 this.name = name; 60 } 61 62 64 69 public int doStartTag() throws JspException { 70 71 return (SKIP_BODY); 72 73 } 74 75 81 public int doEndTag() throws JspException { 82 83 ActionForward forward = null; 85 ModuleConfig config = TagUtils.getInstance().getModuleConfig(pageContext); 86 87 if (config != null){ 88 forward = (ActionForward) config.findForwardConfig(name); 89 } 90 91 if (forward == null) { 92 JspException e = 93 new JspException (messages.getMessage("forward.lookup", name)); 94 TagUtils.getInstance().saveException(pageContext, e); 95 throw e; 96 } 97 98 String path = forward.getPath(); 100 path = config.getPrefix() + path; 101 102 if (forward.getRedirect()) { 103 this.doRedirect(path); 104 } else { 105 this.doForward(path); 106 } 107 108 return (SKIP_PAGE); 110 111 } 112 113 119 protected void doForward(String path) throws JspException { 120 try { 121 pageContext.forward(path); 122 123 } catch (Exception e) { 124 TagUtils.getInstance().saveException(pageContext, e); 125 throw new JspException ( 126 messages.getMessage("forward.forward", name, e.toString())); 127 } 128 } 129 130 136 protected void doRedirect(String path) throws JspException { 137 HttpServletRequest request = 138 (HttpServletRequest ) pageContext.getRequest(); 139 140 HttpServletResponse response = 141 (HttpServletResponse ) pageContext.getResponse(); 142 143 try { 144 if (path.startsWith("/")) { 145 path = request.getContextPath() + path; 146 } 147 148 response.sendRedirect(response.encodeRedirectURL(path)); 149 150 } catch (Exception e) { 151 TagUtils.getInstance().saveException(pageContext, e); 152 throw new JspException ( 153 messages.getMessage("forward.redirect", name, e.toString())); 154 } 155 } 156 157 160 public void release() { 161 162 super.release(); 163 name = null; 164 165 } 166 167 } 168 | Popular Tags |