JSF接收POST参数 [英] JSF to receive POST parameters

查看:114
本文介绍了JSF接收POST参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我正在使用servlet从HTML页面接收POST,然后重定向到我的JSF页面.

Today I'm using a servlet to receive a POST from a HTML page and then redirecting to my JSF page.

这是我实际的Servlet:

This is my actual Servlet:

   public class CommInServlet extends HttpServlet {

   private String reportKey;

      protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         req.getSession(true).setAttribute("reportKey", req.getParameter("reportkey"));
         req.getRequestDispatcher("main.xhtml").forward(req, resp);
       }

    }

HTML帖子页面:

<html>
<head />
<body>
<form action="Filtro" method="post">
<input type="text" size="120" name="reportkey" value="XXXXXXXXXXXX" />
<input type="submit" value="doPost" />
</form>
</body>
</html>

是否可以直接发布到我的JSF页面(ManagedBean)?如何? 我想用Servlet代替某些东西……更好.

Is it possible to post directly to my JSF page (ManagedBean)? How? I want to replace the Servlet for something... better.

推荐答案

可以.无论如何,大多数JSF请求都是POST,因此,如果您打算使用JSF页面的路径来处理POST请求,则可以在该页面支持的托管bean中获取参数,或者获取页面本身内的参数.

Sure you can. Most JSF requests are POSTs anyway, so if you use the path to the JSF page you're intending to handle the POST request, you can then get the parameter within a managed bean that is backed by that page OR get the parameter within the page itself.

在托管bean中:

     @PostConstruct
      public void initMyBean(){
      /**This map contains all the params you submitted from the html form */
      Map<String,String> requestParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
      requestParams.get("reportKey");
                       }

OR 托管Bean中有

     @ManagedProperty(value="#{param.reportKey}")
     String reportKey;
     //getter and setter of course!

@PostConstruct注释的方法将在实例化托管bean之后执行.上面的内容使您可以在托管bean中进行访问.

The method you've annotated with @PostConstruct will be executed after the managed bean has been instantiated. The above will give you access within your managed bean.

但是,如果您首先需要在页面中使用该值,则可以在页面中使用它(最好在顶部)

If you need the value within your page first however, you can have this in your page (preferably at the top)

     <f:metadata>
      <f:viewParam name="reportKey" value="#{backingBean.reportKey}" required="true"/>
     </f:metadata>

注意如何在视图中对参数执行验证.很酷的功能.

Notice how you can perform validations on the parameter from within your view. Pretty cool feature.

只需确保将html表单action属性设置为JSF视图的路径即可.

Just be sure and set your html form action attribute to the path of the JSF view.

这篇关于JSF接收POST参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆