如何使用带有提交按钮的表单从facelets文件中正确调用servlet?是否需要特定的表单?
<form>
,而不是JSF的<h:form>
。JSF的<h:form>
默认发送POST请求到当前视图ID的URL,并默认调用FacesServlet
。它不允许您更改表单操作URL或方法。一个纯HTML的<form>
允许您指定不同的URL,并且必要时也可以指定方法。<form action="http://google.com/search">
<input type="text" name="q" />
<input type="submit" />
</form>
请注意,您不需要使用JSF组件来进行输入/按钮操作。可以使用<h:inputText>等标签,但是关联后端bean中的值不会被设置。因此,JSF组件开销是不必要的。/foo/*
的servlet发送POST请求,并且需要发送名称为bar
的请求参数时,需要按照以下方式创建表单:<form action="#{request.contextPath}/foo" method="post">
<input type="text" name="bar" />
<input type="submit" />
</form>
这样就会调用servlet的doPost()
方法:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bar = request.getParameter("bar");
// ...
}
你可以通过以下方式从jsf中进行调用:
<h:outputText value="Download" />
<h:outputLink value="#{request.contextPath}/files" id="btnDownload1" styleClass="redButton">
<h:outputText value="FILESDOWNLOAD" />
</h:outputLink>
</h:panelGrid>
然后在web.xml中:
<servlet>
<servlet-name>files</servlet-name>
<servlet-class>com.Download</servlet-class>