首页 > 代码库 > 如何在类中获取request,和网站路径

如何在类中获取request,和网站路径

@RequestMapping(value = "http://www.mamicode.com/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>

  效果:

技术分享

 

如何在类中获取request,和网站路径