【问题标题】:Spring how to merge an external html page with generated contentSpring如何将外部html页面与生成的内容合并
【发布时间】:2013-06-04 08:29:29
【问题描述】:

您好,我目前正在开发一个 spring (3.2.x) 应用程序,我必须将我的内容插入到由 id 指定的特定点的给定页面中。

这就是我目前正在做的事情:

@RequestMapping(value = "/{part}", method = RequestMethod.POST, produces="text/html")
@ResponseBody
public String enterModul(HttpServletRequest request, @PathVariable String part, @ModelAttribute Body body){
    //body handling omitted
    //getting the external html
    String frame = restTemplate.getForObject("...externalUrl", String.class);

    //getting my content
    String uri = request.getRequestURL().toString();
    String content = restTemplate.getForObject(uri, String.class);

    // merge frame and content
    String completeView = this.mergeFrameAndContent(frame, content);
    return completeView;
}

@RequestMapping(value = "/{part}", method = RequestMethod.GET, produces="text/html")
@ResponseBody
public ModelAndView getInitialContentForPart(@PathVariable String part) {
    //irrelevant code/model creation ommited
    //just using InternalResourceViewResolver so nothing fancy here
    ModelAndView view = new ModelAndView(part, "model", model);
    return view;
}

private String mergeFrameAndContent(String frame, String content) {
            //id identifies position
    String view = frame.replace("id", content);
    return view;
}

但不知何故,这样做感觉不对。有更好的解决方案吗?我试着用瓷砖 3 来做,但没有用。

【问题讨论】:

  • 您只是想插入文本还是更改代码?
  • 我想将内容html插入到外部html中
  • 好吧,那你为什么不使用 jQuery 呢?
  • 看看这对你有帮助吗? stackoverflow.com/questions/4967629/…
  • 我无法控制要在其中插入 html 的外部页面。你会怎么用jquery做呢?在我的内容 html 中使用 jquery 是没有问题的,但我必须以某种方式将它注入外部 html,不是吗?

标签: java spring spring-mvc template-engine


【解决方案1】:

由于您谈论的是具有许多应用程序的分布式架构,Edge Side Includes(ESI,请参阅http://en.wikipedia.org/wiki/Edge_Side_Includes)可能是您正在寻找的。你可以使用例如Varnish 反向代理(参见https://www.varnish-cache.org/trac/wiki/ESIfeatures)来处理它们。

【讨论】:

    【解决方案2】:

    我通过使用过滤器来操纵响应找到了更好的解决方案。通过这样做,我保存了一个内部请求,它是一个可重用的解决方案:

    public class FrameFilter extends GenericFilterBean { 
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            ServletResponse newResponse = response;
    
            if (request instanceof HttpServletRequest) {
                newResponse = new CharResponseWrapper((HttpServletResponse) response);
            }
    
            chain.doFilter(request, newResponse);
    
            if (newResponse instanceof CharResponseWrapper) {
               String modulContent = newResponse.toString();
               if (modulContent != null) {
                    RestTemplate restTemplate = new RestTemplate();
                    String frame = restTemplate.getForObject("FRAMEURL", String.class);
                    String completeView = this.mergeFrameAndContent(frame, modulContent);
                    response.getWriter().write(completeView);
                }
            }
        }
    }
    

    CharResponseWrapper 取自此示例:Example

    【讨论】:

      猜你喜欢
      • 2018-12-29
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      • 2014-08-21
      • 1970-01-01
      相关资源
      最近更新 更多