使用ModelAndView和过滤器实现获取HTML请求的参数
需求:获取HTML的请求参数
类似:
实现思路:
1、利用过滤器拦截所有HTML请求
2、利用ModelAndView把参数保存起来
3、在HTML页面使用${key}把参数获取到
一、利用过滤器拦截所有HTML请求
web.xml配置:
<!-- html --> <filter> <filter-name>html</filter-name> <filter-class>core.filter.HTML</filter-class> </filter> <filter-mapping> <filter-name>html</filter-name> <url-pattern>*.html</url-pattern> </filter-mapping>
过滤器:
package core.filter; import java.io.IOException; import java.util.Enumeration; 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 javax.servlet.http.HttpServletResponse; /** * 过滤所有HTML请求 * */ public class HTML implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; req.setCharacterEncoding("UTF-8"); res.setCharacterEncoding("UTF-8"); res.setHeader( "Access-Control-Allow-Origin", "*" ); String uri = backslashToSingle( req.getRequestURI() ); String context = req.getContextPath(); Enumeration enu = request.getParameterNames(); StringBuffer params = new StringBuffer(); while(enu.hasMoreElements()){ String paraName = (String)enu.nextElement(); params.append("&"+paraName+"="+request.getParameter(paraName)); } //重定向 if(uri.indexOf(".") != -1) { res.sendRedirect(context + "/html/go?pagepath=" + uri.substring(context.length()+1,uri.indexOf(".")) + params); return; } chain.doFilter(req, res); } @Override public void init(FilterConfig arg0) throws ServletException { } public void destroy() { } /** * 将路径中的连续斜杠转成单个的 * @param strURL * @return */ public static String backslashToSingle(String strURL){ while ( strURL.indexOf( "//" ) != -1 ) { strURL = strURL.replace( "//", "/" ); } if( strURL.indexOf( ":" ) != -1 ) { strURL = strURL.replace( ":", ":/" ); } return strURL; } }
HTML的controller类:
package module.api.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import core.common.springmvc.BasicController; /** * 往html界面中注入常用el属性 */ @Controller @RequestMapping("/html") public class HTMLController extends BasicController { @RequestMapping("/go") @ResponseBody public ModelAndView go(HttpServletRequest request, String pagepath, String parameter) { ModelAndView modelAndView = new ModelAndView(); String rootPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort(); String basePath = rootPath + request.getContextPath() + "/"; rootPath += "/"; String title = request.getParameter("name"); modelAndView.setViewName(pagepath); modelAndView.addObject("rootPath", rootPath); modelAndView.addObject("basePath", basePath); // modelAndView.addObject("basePath", "http://183.203.223.224:82/taiyuan/"); Object object = null; try { object = JSON.parse(parameter); if (!(object instanceof JSONObject)) { parameter = "{}"; } } catch (Exception e) { parameter = "{}"; } modelAndView.addObject("parameter", parameter); modelAndView.addObject("title", title == null ? "" : title); return modelAndView; } @RequestMapping("/readdata/go") @ResponseBody public ModelAndView readDataGo(HttpServletRequest request, String pagepath, String parameter, String interfacepath, String interfaceparameter) { ModelAndView modelAndView = new ModelAndView(); String rootPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort(); String basePath = rootPath + request.getContextPath() + "/"; rootPath += "/"; String title = request.getParameter("name"); modelAndView.setViewName(pagepath); modelAndView.addObject("rootPath", rootPath); modelAndView.addObject("basePath", basePath); // modelAndView.addObject("basePath", "http://183.203.223.224:82/taiyuan/"); Object object = null; try { object = JSON.parse(parameter); if (!(object instanceof JSONObject)) { parameter = "{}"; } } catch (Exception e) { parameter = "{}"; } modelAndView.addObject("parameter", parameter); modelAndView.addObject("title", title == null ? "" : title); return modelAndView; } }
父类BasicController:
package core.common.springmvc; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import util.ResponseUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public abstract class BasicController { protected Map<String, Object> result = new HashMap<String, Object>(); protected boolean flag = false; protected String msg = ""; protected String businessCode=""; public String basePath() { HttpServletRequest request = request(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); return basePath; } public String rootPath() { HttpServletRequest request = request(); String rootPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort(); return rootPath; } public String cdnPath() { HttpServletRequest request = request(); String cdnPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/cdn"; return cdnPath; } public void write(Integer total,List list,HttpServletResponse response) throws Exception{ JSONObject result = new JSONObject(); JSONArray jsonArray = new JSONArray( list ); result.put("rows", jsonArray); result.put("total", total); ResponseUtil.write(response, result); } @InitBinder public void initBinder(WebDataBinder binder) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(true); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); } public String findSessionId() { HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); String contextPath = request.getContextPath(); return contextPath+request.getSession().getId(); } public HttpServletRequest request() { HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); return request; } }
二、利用ModelAndView把参数保存起来
三、在HTML页面使用${key}把参数获取到
var basePath = '${basePath}'; var rootPath = '${rootPath}'; var parameter = ${ parameter };
实现获取参数:
参考文章:
https://www.cnblogs.com/Sunnor/p/6130380.html
https://blog.csdn.net/ITBigGod/article/details/79685610
https://blog.csdn.net/qq_29569183/article/details/79282673
https://blog.csdn.net/qq_39592670/article/details/82466907
https://blog.csdn.net/qq_41995919/article/details/102944960