Ajaxを使用してテキストと画像をアップロードしようとしています。Struts2 フレームワークとシンプルな JavaScript を使用しています。このアップロードエラーは、それを解決する方法を示しています。
JSPページ内
<s:form action="javascript:void(0)" onsubmit="javascript:postUserOwnMessages()"
enctype="multipart/form-data">
<s:textarea rows="2" cols="40" name="message" id="message1">
</s:textarea><br>
<s:file name="user_post_image" id="user_post_image"/>
<s:select name="msg_visibility" id="msg_visibility" list="#{'public':'Public', 'friends':'Friends','me':'Me only'}" value="public"/>
<s:submit value="Post"/>
</s:form>
同じページで使用される機能
<script type="text/javascript">
function postUserOwnMessages()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (typeof xmlhttp == "undefined")
{
ContentDiv.innerHTML="<h1>XMLHttp cannot be created!</h1>";
}
else{
var message1=document.getElementById('message1').value;
var user_post_image=document.getElementById('user_post_image').value;
var msg_visibility=document.getElementById('msg_visibility').value;
document.getElementById('message1').value="";
var query='ownmessages?message='+message1+'&user_post_image='+user_post_image
+'&msg_visibility='+msg_visibility;
xmlhttp.open("GET",query,true);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("messages_and_pages").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.send();
}
}
</script>
行動中
public class UserMessages extends ActionSupport {
private String userid;
private String message;
private File user_post_image;
private String user_post_imagePath;
private String user_post_imageContentType;
private String msg_visibility;
public String insert() {
System.out.println(getMessage()
+ " " + getUser_post_image()
+ " " + getUser_post_imageContentType() + " " + getUser_post_imagePath());
return SUCCESS;
}
}
次のエラー/警告を表示しています
WARNING: Error setting expression 'user_post_image' with value '[Ljava.lang.String;@1395750'
ognl.MethodFailedException: Method "setUser_post_image" failed for object social.action.UserMessages@765e8c [java.lang.NoSuchMethodException: social.action.UserMessages.setUser_post_image([Ljava.lang.String;)]
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1265)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1454)