NutzCN Logo
问答 适配器的问题
发布于 3351天前 作者 qq_6c5a7e9a 2212 次浏览 复制 上一个帖子 下一个帖子
标签: mvc

JSP:
Java:
public String 好傻逼(
@Param("::user.") User userp,
@Param("::children") ArrayList children
) {
System.out.println(Json.toJson(userp));
System.out.println(Json.toJson(children));
return "哈哈";
}
两个输出都正常
JSP:
Java:
@AdaptBy(type = UploadAdaptor.class, args = { "${app.root}/WEB-INF/tmp" })
public String 好傻逼(
@Param("::user.") User userp,
@Param("::children") ArrayList children
) {
System.out.println(Json.toJson(userp));
System.out.println(Json.toJson(children));
return "哈哈";
}
第二个输出为null

13 回复

JSP:《form action="test" method="post"》
JSP:《form action="test" method="post" enctype="multipart/form-data"》

待我试一下

我测试了一下, 入口方法如下

	@At("/upload/beans")
	@Ok("raw")
	@AdaptBy(type=UploadAdaptor.class)
	public void uploadWithBeans(@Param("::user.")User user, @Param("file")TempFile f) {
		System.out.println(Json.toJson(user));
		System.out.println(f.getMeta());
		System.out.println(f.getFile());
	}

JSP文件如下,只贴form部分

<form action="${base }/demo/upload/beans" method="post"  enctype="multipart/form-data">
	用户名<input name="user.name">
	id<input name="user.id">
	文件<input type="file" name="file">
	<button type="submit">提交</button>
</form>

后台输出

2015-10-21 16:48:59,967 org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:181) DEBUG - Select Html4 Form upload parser --> /nutzbook/demo/upload/beans
2015-10-21 16:48:59,972 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:39) DEBUG - FastUpload : /demo/upload/beans
2015-10-21 16:48:59,974 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:54) DEBUG - info created
2015-10-21 16:48:59,974 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:60) DEBUG - Params map created - 0 params
2015-10-21 16:48:59,976 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:77) DEBUG - boundary: 
------WebKitFormBoundaryrPuGxXG2nkAlAh37
2015-10-21 16:48:59,983 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:100) DEBUG - skip first boundary
2015-10-21 16:48:59,983 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:111) DEBUG - Reading...
2015-10-21 16:48:59,984 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) DEBUG - Upload File info: FilePath=[null],fieldName=[user.name]
2015-10-21 16:48:59,984 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) DEBUG - Found a param, name=[user.name] value=[wendal]
2015-10-21 16:48:59,984 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) DEBUG - Upload File info: FilePath=[null],fieldName=[user.id]
2015-10-21 16:48:59,984 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) DEBUG - Found a param, name=[user.id] value=[2]
2015-10-21 16:48:59,984 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) DEBUG - Upload File info: FilePath=[logo.png],fieldName=[file]
2015-10-21 16:48:59,984 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:137) DEBUG - Upload Info: name=logo.png,content_type=image/png
2015-10-21 16:48:59,997 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:230) DEBUG - ...Done 20731 bytes readed
{
   "id" :2,
   "name" :"wendal",
   "locked" :false
}
org.nutz.mvc.upload.FieldMeta@28e251
C:\Users\wendal\AppData\Local\Temp\00\00\00\00\00\00\00\01.png

可见, User类接受到正确的数据,name和id均有数据, 而TempFile也有上传的文件

入口方法如下:

@AdaptBy(type = UploadAdaptor.class, args = { "${app.root}/WEB-INF/tmp" })
	public String 好傻逼(
			@Param("::user.") User userp,
			@Param("::children.") ArrayList<Child> children,
			@Param("cnmlgb") File[] files
	) throws FileNotFoundException {
		System.out.println(files.length);
		System.out.println(Json.toJson(userp));
		System.out.println(Json.toJson(children));
		return "哈哈";
	}

JSP如下

<form action="test" method="post" enctype="multipart/form-data">
		
		<input name="user.name" value="胡涛"></br>
		<input name="user.age" value="23"></br>
		<input name="user.birthday" value="1991-01-06"></br>
		<input name="user.sex" value="true"></br>
		<input name="user.children[sb].name" value="女儿"></br>
		<input name="user.children[sb].age" value="13"></br>
		<input name="user.children[cnm].name" value="儿子"></br>
		<input name="user.children[cnm].age" value="12"></br></br>
		
		<input name="children[sb].name" value="老大"></br>
		<input name="children[sb].age" value="17"></br>
		<input name="children[cnm].name" value="老二"></br>
		<input name="children[cnm].age" value="18"></br>
		<input name="children[nsn].name" value="老三"></br>
		<input name="children[nsn].age" value="19"></br></br>
		
		<input name="cnmlgb" type="file"></br>
		<input name="cnmlgb" type="file"></br>
		<input name="cnmlgb" type="file"></br>
		
		<input type="submit">
	</form>

控制台输出

3
{
   "name" :"胡涛",
   "age" :23,
   "birthday" :"1991-01-06 00:00:00",
   "sex" :true,
   "children" :[{
      "name" :"女儿",
      "age" :13
   }, {
      "name" :"儿子",
      "age" :12
   }]
}
null

如果不选择任何文件的话,或提示错误

2015-10-21 17:2:28.934 WARN [http-bio-8080-exec-17] Error@/user/test :
java.lang.NullPointerException
	at com.hutao.controler.UserControler.好傻逼(UserControler.java:36)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:40)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:113)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:40)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:183)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:744)

file数组的话 要用tempfile

我知道什么原因了, 的确是个bug

UploadAdaptor的156-157行

        if (List.class.isAssignableFrom(clazz))
            return new MapListInjector(paramName);

谢谢你,爱死你了

给你一个临时解决的方案, 集成然后改造一下方法

package net.wendal.nutzbook.mvc;

import java.io.IOException;
import java.lang.reflect.Type;

import org.nutz.mvc.adaptor.ParamInjector;
import org.nutz.mvc.adaptor.injector.ObjectNavlPairInjector;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.upload.UploadAdaptor;
import org.nutz.mvc.upload.UploadingContext;

public class UploadAdaptor2 extends UploadAdaptor {

	protected ParamInjector evalInjectorBy(Type type, Param param) {
		if (param != null && param.value().startsWith("::"))
			return new ObjectNavlPairInjector(param.value().substring(2), type);
		return super.evalInjectorBy(type, param);
	}
	
	public UploadAdaptor2() throws IOException {
	}

	public UploadAdaptor2(UploadingContext context) {
		super(context);
	}

	public UploadAdaptor2(String path) {
		super(path);
	}

	public UploadAdaptor2(String path, int buffer) {
		super(path, buffer);
	}

	public UploadAdaptor2(String path, int buffer, String charset) {
		super(path, buffer, charset);
	}

	public UploadAdaptor2(String path, int buffer, String charset, int poolSize) {
		super(path, buffer, charset, poolSize);
	}

	public UploadAdaptor2(String path, int buffer, String charset, int poolSize, int maxFileSize) {
		super(path, buffer, charset, poolSize, maxFileSize);
	}

}

至于没选文件就点上传,然后抛异常

java.lang.NullPointerException at com.hutao.controler.UserControler.好傻逼(UserControler.java:36)

那是你代码没判断是否为null

添加回复
请先登陆
回到顶部