0

嗨,我目前正在开发一个 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 来做,但没有用。

4

2 回答 2

0

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

于 2013-06-04T21:26:45.943 回答
0

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

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 取自此示例:示例

于 2013-06-26T10:58:10.110 回答