处理不是 Action 请求的 Multipart 请求? [英] Handling Multipart request that is not an Action request?
问题描述
我一直在考虑是否可以处理不是 Action 请求的 Multipart 请求.我觉得这不可能是有原因的:
I've been thinking if it is possible to handle Multipart request that is not an Action request. There is a reason why it seems impossible to me :
只有 ActionRequest 实现getFile() 类方法.我不能找到任何简单的方法来获取文件除了 Action 之外的请求请求
Only ActionRequest implements getFile() kind of methods. I can't find any easy way how to get the file out of request other than Action request
如果我不使用 html 表单上传文件并且我不希望在操作请求后呈现视图怎么办 - 呈现阶段总是在操作阶段之后发生.
What if I don't use a html form to upload a file and I don't want a view to be rendered after action request - render phase happens always after the action phase.
如果我想通过 ajax 创建一个 post 请求(带有文件)并使用 @ResourceMapping 处理程序怎么办.我如何从 ResourceRequest 中获取它?
What if I want to create a post request (with file(s)) by ajax and use @ResourceMapping handler. How do I get it out of ResourceRequest ?
非常感谢您的想法.
推荐答案
这是处理多部分请求的最佳方式的模式"
This is the "pattern" that is afaik the best way of handling Multipart requests
来自视图层的动作请求转到这个方法:
Action request from view layer goes to this method:
@ActionMapping(params = "javax.portlet.action=sample")
public void response(MultipartActionRequest request, ActionResponse response) {
response.setRenderParameter("javax.portlet.action", "success");
List<MultipartFile> fileList = request.getFiles("file");
}
渲染阶段如下:
@RequestMapping(params = "javax.portlet.action=success")
public ModelAndView process(RenderRequest request, Model model) throws IOException {
Map map = new HashMap();
map.put("test", new Integer(1));
return new ModelAndView("someView", map);
}
您创建了一个bean"视图:
You create a "bean" view :
@Component("someView")
public class SomeView extends AbstractView {
private Logger logger = Logger.getLogger(SomeView.class);
@Override
protected void renderMergedOutputModel(Map map, HttpServletRequest request, HttpServletResponse response)
throws Exception {
logger.info("Resolving ajax request view - " + map);
JSONObject jsonObj = new JSONObject(map);
logger.info("content Type = " + getContentType());
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonObj.toString());
response.getWriter().flush();
}
}
您将 BeanNameViewResolver 添加到您的 servlet/portlet 上下文中:
You add BeanNameViewResolver into your servlet/portlet context:
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="1" />
这篇关于处理不是 Action 请求的 Multipart 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!