📄 defaultrequestprocessor.java
字号:
ei.handle(error, param.getAction(), null, new Object[] { param });
}
return false;
}
public Page getResult(Module module, WebForm form, IWebAction action)
throws ServletException, FrameworkException {
// form.setEasyJWebResult(new HashMap());
Page page = null;
try {
page = action.execute(form, module);
} catch (Exception e) {
throw new FrameworkException(e.getMessage(), e);
// servlet.error(ActionContext.getContext().getRequest(),
// ActionContext.getContext().getResponse(), e);
}
// 保存表单form中的数据
// 处理没有定义属性property的表单
if (form != null && form.getClass() == WebForm.class) {
form.setProperty(form.getTextElement());// 只保存文本属性
form.getProperty().putAll(form.getFileElement());
}
if (form != null && (form.getProperty() != null)) {
Iterator it = form.getProperty().keySet().iterator();
while (it.hasNext()) {
String name = (String) it.next();
form.addResult(name, form.get(name));
}
}
// if(form!=null &&
// (form.getClass()==WebForm.class))form.getProperty().clear();
// request.setAttribute("easyJWebResult",easyJWebResult);
return page;
}
// private Page handleError(HttpServletRequest request,
// HttpServletResponse response, List handlers, WebForm form,
// Throwable ex) throws ServletException{
// Page page = null;
// for (Iterator it = handlers.iterator(); it.hasNext();) {
// IErrorHandler handler = (IErrorHandler) it.next();
// page = handler.handle(request, response, form, ex);
// if (page != null) {
// return page;
// }
// }
// return null;
// }
private Map doTheme(HttpServletRequest request, IPathMappingRuler pathRuler) {
if (this.getThemeManager() == null) {
this
.setThemeManager(new PropertiesNameBasedThemeManager(
pathRuler));
try {
ITheme theme = this.getThemeManager().getTheme(request);
if (theme != null) {
return theme.getThemeMap();
}
} catch (ThemeProcessException ex) {
logger.error(ex);
}
}
return null;
}
protected void doForward(String uri, HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
rd.forward(request, response);
}
protected void doTemplate(String uri, WebForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
Context context = createContext(form);
context.put("session", session2map(request.getSession()));
context.put("request", request2map(request));
context.put("theme", this.theme);
try {
Template template = getTemplate(uri, request.getCharacterEncoding());
if (context != null && (template != null)) {
mergeTemplate(template, context, response);
} else {
throw new FrameworkException("模块合成出错!");
}
} catch (ResourceNotFoundException rnfe) {
throw new FrameworkException("找不到模板!"
+ webConfig.getTemplateBasePath() + uri, rnfe);
} catch (ParseErrorException pee) {
throw new FrameworkException("模板文件中存在语法错误,不能正常解析!"
+ webConfig.getTemplateBasePath() + uri, pee);
} catch (MethodInvocationException mie) {
throw new FrameworkException("模板方法调用错误:", mie);
} catch (Exception e) {
throw new FrameworkException("查找模板错误!", e);
}
}
private String parseTemplateName(String templateName)
{
String name=templateName;
if(name.length()>10)
{
String begin=name.substring(0,10);
if(begin.equalsIgnoreCase("classpath:"))name=templateName.substring(10);
}
return name;
}
protected Template getTemplate(String templateName, String encoding)
throws ResourceNotFoundException, ParseErrorException,
MethodInvocationException, Exception {
String name=parseTemplateName(templateName);
if (webConfig.isDebug())
return RuntimeSingleton.getTemplate(name, encoding);// 若为Debug状态,则每次都重新载入模板
String templatePath = (String) Velocity.getProperty("file.resource.loader.path");
java.io.File f = new java.io.File(new java.io.File(templatePath),name);
Template template = (Template) templateCache.get(name);// 先从Cache中读取模板文件
// 这里得进一步完善,当用户已经更改模板文件后,需要能够自动加载,同时增加Cache数量的限制
if (template == null
|| (f.exists() && f.lastModified() > template.getLastModified()))
synchronized (templateCache) {
{
logger.info("重新加载模板文件:" +name);
template = RuntimeSingleton.getTemplate(name, encoding);// name
templateCache.put(name, template);
}
}
return template;
}
protected Context createContext(WebForm form) {
Map result = form.getEasyJWebResult();
Context context = new VelocityContext();
Iterator it = result.keySet().iterator();
while (it.hasNext()) {
String name = (String) it.next();
context.put(name, result.get(name));
createUtilContext(context);
}
return context;
}
protected void createUtilContext(Context context) {
context.put("HtmlUtil", HtmlUtil.getInstance());
context.put("CommUtil", CommUtil.getInstance());
context.put("TagUtil", TagUtil.getInstance());
}
protected Map session2map(HttpSession session) {
Map map = new HashMap();
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
map.put(key, session.getAttribute(key));
}
return map;
}
protected Map request2map(HttpServletRequest request) {
Map map = new HashMap();
Enumeration e = request.getAttributeNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
map.put(key, request.getAttribute(key));
}
return map;
}
protected void mergeTemplate(Template template, Context context,
HttpServletResponse response) {
VelocityWriter vw = null;
Writer writer = null;
try {
response.setCharacterEncoding(template.getEncoding());
response.setContentType("text/html;charset=utf-8");
writer = servlet.getResponseWriter(response);
vw = (VelocityWriter) writerPool.get();
if (vw == null) {
vw = new VelocityWriter(writer, 4 * 1024, true);
} else {
vw.recycle(writer);
}
template.merge(context, vw);
} catch (Exception e) {
logger.error(e);
// throw new FrameWorkException("合并模块错误!",e);
} finally {
if (vw != null) {
try {
vw.flush();
vw.recycle(null);
this.writerPool.put(vw);
} catch (Exception e) {
logger.error("Trouble releasing VelocityWriter: "
+ e.getMessage());
}
}
}
}
protected ServletContext getServletContext() {
return (servlet.getServletContext());
}
public WebConfig getWebConfig() {
return webConfig;
}
public void setWebConfig(WebConfig webConfig) {
this.webConfig = webConfig;
}
public IThemeManager getThemeManager() {
return themeManager;
}
public void setThemeManager(IThemeManager themeManager) {
this.themeManager = themeManager;
}
public IErrorHandlerManager getErrorHandlerManager() {
return errorHandlerManager;
}
public void setErrorHandlerManager(IErrorHandlerManager errorHandlerManager) {
this.errorHandlerManager = errorHandlerManager;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -