版权声明:Firewine 的博客,想要转载请标明出处 https://blog.csdn.net/xyjworkgame/article/details/88047750
在ssm开发中,总是发现url路径和静态资源的路径都需要加上项目名,所以想到提出来方法,把他放在application范围中,就可以了,,不用每个jsp页面都要加
public class ServerStartupListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
// 将web应用名称(路径)保存到application范围中
ServletContext application = sce.getServletContext();
String path = application.getContextPath();
application.setAttribute("APP_PATH", path);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
}
然后在web.xml里添加listener
<listener>
<listener-class>com.study.diary.web.ServerStartupListener</listener-class>
</listener>