1 18 19 package org.apache.struts.tiles.actions; 20 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.struts.action.Action; 30 import org.apache.struts.action.ActionForm; 31 import org.apache.struts.action.ActionForward; 32 import org.apache.struts.action.ActionMapping; 33 import org.apache.struts.tiles.ComponentDefinition; 34 import org.apache.struts.tiles.DefinitionsFactoryException; 35 import org.apache.struts.tiles.DefinitionsUtil; 36 import org.apache.struts.tiles.FactoryNotFoundException; 37 import org.apache.struts.tiles.NoSuchDefinitionException; 38 import org.apache.struts.tiles.TilesUtil; 39 40 69 public class DefinitionDispatcherAction extends Action { 70 71 74 protected static Log log = LogFactory.getLog(DefinitionDispatcherAction.class); 75 76 90 public ActionForward execute( 91 ActionMapping mapping, 92 ActionForm form, 93 HttpServletRequest request, 94 HttpServletResponse response) 95 throws Exception { 96 97 String parameter = mapping.getParameter(); 100 if (parameter == null) { 101 parameter = "def"; 102 } 103 104 String name = request.getParameter(parameter); 106 if (name == null) { 107 log.error("Can't get parameter '" + parameter + "'."); 108 109 return mapping.findForward("error"); 110 } 111 112 try { 114 ComponentDefinition definition = 116 TilesUtil.getDefinition( 117 name, 118 request, 119 getServlet().getServletContext()); 120 121 if (log.isDebugEnabled()) { 122 log.debug("Get Definition " + definition); 123 } 124 125 DefinitionsUtil.setActionDefinition(request, definition); 126 127 } catch (FactoryNotFoundException e) { 128 log.error("Can't get definition factory.", e); 129 return mapping.findForward("error"); 130 131 } catch (NoSuchDefinitionException e) { 132 log.error("Can't get definition '" + name + "'.", e); 133 return mapping.findForward("error"); 134 135 } catch (DefinitionsFactoryException e) { 136 log.error("General Factory error '" + e.getMessage() + "'.", e); 137 return mapping.findForward("error"); 138 139 } catch (Exception e) { 140 log.error("General error '" + e.getMessage() + "'.", e); 141 return mapping.findForward("error"); 142 } 143 144 return mapping.findForward("success"); 145 146 } 147 148 151 protected void printError(HttpServletResponse response, String msg) 152 throws IOException { 153 response.setContentType("text/plain"); 154 PrintWriter writer = response.getWriter(); 155 writer.println(msg); 156 writer.flush(); 157 writer.close(); 158 } 159 } 160 | Popular Tags |