我正在尝试从表单中调用servlet。我在一个Web应用程序中有一个xhtml调用servlet但是没有显示来自servlet的响应。按下提交按钮后会显示原始xhtml。我为servlet响应尝试了不同的替代方案,但每次都得到相同的结果。
XHTML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:ejstreegrid="https://unfccc.int/nais/ejstreegrid"
xmlns:grid="http://java.sun.com/jsf/composite/gridcomp"
xmlns:nais="http://java.sun.com/jsf/composite/naiscomp">
<body>
<ui:composition template="/templateForm.xhtml">
<ui:define name="title">Some title</ui:define>
<ui:param name="currentPage" value="somepage.xhtml" />
<ui:define name="body">
<h2>the name to be added</h2><br/><br/>
<form action="someServlet" method="post">
<input type="text" name="someName" />
<input type="submit" />
</form>
</ui:define>
</ui:composition>
</body>
</html>
调用Web应用程序中的servlet:
package netgui.servlet;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class someServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public someServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
private void processRequest(HttpServletRequest request,
HttpServletResponse response) throws IOException {
try {
response.getWriter().write("some response");
} catch (Exception e) {
logger.error(e.getMessage());
e.printStackTrace();
response.getWriter().println("Error: " + e.getMessage());
}}}
我还有一个调用xhtml文件的menu.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<body>
<script type="text/javascript">
$(document).ready(function() {
$("#btn").click(function() {
$("#upload").click();
return false;
});
});
</script>
<ui:composition>
<div id="navigation_bar">
<ul id="topbarleft">
<c:choose>
<c:when test="${currentPage=='somepage.xhtml'}">
<li><b>Other (Please Specify) Two</b></li>
</c:when>
<c:otherwise>
<li><h:outputLink value="somepage.jsf">
<h:outputText value="some page" />
</h:outputLink></li>
</c:otherwise>
</c:choose>
</ul>
</div>
</ui:composition>
</body>
</html>
任何想法都可能出错?是否有一些特殊格式用于在jsf中向servlet提交表单?与facelets?
答案 0 :(得分:0)
问题是我在facelets中使用模板,同时还使用表单调用servlet。
xmlns:ui="http://java.sun.com/jsf/facelets"
<ui:composition template="/templateForm.xhtml">
<form action="someServlet" method="post">
模板有自己的h:form标签,所以当我添加另一个表单来调用servlet时,我最终会在表单中找到一个不允许的表单。这就是为什么我的servlet没有被调用的原因。我通过从模板中删除h:form标签来修复它,现在正在调用我的servlet。