从Facelets调用servlet的正确方法? [英] Proper way to call servlet from Facelets?

查看:106
本文介绍了从Facelets调用servlet的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用带有提交按钮的表单从facelets文件中调用servlet的正确方法是什么?是否需要特定的表格?

What is the proper way to call a servlet from a facelets file using a form with submit button? Is there a particular form required?

推荐答案

只需使用纯HTML <form>而不是JSF <h:form>. JSF <h:form>默认情况下将POST请求发送到当前视图ID的URL,并默认调用FacesServlet.它不允许您更改表单操作URL或方法.普通的HTML <form>允许您指定其他URL,并在必要时指定方法.

Just use a plain HTML <form> instead of a JSF <h:form>. The JSF <h:form> sends by default a POST request to the URL of the current view ID and invokes by default the FacesServlet. It does not allow you to change the form action URL or method. A plain HTML <form> allows you to specify a different URL and, if necessary, also the method.

以下启动示例向Google发送搜索请求:

The following kickoff example sends a search request to Google:

<form action="http://google.com/search">
    <input type="text" name="q" />
    <input type="submit" />
</form>

请注意,您也不需要将JSF组件用于输入/按钮.可以使用<h:inputText>等,但是不会在关联的后备bean中设置值.这样就不需要JSF组件开销了.

Note that you do not need to use JSF components for the inputs/buttons as well. It is possible to use <h:inputText> and so on, but the values won't be set in the associated backing bean. The JSF component overhead is then unnecessary.

例如,当您想向Servlet发送POST请求,该Servlet映射到/foo/*的URL模式,并且需要发送名称为bar的请求参数时,则需要创建形式如下:

When you want, for example, to send a POST request to a servlet which is mapped to a URL pattern of /foo/* and you need to send a request parameter with the name bar, then you need to create the form as follows:

<form action="#{request.contextPath}/foo" method="post">
    <input type="text" name="bar" />
    <input type="submit" />
</form>

这样,将调用servlet的doPost()方法:

This way the servlet's doPost() method will be invoked:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String bar = request.getParameter("bar");
    // ...
}

这篇关于从Facelets调用servlet的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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