【问题标题】:Calling a servlet /logout from a template.xhtml从 template.xhtml 调用 servlet /logout
【发布时间】:2018-12-30 02:54:48
【问题描述】:

我想从位于文件夹模板中的 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>

模板.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");
    }
}

【问题讨论】:

    标签: jsf web-applications primefaces


    【解决方案1】:

    我认为你想做的几件事。

    创建一个普通的 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"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多