@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>
效果: