2017-06-13 1 views
0

In web.xml ich habeWie kontext-param mit jstl drucken?

<context-param> 
     <param-name>email</param-name> 
     <param-value>test</param-value> 
    </context-param> 

In jsp habe ich

<c:out value= "MUST PRINT EMAIL"> 
    </c:out> 
    <c:out value= "${applicationScope.email}"> 
    </c:out> 

Aber diese druckt nur MUST PRINT EMAIL. Der E-Mail-Testwert wird nicht gedruckt. Warum? Wie bekomme ich Kontext-Parameter?

Antwort

1

Ich glaube, Sie dies nicht direkt tun können, Sie alle Attribute in Anwendungsbereich dieses Code-Dump kann

${pageScope}<br> ${requestScope}<br> ${sessionScope} <br> ${applicationScope} 

oder ausführlichere

<% 
    Enumeration attrNames=request.getServletContext().getAttributeNames(); 
    while(attrNames.hasMoreElements()){ 
     String ctxAttribute=(String) attrNames.nextElement(); 
     out.print("<br> Attribute Name --> "+ctxAttribute+", has value Value --> "+request.getServletContext().getAttribute(ctxAttribute)); 
    } 

out.println("<br><br>"); 

    attrNames=application.getAttributeNames(); 
    while(attrNames.hasMoreElements()){ 
     String ctxAttribute=(String) attrNames.nextElement(); 
     out.println("<BR> Attribute Name --> "+ctxAttribute+", has value Value --> "+application.getAttribute(ctxAttribute)); 
    } 
%> 

Wenn Sie diesen Parameter so erhalten möchten eine Art und Weise, können Sie dieses Attribut auf Anwendungsbereich setzen, wenn dann Kontext initialisiert:

import javax.servlet.ServletContextEvent; 
import javax.servlet.ServletContextListener; 
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 

public class MyContextListener implements ServletContextListener { 
    private static final Log logger = LogFactory.getLog(MyContextListener.class); 


    @Override 
    public void contextInitialized(ServletContextEvent servletContextEvent) { 
     String email= servletContextEvent.getServletContext().getInitParameter("email"); 
     logger.info("Use email:" + email); 
     servletContextEvent.getServletContext().setAttribute("email", email+"==setbylistener"); 
    } 

    @Override 
    public void contextDestroyed(ServletContextEvent servletContextEvent) { 

    } 
} 

und vergessen Sie nicht, es zu konfigurieren in Ihrem web.xml

<context-param> 
     <param-name>email</param-name> 
     <param-value>test123</param-value> 
    </context-param> 

    <listener> 
     <listener-class>MyContextListener</listener-class> 
    </listener> 

Hoffnung, das hilft.