我想从位于templates文件夹中的templateHeader.xhtml调用servlet /logout,如下所示:
webapp
|-- form
| |-- form.xhtml
|-- WEB-INF
| |-- templates
| | |-- template.xhtml
| | |-- templateFooter.xhtml
| | |-- templateHeader.xhtml
|-- resources
|-- admin.xhtml
|-- login.xhtml
:
问题是我不知道如何调用servet,因为根据您所在的页面,到servlet的路径是不同的。我正在寻找#{request.contextPath}/myPage的等价物,但用于servlet。出于好奇,如果我想从bean Login调用一个方法myMethod(),我该怎么做呢?
我关注过这个Kill session and redirect to login page on click of logout button,但我觉得我用错了。注意,我还尝试将method="post“添加到菜单项中。另请注意,下面代码的第一个菜单项正在运行。
templateHeader.xhtml
<ui:composition
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<p:layoutUnit id="top" position="north" size="50">
<h:form>
<p:menubar>
<p:menuitem value="Satisfact'IT" url="#{request.contextPath}/admin.xhtml" icon="fa fa-home" />
<p:menuitem value="Quitter" action="${pageContext.request.contextPath}/logout" icon="fa fa-sign-out"/>
</p:menubar>
</h:form>
</p:layoutUnit>
</ui:composition>
template.xhtml
<?xml version='1.0' encoding='ISO-8859-1' ?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
>
<h:head>
</h:head>
<h:body>
<p:layout fullPage="true" >
<ui:insert name="header" >
<ui:include src="commonHeader.xhtml" />
</ui:insert>
<ui:insert name="footer" >
<ui:include src="commonFooter.xhtml" />
</ui:insert>
</p:layout>
</h:body>
</html>
最后是注销servlet
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getSession().invalidate();
response.sendRedirect(request.getContextPath() + "/login.xhtml");
}
}
发布于 2018-07-24 07:07:57
我觉得你有几件事想做。
创建一个普通的JSF控制器(例如LogoutController)方法,如下所示...
public String logout() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
session.invalidate();
return "/login.xhtml?faces-redirect=true";
}
将菜单项更改为..
<p:menuitem value="Quitter" action="${logoutController.logout}" icon="fa fa-sign-out"/>
https://stackoverflow.com/questions/51465506
复制相似问题