处理不是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()这种方法.我不能 找到任何简单的方法如何获取文件 超出动作以外的要求 请求
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创建发布请求(带有文件)并使用@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屋!