html页面调用BasePath项目路径

BasePath项目路径,是特别游泳的类,放到request里面,在html页面就可以直接用
//放入basePath,供html页面调用
request.setAttribute(“basePath”, BasePath.getBasePath(request));
//html页面用freemarker去调用
${basePath}xxxx/xxx去调用,

例如我请求http://127.0.0.1:8080/user/findUser,只要html页面写${basePath}user/findUser即可。

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

public class BasePath {
    protected static String contextPath = null;
    protected static String basePath = null; 
    protected static String realPath = null;

    public static String getBasePath(HttpServletRequest request) {
        contextPath = request.getContextPath();
        basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+contextPath+"/";
        return basePath;
    }

    public static String getRealPath(HttpServletRequest request, String path) {
        ServletContext context = request.getSession().getServletContext();
        realPath = context.getRealPath(path);
        realPath = context.getRealPath(path)+"\\";
        return realPath;
    }

    public static String getMyRealPath(HttpServletRequest request, String path) {
        ServletContext context = request.getSession().getServletContext();
        realPath = context.getRealPath(path);
        realPath = context.getRealPath(path);
        return realPath;
    }
}

在spring-boot 2.0下面调用可以使用annotation配置,不然得在web.xml里面配置

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.filters.RemoteIpFilter;
import org.microservice.tcbj.yytsg.checksys.util.BasePath;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class ConfigurationFilter{

    @Bean
    public RemoteIpFilter remoteIpFilter() {
        return new RemoteIpFilter();
    }
    @Bean
    public FilterRegistrationBean testFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new MyFilter());//添加过滤器
        registration.addUrlPatterns("/*");//设置过滤路径,/*所有路径
        registration.addInitParameter("admin", "moshow");//添加默认参数
        registration.setName("MyFilter");//设置优先级
        registration.setOrder(1);//设置优先级
        return registration;
    }
    public class MyFilter implements Filter {
        public void destroy() {
        }

        public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain
                filterChain)
                        throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) srequest;
            //放入basePath,供html页面调用
            request.setAttribute("basePath", BasePath.getBasePath(request));
            //打印请求Url
            System.out.println("ConfigurationFilter->filter-url->" + request.getRequestURI());
            filterChain.doFilter(srequest, sresponse);
        }

        public void init(FilterConfig arg0) throws ServletException {
        }
    }

}

你可能感兴趣的:(Spring,SpringBoot2启示录)