首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从template.xhtml调用servlet /logout

从template.xhtml调用servlet /logout
EN

Stack Overflow用户
提问于 2018-07-22 21:18:24
回答 1查看 653关注 0票数 1

我想从位于templates文件夹中的templateHeader.xhtml调用servlet /logout,如下所示:

代码语言:javascript
复制
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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
@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");
    }
}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-24 07:07:57

我觉得你有几件事想做。

创建一个普通的JSF控制器(例如LogoutController)方法,如下所示...

代码语言:javascript
复制
public String logout() {
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    session.invalidate();
    return "/login.xhtml?faces-redirect=true";
}

将菜单项更改为..

代码语言:javascript
复制
<p:menuitem value="Quitter" action="${logoutController.logout}" icon="fa fa-sign-out"/>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51465506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档