You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

5 years ago
package ${basePackage}.frame.utils;
6 years ago
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
5 years ago
6 years ago
import javax.servlet.http.HttpServletRequest;
import java.util.Locale;
/**
5 years ago
* Url
6 years ago
*
* @author wangbing
* @version 0.0.1
* @since 2017-01-01
*/
@Component
5 years ago
public class UrlUtil {
6 years ago
5 years ago
public String getUrl(String url) {
if (url == null) {
6 years ago
return "";
}
5 years ago
if (!url.startsWith("/")) {
return "/" + url;
6 years ago
}
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// 协议
String scheme = request.getScheme();
// 域名
String serverName = request.getServerName();
// 端口
int serverPort = request.getServerPort();
// 上下文路径
String contextPath = request.getContextPath();
5 years ago
return String.format(Locale.CHINA, "%s://%s:%d%s%s", scheme, serverName, serverPort, contextPath, url);
6 years ago
}
}

Powered by TurnKey Linux.