-1

What is the best way to read HTML content from a text file and display the content in JSP? I have placed the text file in the resource folder using spring mvc 3. Normally I do this kind of stuff with Apache commons in struts but believe spring must have provided some utility for this.

I am thinking about reading it in a tag file. What utility should i use for that?

Matthew Spencer
  • 2,265
  • 1
  • 23
  • 28
user22197
  • 129
  • 1
  • 2
  • 10
  • This is what I was trying to acheive. [Stackoverflow] http://stackoverflow.com/questions/14022839/how-do-i-load-a-resource-and-use-its-contents-as-a-string-in-spring. – user22197 Nov 09 '14 at 16:06

2 Answers2

0

Ignoring the question of why you would want to do this. The return value from a Spring MVC controller method is usually used to resolve a view. The resolved view then becomes the response body. However, you can use the @ResponseBody annotation to make the raw return value the response body.

Romski
  • 1,912
  • 1
  • 12
  • 27
0

You can do it in the @RequestMapping method. The Key is the return type ModelAndView.

You read your text file and get the html that you need, after this, adding the html to the model and then return the a new ModelAndView Object.

Here's a sample:

@RequestMapping(value = "siteWithResHtml", method = RequestMethod.GET)
public ModelAndView loadSiteWithResHtml(Model model)
{
  String resourceHtml;
  // do your stuff for reading file and assign it to the String
  model.addAttribute("resource_Html", resourceHtml);
  return new ModelAndView("yourJSP", "model", model);
}

and in the jsp you can read the value from the model that you forwarded to the jsp, like this:

<div>${model.resource_Html}</div>

please take notice that the names are match.

Manu Zi
  • 2,300
  • 6
  • 33
  • 67