zoukankan      html  css  js  c++  java
  • 如何在类中获取request,和网站路径

    @RequestMapping(value = "res/testjs")
    	public ModelAndView testjs( ModelMap model ) throws Exception {
    		String path = resService.getRequestPath();
    		model.put("path", path);
    		return new ModelAndView("res/testjs");
    	}
    

      上面是调用:

    下面是方法:

    import javax.servlet.http.HttpServletRequest;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes; 
    
    public String getRequestPath() {
    		HttpServletRequest request  = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    		String path =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/"+request.getRequestURL()+"/";  
    		return path;
    	}
    

      还有web.xml配置监听:

    <listener>
            <description>springrequestListener </description>
            <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
         </listener>
    

      效果:

  • 相关阅读:
    2004选拔赛 最小值
    [JSOI2008]最大数maxnumber
    HDOJ 2896 病毒侵袭
    POJ 2243 Knight Moves
    HDOJ Is It A Tree?
    MST 小希的迷宫
    PKU 3278 Catch That Cow
    POJ 2488 A Knight's Journey
    [SCOI2006]整数划分
    COJ 1259: 跳跳
  • 原文地址:https://www.cnblogs.com/hoge/p/7004012.html
Copyright © 2011-2022 走看看