我试图获取应用程序的根路径,对于ex" / myApp",但遗憾的是,它无法运行..
我怎样才能得到它?
我到目前为止试过..
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);
}
}
并在web.xml中添加:
<listener>
<listener-class>
redb.main.modules.sample.view.overview.columns.AnalLinkResolver</listener-class>
</listener>
我从contextInitialized方法打印了contextPath,但是没有从visit方法打印..我得到null .. 我应该怎么做才能获得根路径?对于ex&#34; / myApp&#34; ?
PS:我使用的是springmvc&amp;我的申请中的检票口
答案 0 :(得分:0)
WebApplication.get().getServletContext().getContextPath()