ZelelB
ZelelB

Reputation: 2000

how to get the Application root path

I'm trying to get the application root path, for ex "/myApp", but it is, unfortunately, not working..

How can I get it?

I have so far tried..

package redb.main.modules.sample.view.overview.columns;

import java.io.Serializable;
import java.text.MessageFormat;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import redb.main.core.model.Dev;
import redb.main.core.model.Sam;
import redb.main.core.model.SampleVisitor;
import redb.main.core.model.Sys;

public class AnalLinkResolver implements SampleVisitor, Serializable, ServletContextListener {

    private static final long serialVersionUID = 1L;
    private final String DANAL= "/anal/over?sRNo={0,number,#}";
    private final String SANAL= "/anal/overv?sRNo=S{0,number,#}";


    private String linkPattern;
    private String contextPath;
    private Integer id;

    public AnalLinkResolver() {

    }

    @Override
    public void visit(Sys s) {
        linkPattern = contextPath + SANAL;
        id = s.getRNo();
        System.out.println("contextpathis: " + contextPath);

    }

    @Override
    public void visit(Dev d) {
        linkPattern = contextPath + DANAL;
        id = d.getRNo();
    }

    public String resolveForSam(Sam sam) {
        sam.accept(this);
        return MessageFormat.format(linkPattern, id);
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
            contextPath = event.getServletContext().getContextPath().toString();
            System.out.println("PATHNY: " + contextPath);
     }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        //contextPath = event.getServletContext().getContextPath().toString();
        //System.out.println("PATHNY: " + contextPath);
    }

}

and added this in the web.xml:

<listener>
        <listener-class>
            redb.main.modules.sample.view.overview.columns.AnalLinkResolver</listener-class>
</listener>

I'm getting the contextPath printed from the contextInitialized method, but not from the visit method.. there i get null.. what should i make to get the root path? for ex "/myApp" ?

PS: I'm using springmvc & wicket in my application

Upvotes: 0

Views: 1777

Answers (1)

TOUDIdel
TOUDIdel

Reputation: 1304

WebApplication.get().getServletContext().getContextPath()

Upvotes: 1

Related Questions