使用filter代替jsp获取参数

1、编写一个filter

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;
    }
}

 

2.在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>

 

3、创建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);
        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);
        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;
    }
}

 

4、在页面中使用:

<!DOCTYPE html>
<html lang="en" allowtransparency="true">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>vcn</title>
    <link rel="stylesheet" href="${basePath}/static/css/reset.css" />
    <link rel="stylesheet" href="${basePath}/pages/vcn/css/vcn.css" />
</head>

<body>
    <div class="to_video2 _one">
        <iframe src="${basePath}/pages/vcn/vcnbutton.html?parameter={num:0}" frameBorder="0" marginHeight="0" marginWidth="0">good</iframe>
    </div>

    <div class="to_video2 _two">
        <iframe src="${basePath}/pages/vcn/vcnbutton.html?parameter={num:1}" frameBorder="0" marginHeight="0" marginWidth="0">good</iframe>
    </div>

    <div class="videoroad-box2" >
        <object id="vcnocx" width="100%" height="100%" CLASSID="CLSID:3556A474-8B23-496F-9E5D-38F7B74654F4"></object>
    </div>

</body>
<script type="text/javascript">
    var basePath = '${basePath}';
    var rootPath = '${rootPath}';
    var parameter = ${ parameter };
    var page = {
        "init": function () {
            loginVCN(vcn2road);
        }
    };
</script>
<script src='${basePath}/plugins/jquery/jquery-3.3.1.min.js'></script>
<script src="${rootPath}/libs/resafety/build/resafety-1.19.5.js"></script>
<script src="${basePath}/pages/vcn/js/vcn.js"></script>

</html>

 

posted @ 2020-04-12 17:06  图图小淘气_real  阅读(269)  评论(0编辑  收藏  举报