ソースを参照

jfinal 2.2 release ^_^

James 10 年 前
コミット
9afd5f8d40

+ 5 - 2
src/com/jfinal/core/Controller.java

@@ -587,9 +587,12 @@ public abstract class Controller {
 	private Controller doSetCookie(String name, String value, int maxAgeInSeconds, String path, String domain, Boolean isHttpOnly) {
 		Cookie cookie = new Cookie(name, value);
 		cookie.setMaxAge(maxAgeInSeconds);
-		if (path != null) {
-			cookie.setPath(path);
+		// set the default path value to "/"
+		if (path == null) {
+			path = "/";
 		}
+		cookie.setPath(path);
+		
 		if (domain != null) {
 			cookie.setDomain(domain);
 		}

+ 1 - 1
src/com/jfinal/ext/render/CaptchaRender.java

@@ -57,7 +57,7 @@ public class CaptchaRender extends Render {
 		vCode = HashKit.md5(vCode);
 		Cookie cookie = new Cookie(captchaName, vCode);
 		cookie.setMaxAge(-1);
-		// cookie.setPath("/");
+		cookie.setPath("/");
 		try {
 			// try catch 用来兼容不支持 httpOnly 的 tomcat、jetty
 			cookie.setHttpOnly(true);

+ 11 - 11
src/com/jfinal/json/JFinalJson.java

@@ -43,9 +43,9 @@ public class JFinalJson extends Json {
 	
 	private static int defaultConvertDepth = 15;
 	
-	private int convertDepth = defaultConvertDepth;
-	private String timestampPattern = "yyyy-MM-dd HH:mm:ss";
-	private String datePattern = "yyyy-MM-dd";
+	protected int convertDepth = defaultConvertDepth;
+	protected String timestampPattern = "yyyy-MM-dd HH:mm:ss";
+	protected String datePattern = "yyyy-MM-dd";
 	
 	/**
 	 * 设置全局性默认转换深度
@@ -85,7 +85,7 @@ public class JFinalJson extends Json {
 		return new JFinalJson();
 	}
 	
-	private String mapToJson(Map map, int depth) {
+	protected String mapToJson(Map map, int depth) {
 		if(map == null) {
 			return "null";
 		}
@@ -107,7 +107,7 @@ public class JFinalJson extends Json {
 		return sb.toString();
 	}
 	
-	private String toKeyValue(String key, Object value, StringBuilder sb, int depth){
+	protected String toKeyValue(String key, Object value, StringBuilder sb, int depth){
 		sb.append('\"');
         if(key == null)
             sb.append("null");
@@ -120,7 +120,7 @@ public class JFinalJson extends Json {
 		return sb.toString();
 	}
 	
-	private String listToJson(List list, int depth) {
+	protected String listToJson(List list, int depth) {
 		if(list == null)
 			return "null";
 		
@@ -149,7 +149,7 @@ public class JFinalJson extends Json {
 	/**
 	 * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
 	 */
-	private String escape(String s) {
+	protected String escape(String s) {
 		if(s == null)
 			return null;
         StringBuilder sb = new StringBuilder();
@@ -157,7 +157,7 @@ public class JFinalJson extends Json {
         return sb.toString();
     }
 	
-	private void escape(String s, StringBuilder sb) {
+	protected void escape(String s, StringBuilder sb) {
 		for(int i=0; i<s.length(); i++){
 			char ch = s.charAt(i);
 			switch(ch){
@@ -205,7 +205,7 @@ public class JFinalJson extends Json {
 		return toJson(object, convertDepth);
 	}
 	
-	private String toJson(Object value, int depth) {
+	protected String toJson(Object value, int depth) {
 		if(value == null || (depth--) < 0)
 			return "null";
 		
@@ -265,7 +265,7 @@ public class JFinalJson extends Json {
 		return "\"" + escape(value.toString()) + "\"";
 	}
 	
-	private String otherToJson(Object value, int depth) {
+	protected String otherToJson(Object value, int depth) {
 		if (value instanceof Character) {
 			return "\"" + escape(value.toString()) + "\"";
 		}
@@ -292,7 +292,7 @@ public class JFinalJson extends Json {
 		return beanToJson(value, depth);
 	}
 	
-	private String beanToJson(Object model, int depth) {
+	protected String beanToJson(Object model, int depth) {
 		Map map = new HashMap();
 		Method[] methods = model.getClass().getMethods();
 		for (Method m : methods) {

+ 2 - 2
src/com/jfinal/json/Jackson.java

@@ -39,9 +39,9 @@ public class Jackson extends Json {
 	private static boolean defaultGenerateNullValue = true;
 	
 	// generateNullValue 通过设置此值,可临时改变默认生成 null value 的行为
-	private Boolean generateNullValue = null;
+	protected Boolean generateNullValue = null;
 	
-	private ObjectMapper objectMapper = new ObjectMapper();
+	protected ObjectMapper objectMapper = new ObjectMapper();
 	
 	public static void setDefaultGenerateNullValue(boolean defaultGenerateNullValue) {
 		Jackson.defaultGenerateNullValue = defaultGenerateNullValue;

+ 41 - 5
src/com/jfinal/plugin/activerecord/ModelRecordElResolver.java

@@ -32,6 +32,27 @@ public class ModelRecordElResolver extends ELResolver {
 	
 	static JspApplicationContext jspApplicationContext = null;
 	
+	private static boolean resolveBeanAsModel = false;
+	
+	/**
+	 * 设置为 true 时,使用生成器生成的实现了 IBean 接口的 Class 将被当成  Model 来处理,
+	 * getter 不被 jsp/jstl 用来输出数据,仍然使用 model.get(String attr) 来输出数据。
+	 * 
+	 * 有利于在关联查询时输出无 getter 方法的字段值。建议mysql数据表中的字段采用驼峰命名,
+	 * 表名仍然用下划线方式命名。 resolveBeanAsModel 默认值为 false。
+	 * 
+	 * 注意:这里所指的 Bean 仅仅指用 BaseModelGenerator 生成的实现了 IBean接口后的类文件
+	 * <pre>
+	 * 使用方式, 在 YourJFinalConfig 中创建方法,并调用本方法:
+	 * public void afterJFinalStart() {
+	 *    ModelRecordElResolver.setResolveBeanAsModel(true);
+	 * }
+	 * </pre>
+	 */
+	public static void setResolveBeanAsModel(boolean resolveBeanAsModel) {
+		ModelRecordElResolver.resolveBeanAsModel = resolveBeanAsModel;
+	}
+	
 	/**
 	 * Compatible for JspRender.setSupportActiveRecord(true);
 	 * Delete it in the future
@@ -55,7 +76,10 @@ public class ModelRecordElResolver extends ELResolver {
 	}
 	
 	public Object getValue(ELContext context, Object base, Object property) {
-		if (isWorking == false || base instanceof IBean) {
+		if (isWorking == false) {
+			return null;
+		}
+		if (resolveBeanAsModel == false && base instanceof IBean) {
 			return null;
 		}
 		
@@ -75,7 +99,10 @@ public class ModelRecordElResolver extends ELResolver {
 	}
 	
 	public Class<?> getType(ELContext context, Object base, Object property) {
-		if (isWorking == false || base instanceof IBean) {
+		if (isWorking == false) {
+			return null;
+		}
+		if (resolveBeanAsModel == false && base instanceof IBean) {
 			return null;
 		}
 		
@@ -84,7 +111,10 @@ public class ModelRecordElResolver extends ELResolver {
 	}
 	
 	public void setValue(ELContext context, Object base, Object property, Object value) {
-		if (isWorking == false || base instanceof IBean) {
+		if (isWorking == false) {
+			return ;
+		}
+		if (resolveBeanAsModel == false && base instanceof IBean) {
 			return ;
 		}
 		
@@ -107,7 +137,10 @@ public class ModelRecordElResolver extends ELResolver {
 	}
 	
 	public boolean isReadOnly(ELContext context, Object base, Object property) {
-		if (isWorking == false || base instanceof IBean) {
+		if (isWorking == false) {
+			return false;
+		}
+		if (resolveBeanAsModel == false && base instanceof IBean) {
 			return false;
 		}
 		
@@ -125,7 +158,10 @@ public class ModelRecordElResolver extends ELResolver {
 	
 	// Do not invoke context.setPropertyResolved(true) for this method
 	public Class<?> getCommonPropertyType(ELContext context, Object base) {
-		if (isWorking == false || base instanceof IBean) {
+		if (isWorking == false) {
+			return null;
+		}
+		if (resolveBeanAsModel == false && base instanceof IBean) {
 			return null;
 		}
 		

+ 1 - 1
src/com/jfinal/plugin/activerecord/generator/MetaBuilder.java

@@ -47,7 +47,7 @@ public class MetaBuilder {
 	
 	protected String[] removedTableNamePrefixes = null;
 	
-	private TypeMapping typeMapping = new TypeMapping();
+	protected TypeMapping typeMapping = new TypeMapping();
 	
 	public MetaBuilder(DataSource dataSource) {
 		if (dataSource == null) {

+ 21 - 7
src/com/jfinal/render/CaptchaRender.java

@@ -39,7 +39,7 @@ import com.jfinal.render.Render;
  */
 public class CaptchaRender extends Render {
 
-	private static final String DEFAULT_CAPTCHA_NAME = "_jfinal_captcha";
+	private static String captchaName = "_jfinal_captcha";
 
 	// 默认的验证码大小
 	private static final int WIDTH = 108, HEIGHT = 40;
@@ -54,7 +54,17 @@ public class CaptchaRender extends Render {
 		new Font("Impact", Font.BOLD, 32),
 		new Font(Font.MONOSPACED, Font.BOLD, 40)
 	};
-
+	
+	/**
+	 * 设置 captchaName
+	 */
+	public static void setCaptchaName(String captchaName) {
+		if (StrKit.isBlank(captchaName)) {
+			throw new IllegalArgumentException("captchaName can not be blank.");
+		}
+		CaptchaRender.captchaName = captchaName;
+	}
+	
 	/**
 	 * 生成验证码
 	 */
@@ -63,9 +73,9 @@ public class CaptchaRender extends Render {
 		String vCode = drawGraphic(image);
 		vCode = vCode.toUpperCase();	// 转成大写重要
 		vCode = HashKit.md5(vCode);
-		Cookie cookie = new Cookie(DEFAULT_CAPTCHA_NAME, vCode);
+		Cookie cookie = new Cookie(captchaName, vCode);
 		cookie.setMaxAge(-1);
-		// cookie.setPath("/");
+		cookie.setPath("/");
 		try {
 			// try catch 用来兼容不支持 httpOnly 的 tomcat、jetty
 			cookie.setHttpOnly(true);
@@ -82,8 +92,12 @@ public class CaptchaRender extends Render {
 		try {
 			sos = response.getOutputStream();
 			ImageIO.write(image, "jpeg", sos);
+		} catch (IOException e) {
+			if (getDevMode()) {
+				throw new RenderException(e);
+			}
 		} catch (Exception e) {
-			throw new RuntimeException(e);
+			throw new RenderException(e);
 		} finally {
 			if (sos != null) {
 				try {sos.close();} catch (IOException e) {LogKit.logNothing(e);}
@@ -183,9 +197,9 @@ public class CaptchaRender extends Render {
 		
 		userInputCaptcha = userInputCaptcha.toUpperCase();	// 转成大写重要
 		userInputCaptcha = HashKit.md5(userInputCaptcha);
-		boolean result = userInputCaptcha.equals(controller.getCookie(DEFAULT_CAPTCHA_NAME));
+		boolean result = userInputCaptcha.equals(controller.getCookie(captchaName));
 		if (result == true) {
-			controller.removeCookie(DEFAULT_CAPTCHA_NAME);
+			controller.removeCookie(captchaName);
 		}
 		return result;
 	}

+ 9 - 9
src/com/jfinal/render/FileRender.java

@@ -90,9 +90,10 @@ public class FileRender extends Render {
         	rangeRender();
 	}
 	
-	private String encodeFileName(String fileName) {
+	protected String encodeFileName(String fileName) {
 		try {
-			return new String(fileName.getBytes("GBK"), "ISO8859-1");
+			// return new String(fileName.getBytes("GBK"), "ISO8859-1");
+			return new String(fileName.getBytes(getEncoding()), "ISO8859-1");
 		} catch (UnsupportedEncodingException e) {
 			return fileName;
 		}
@@ -110,14 +111,13 @@ public class FileRender extends Render {
                 outputStream.write(buffer, 0, len);
             }
             outputStream.flush();
-        }
-        catch (IOException e) {
-        	if (getDevMode())	throw new RenderException(e);
-        }
-        catch (Exception e) {
+        } catch (IOException e) {
+        	if (getDevMode()) {
+        		throw new RenderException(e);
+        	}
+        } catch (Exception e) {
         	throw new RenderException(e);
-        }
-        finally {
+        } finally {
             if (inputStream != null)
                 try {inputStream.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);}
             if (outputStream != null)