springmvc 路径和资源配置

api-request

请求: http://localhost:8080/pscard/list

private void logRequest(HttpServletRequest request) {
        out.println("Protocol: " + request.getProtocol());
        out.println("Scheme: " + request.getScheme());
        out.println("Server Name: " + request.getServerName() );
        out.println("Server Port: " + request.getServerPort());
        out.println("Protocol: " + request.getProtocol());
        out.println("Server Info: " + request.getServletContext().getServerInfo());
        out.println("Remote Addr: " + request.getRemoteAddr());
        out.println("Remote Host: " + request.getRemoteHost());
        out.println("Character Encoding: " + request.getCharacterEncoding());
        out.println("Content Length: " + request.getContentLength());
        out.println("Content Type: "+ request.getContentType());
        out.println("Auth Type: " + request.getAuthType());
        out.println("HTTP Method: " + request.getMethod());
        out.println("Path Info: " + request.getPathInfo());
        out.println("Path Trans: " + request.getPathTranslated());
        out.println("Query String: " + request.getQueryString());
        out.println("Remote User: " + request.getRemoteUser());
        out.println("Session Id: " + request.getRequestedSessionId());
        out.println("Request URI: " + request.getRequestURI());
        out.println("Servlet Path: " + request.getServletPath());
        out.println("Accept: " + request.getHeader("Accept"));
        out.println("Host: " + request.getHeader("Host"));
        out.println("Referer : " + request.getHeader("Referer"));
        out.println("Accept-Language : " + request.getHeader("Accept-Language"));
        out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding"));
        out.println("User-Agent : " + request.getHeader("User-Agent"));
        out.println("Connection : " + request.getHeader("Connection"));
        out.println("Cookie : " + request.getHeader("Cookie"));
        out.println("Created : " + request.getSession().getCreationTime());
        out.println("LastAccessed : " + request.getSession().getLastAccessedTime());
    }

结果:
Protocol: HTTP/1.1
Scheme: http
Server Name: localhost
Server Port: 8080
Protocol: HTTP/1.1
Server Info: Apache Tomcat/8.5.15
Remote Addr: 0:0:0:0:0:0:0:1
Remote Host: 0:0:0:0:0:0:0:1
Character Encoding: UTF-8
Content Length: -1
Content Type: null
Auth Type: null
HTTP Method: GET
Path Info: null
Path Trans: null
Query String: null
Remote User: null
Session Id: null
Request URI: /pscard/list
Servlet Path: /pscard/list
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,q=0.8
Host: localhost:8080
Referer : null
Accept-Language : zh-CN,zh;q=0.8,en;q=0.6
Accept-Encoding : gzip, deflate
User-Agent : Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.1.2716.203 Safari/537.36
Connection : keep-alive
Cookie : __utma=111872281.231241494.1494552094.1494552094.1494559952.2; __utmz=111872281.1494552094.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmv=111872281.|1=Treatment=PE=1
Created : 1500516164533
LastAccessed : 1500516164533

api -servletContext

添加servlet监听器.因为springmvc其实全局就只有一个dispatchservlet,其生命周期几乎等同于application的生命周期,所以其api获取的数据可以直接变成静态变量,提供给工具类使用

springboot中添加servlet生命周期监听器:

@WebListener
public  class MyListener implements ServletContextListener {

    private ServletContext context = null;

    /* 这个方法在Web应用服务被移除,没有能力再接受请求的时候被调用。
     */
    public void contextDestroyed(ServletContextEvent event){
        //Output a simple message to the server's console
        System.out.println("The Simple Web App. Has Been Removed");
        this.context = null;

    }

    // 这个方法在Web应用服务做好接受请求的时候被调用。
    public void contextInitialized(ServletContextEvent event){
        this.context = event.getServletContext();
        ContextPathUtil.setContext(this.context);
        context.setAttribute("basePath","");

         System.out.println("The Simple Web App. Is Ready");
        org.apache.ibatis.logging.LogFactory.useSlf4jLogging();

    }
}

ServletContext的api

private void logServelet(ServletContext context) {
        log("getContextPath:"+context.getContextPath());
        log("getServerInfo:"+context.getServerInfo());
        log("getRealPath(/):"+context.getRealPath("/"));
        //log("getRealPath(/):"+context.getres("/"));
    }


getContextPath:
getServerInfo:Apache Tomcat/8.5.15
getRealPath(/):C:\Users\Administrator\AppData\Local\Temp\tomcat-docbase.608303050980735220.8080\

如何获取一个本服务器中一个静态资源的真实文件路径?todo

获取本项目的绝对磁盘路径

添加启动监听,拿到静态数据
http://blog.csdn.net/mytt_10566/article/details/58599243

获取本项目的完整url根路径(域名+项目名)

没有静态获取方式,都需要从request中获取

url和本地路径的映射

  • 项目内静态资源的映射
  • 上传下载时,url与项目外绝对路径的映射

http://blog.csdn.net/yansong_8686/article/details/47356499

你可能感兴趣的:(springmvc 路径和资源配置)