init
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package org.nutz.mvc.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.PARAMETER, ElementType.FIELD})
|
||||
@Documented
|
||||
public @interface Param {
|
||||
boolean required() default true;
|
||||
|
||||
String value();
|
||||
|
||||
String dfmt() default "";
|
||||
|
||||
String df() default "//NOT EXIST IN//";
|
||||
|
||||
String locale() default "";
|
||||
|
||||
boolean array_auto_split() default true;
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package org.nutz.mvc.view;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.nutz.json.JsonFormat;
|
||||
import org.nutz.lang.Lang;
|
||||
import org.nutz.mvc.Mvcs;
|
||||
import org.nutz.mvc.View;
|
||||
|
||||
/**
|
||||
* 重写
|
||||
* 将数据采用json方式输出的试图实现
|
||||
*
|
||||
* @author zozoh(zozohtnt @ gmail.com)
|
||||
* @author mawn(ming300 @ gmail.com)
|
||||
* @author wendal(wendal1985 @ gmail.com)
|
||||
*/
|
||||
public class UTF8JsonView implements View {
|
||||
|
||||
public static String CT = "application/json";
|
||||
public static String JSONP_CT = "application/javascript";
|
||||
|
||||
protected JsonFormat format;
|
||||
|
||||
protected Object data;
|
||||
|
||||
protected boolean jsonp;
|
||||
|
||||
protected String jsonpParam;
|
||||
|
||||
public UTF8JsonView setData(Object data) {
|
||||
|
||||
this.data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UTF8JsonView setJsonp(boolean jsonp) {
|
||||
this.jsonp = jsonp;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UTF8JsonView setJsonpParam(String jsonpParam) {
|
||||
this.jsonpParam = jsonpParam;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UTF8JsonView(JsonFormat format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public UTF8JsonView() {
|
||||
this.format = new JsonFormat(false);
|
||||
}
|
||||
|
||||
public void render(HttpServletRequest req, HttpServletResponse resp, Object obj)
|
||||
throws IOException {
|
||||
|
||||
if (resp.getContentType() == null)
|
||||
if (jsonp)
|
||||
resp.setContentType(JSONP_CT);
|
||||
else
|
||||
resp.setContentType(CT);
|
||||
Writer writer = resp.getWriter();
|
||||
if (jsonp)
|
||||
writer.write(req.getParameter(jsonpParam == null ? "callback" : jsonpParam) + "(");
|
||||
|
||||
//默认过滤掉loginSessionId,password,salt 有些人就喜欢select ******
|
||||
List<String> lockFields = Lang.list("password", "salt", "loginSessionId");
|
||||
if (format.getLocked() != null) {
|
||||
String patternString = format.getLocked().pattern();
|
||||
String[] parts = patternString.split("\\|");
|
||||
for (String part : parts) {
|
||||
if (!lockFields.contains(part)) {
|
||||
lockFields.add(part);
|
||||
}
|
||||
}
|
||||
}
|
||||
format.setLocked(String.join("|", lockFields));
|
||||
|
||||
Mvcs.write(resp, writer, null == obj ? data : obj, format);
|
||||
if (jsonp)
|
||||
writer.write(");");
|
||||
}
|
||||
|
||||
public static final View NICE = new UTF8JsonView(JsonFormat.nice());
|
||||
public static final View COMPACT = new UTF8JsonView(JsonFormat.compact());
|
||||
public static final View FULL = new UTF8JsonView(JsonFormat.full());
|
||||
public static final View FORLOOK = new UTF8JsonView(JsonFormat.forLook());
|
||||
public static final View JSONP = new UTF8JsonView(JsonFormat.compact()).setJsonp(true);
|
||||
}
|
||||
Reference in New Issue
Block a user