【发布时间】:2018-06-11 05:38:54
【问题描述】:
我正在通过 ajax post 方法将表单数据发送到后端(java+struts)。
$( "#profileModalForm" ).submit(function( event ) {
var formData = new FormData(this);
formData.append('image', $('input[type=file]')[0].files[0]);
formData.append('section', 'general');
formData.append('action', 'previewImg');
$.ajax({
cache: false,
url: 'SaveProfilePopupData.ws',
type: "POST",
data: formData,
contentType: false,
processData: false,
success: function (html) {
$('#notificationArea').html(html);
}
});
event.preventDefault();
});
浏览器控制台将这些显示为发送的参数。
-----------------------------181712305727018
Content-Disposition: form-data; name="hiddenIdTxt"
1000
-----------------------------181712305727018
Content-Disposition: form-data; name="passwordTxt"
abcd
-----------------------------181712305727018
Content-Disposition: form-data; name="repeatPasswordTxt"
abcd
-----------------------------181712305727018
Content-Disposition: form-data; name="fileInput"; filename="myImage.jpg"
Content-Type: image/jpeg
Øÿà.........(Some unreadable data here)
但我无法从 HttpServletRequest 中读取参数和部分。
public ActionForward saveProfilePopupData(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
TransactionResult result = TransactionResult.getInstance();
if (WSUtil.getCurrentUser(request) != null) {
try {
for (Part part : request.getParts()) {
//First Logger
WSLogger.info(part.getSize());
}
//Second Logger
WSLogger.info(request.getParameter("hiddenIdTxt").toString());
result.setResultMessage("Profile Updated!");
result.setResultType(TransactionResultType.SUCCESS);
} catch (Exception ex) {
WSLogger.error("UserController - saveProfilePopupData Exception", ex);
}
} else {
return mapping.findForward(SESEXPIRE);
}
request.setAttribute("RESULT", result);
return mapping.findForward(SUCCESS);
}
第一个记录器显示一个空行,第二个记录器给出空指针异常。我在这里做错了什么? 这是我的应用程序的依赖项。
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-core</artifactId>
<version>1.3.10</version>
<exclusions>
<exclusion>
<artifactId>antlr</artifactId>
<groupId>antlr</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-extras</artifactId>
<version>1.3.10</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-taglib</artifactId>
<version>1.3.10</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>net.sf.flexjson</groupId>
<artifactId>flexjson</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
【问题讨论】:
-
您是否将 MultipartConfig 注解添加到 WebServlet 中?
-
是的,我已将它添加到课堂之上。
-
你为什么要这样做而不是仅仅使用 Struts?
-
如何只使用struts?
标签: servlets jakarta-ee httprequest struts multipartform-data