【发布时间】: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