简体   繁体   English

如何获得应用程序的根路径

[英]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.. 我正在尝试获取应用程序的根路径,例如“ / myApp”,但不幸的是,它无法正常工作。

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: 并将其添加到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? 我正在从contextInitialized方法中打印contextPath,但不是从visit方法中打印出来。.在那里我得到null ..我应该怎么做才能得到根路径? for ex "/myApp" ? 对于前“ / myApp”?

PS: I'm using springmvc & wicket in my application PS:我在应用程序中使用springmvc和wicket

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM