JSF Seam @RequestParameter 文件/图像

发布于 2024-11-09 02:08:11 字数 1455 浏览 0 评论 0原文

我有一个支持 Bean,其中我读取未绑定到组件的参数。 Seam 提供了读取参数的功能。我使用

@RequestParameter private String param1;

这种方式,他不会在验证错误时跳过我的操作方法,就像我使用时一样

,因为我不想渲染 HTML 响应,而是重用业务逻辑。相反,我抓取响应流并自己编写渲染的响应,

FacesContext ctx = FacesContext.getCurrentInstance();

final HttpServletResponse resp = (HttpServletResponse)ctx.getExternalContext().getResponse();      
resp.getOutputStream().write(xml.getBytes());
resp.getOutputStream().flush();
resp.getOutputStream().close();

ctx.responseComplete(); // render response phase done

我尝试从 HttpServletRequest inputStream 读取文件,但该文件已经是空的。有没有办法仍然从 JSF 获取“文件”。我可以使用一个单独的 servlet 并在那里处理它,但这会稍微破坏我构建界面其余部分的方式。

感谢您的任何建议。

编辑:只是为了输入流代码的完整性

final HttpServletRequest request = (HttpServletRequest)ctx.getExternalContext().getRequest();
ServletInputStream stream = request.getInputStream();
int byt = stream.read(); // byt was -1

编辑编辑:很抱歉这篇长文章。现在我做了一个单独的servlet。即使 Firefug 说我传输了文件,我也得到一个空的输入流。怎么可能是空的呢?

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

boolean isMultipart = ServletFileUpload.isMultipartContent(req);
ServletInputStream stream= req.getInputStream();
int test = stream.read(); // test == -1

该 servlet 代码在单独的项目中运行良好,因此过滤器链中的 JSF/Seam servlet 之一必须删除数据。如果有人提示哪一个或如何检查。

i have a Backing Bean in which i read parameter which are not bound to a component. Seam offered that to read get parameter. I used

@RequestParameter private String param1;

this way he doesn't skip my action method on a validation error, like when i used
<param name="param1" value="#{myBean.param1}" />
, because i don't want to render HTML responses but reuse the business logic. Instead i grab the response stream and write the rendered response myself

FacesContext ctx = FacesContext.getCurrentInstance();

final HttpServletResponse resp = (HttpServletResponse)ctx.getExternalContext().getResponse();      
resp.getOutputStream().write(xml.getBytes());
resp.getOutputStream().flush();
resp.getOutputStream().close();

ctx.responseComplete(); // render response phase done

I tried to read the file from the HttpServletRequest inputStream but that was already empty. Is there a way to still get the "file" from JSF. I could use a separate servlet and handle it there, but that would break a little bit how i build the rest of the interface.

Thx for any advice.

Edit: Just for completeness the code for the input stream

final HttpServletRequest request = (HttpServletRequest)ctx.getExternalContext().getRequest();
ServletInputStream stream = request.getInputStream();
int byt = stream.read(); // byt was -1

Edit Edit: Sorry for the long post. Now i made a separate servlet. There i also get an empty input Stream even though Firefug said i transmitted the file. How can that be empty?

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

boolean isMultipart = ServletFileUpload.isMultipartContent(req);
ServletInputStream stream= req.getInputStream();
int test = stream.read(); // test == -1

That servlet code works fine in a separate project, so one of the JSF/Seam servlets in the filter chain has to remove the data. If someone has a hint which one or how to check.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雪碎 2024-11-16 02:08:19

SeamFilter 或者更准确地说 MultipartFilter 正在检查每个请求是否是 POST 和多部分请求。如果是这样,他们会将请求包装在 MultipartRequest 中,并将文件放入单独的参数映射中。

final HttpServletRequestWrapper request = (HttpServletRequestWrapper)FacesContext.getCurrentInstance().getExternalContext().getRequest();
final ServletRequest innerRequest = request.getRequest();
if(innerRequest instanceof MultipartRequest){
    byte[] imageData = ((MultipartRequest)innerRequest).getFileBytes("image");

The SeamFilter or more exactly the MultipartFilter was checking every request if it is a POST and multipart request. If so, they wrapped the request in a MultipartRequest and put the files in a separate parameter map.

final HttpServletRequestWrapper request = (HttpServletRequestWrapper)FacesContext.getCurrentInstance().getExternalContext().getRequest();
final ServletRequest innerRequest = request.getRequest();
if(innerRequest instanceof MultipartRequest){
    byte[] imageData = ((MultipartRequest)innerRequest).getFileBytes("image");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文