xhtml调用servlet不返回响应,不调用servlet
问题描述:
我想从窗体调用一个servlet。我在Web应用程序中调用xhtml来调用servlet,但是servlet的响应未显示。按下提交按钮后显示原始xhtml。我尝试了不同的servlet响应方式,但每次都得到相同的结果。xhtml调用servlet不返回响应,不调用servlet
XHTML:在Web应用程序
<?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>
的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?
答
问题是我在facelets中使用模板,同时也使用表单调用servlet。
xmlns:ui="http://java.sun.com/jsf/facelets"
<ui:composition template="/templateForm.xhtml">
<form action="someServlet" method="post">
模板有它自己的H:表单标签,所以当我添加另一种形式调用servlet,我结束了这是不允许的表单中的表单。所以这就是为什么我的servlet没有被调用。我通过从模板中移除h:form标签来修复它,现在我的servlet正在被调用。
也一般从facelets调用servlet不是一个好主意 – user840930 2012-04-20 08:28:35