Including a document to JSP using Spring MVC - spring

I have a need of "showing" a word document in a jsp file, and when a user clicks on the file, it should get downloaded.
It is basically a Spring MVC application, the controller creates a word document and then returns the next view to be displayed to the client. This view file (jsp file) should have the word document.
The controller essentially just returns the view name:
#Controller
String handleRequest() {
// Logic for creating a word document.
return "nextView";
}
It is in this nextView.jsp the attachment should appear.
Now, how can we "attach" the generated word document into the jsp file?
Any thoughts in this?

Using an iframe with in the page, you can invoke one more requestMapping URI which has header content-type set to excel / word doc and that document can be displayed on fly. And after the iframe add a download link as shown in the below link.
Ref here : http://www.codejava.net/frameworks/spring/spring-mvc-with-excel-view-example-apache-poi-and-jexcelapi

Related

Using Spring Boot, Thymeleaf and Oracle: How to dynamically populate the submenus in a web site menu defined in a fragment?

I have a web site that consists of approximately 30 html thymeleaf templates. I have a home_navigation.html fragment that gets included in all templates as the header. This header defines a main navigational menu with popup submenus. The popup submenus under one of my main menus needs to be generated from my oracle database table.
Normally when passing data from a database into a Thymeleaf template I would put the code in the controller to call the java DAO and return a list of Link objects and then add that list in the controller to the model using .setAttribute. Then in the Thymeleaf template I would iterate through the "${List}" in a "th:each" outputting the "<a href..." for each Link object in the list. That is all fine and dandy.
I also can pass parameters into the fragment. So that isn't the problem BUT...
Since the main navigational menu is added as a header fragment into the beginning of every template then I would have to go into every defined controller and add code that would pull the list of Links and pass it into the template and then pass the list into the fragment from every page. that would be approximately 30 times!!!
How...using Spring Boot and Thymeleaf, does someone feed data into a fragment to populate a menu dynamically from a database that is then added as a header fragment into every page/template on the site?
Is there a way to create a controller for the fragment and somehow have every page call the controller for the fragment before the fragment contents are put into every page?
Thank you.
There are a number of ways to solve this problem depending on whether your menu can change while the user is logged in. If it can change then you can create a base controller class that has a method annotated with #ModelAttribute. All of your controllers would inherit from this base class. This method would obtain the menu items and add them to the model each time a page is requested. A simplistic example of such a class is:
#Component
public abstract class BaseController {
#Autowired
private AlphabetService alphabetService;
#ModelAttribute(name = "alphabet")
public List<String> getAlphabet() {
return alphabetService.getCharacters();
}
}
Your page would access them as normal
th:each="character : ${alphabet}"
You could also access the service directly in the page by doing something like
th:each="character: ${#alphabetService.getCharacters()}"
If the menu items do not change while the user is logged in then you could add #SessionAttributes("alphabet") at the class level to the above example and the service method would be called only once when the first page is displayed and cached in the session for future access.

How to store current URL of the page in a variable for validation purpose on TestComplete

I am working on testcomplete automation tool. I want to know how can I store current URL in a variable for validation. Example,
I click a link that takes me to a particular page. I want to validate against the URL of that page. how can I do that?
You can use the URL property of the Page object. Information on this property along with a script sample can be found in this document:
URL Property (Page Objects)
If it's not opening in a tab/window then this should work fine:
function PageSample()
{
Browsers.Item(btIExplorer).Run("http://smartbear.com/");
// Obtains the browser process
var browser = Sys.Browser("iexplore");
// Obtains the page currently opened in Internet Explorer
var page = browser.Page("*");
// Use the page object property and add a checkpoint to validate
…
}

Request page content through System.Net WebClient in asp.net MVC

Umbraco ver. 4.7
My project is running along side asp.net MVC 4. I have a Umbraco page, which has a Rich Text editor property on the document type, that will retain a body of text with HTML.
Is there a way to use a System.Net.WebClient client, and request the Umbraco page from within my MVC project and have it return the content of that page's document property to the MVC controller's WebClient request?
Bonus Question:
Is there a way to setup an Umbraco macro in the template that will parse the querystring params and have place holders in the Umbraco template that the macro will replace the placeholders with the parsed out querystring params before the content is returned to the request (explained up above)?
Have you used the /Base extension in Umbraco? I believe this may help you out, because I had similar requirements and found /Base extremely powerful. In my document type I actually used a multi-line textbox as input for the html (or in my case Razor script). In my /Base class I created a method that finds the correct node, takes the raw script from the textbox, renders the script into a raw html string, and returns the string wrapped in a tag .
public static string GetRazor()
{
var doc = new DynamicNode(nodeId);
string razorScript = doc.GetPropertyValue("{propertyAlias}");
string razorString = RenderRazor.RenderRazorScriptString(razorScript, nodeId);
return razorString;
}
Another way is to create a macro within Umbraco which your template can call, something like:
#using umbraco.MacroEngines
#{
dynamic node = #Model;
string htmlString = node.GetPropertyValue("{propertyAlias}");
#Html.Raw(htmlString)
}
Then inside your template, you just need to inject the macro:
<umbraco:Macro Alias="{RazorAlias}" runat="server"></umbraco:Macro>
Whatever html in your textbox will be rendered by the razor engine!
-Dan
Add a reference to umbraco.dll and umbraco.MacroEngines.dll in your MVC4 project.
You should then be able to do (within your controller or view):
dynamic d = new DynamicNode(nodeId);
return d.AliasOfRichTextProperty

ASP.NET MVC3 Html.Raw changing absolute URLs?

I'm using ASP.NET MVC 3 and the helper #Html.Raw in my view.
I'm passing it some HTML markup that I have stored in a database. The markup contains some URLs that point to other places on my site. For example http://www.foo.fom/events. These data are forum posts, so the page they're displayed on has the form http://www.foo.com/forums/thread/42/slug.
However, when the page is rendered, the saved URLs are rendered in modified form as:
http://www.foo.com/forums/thread/42/events/
This only happens for URLs on my site. If the URL points to some external site, it is unchanged.
I have verified that what I'm passing into #Html.Raw is the correct URL (http://www.foo.com/events). Why is it getting changed as the page is rendered? Is there an easy way to disable this "feature"?
Here's my code for displaying the markup:
<div>
#Html.Raw(post.Body)
</div>
and here's the controller code that genrates the page data:
var post = _forumRepository.GetPostById(id)
var model = new ForumPostView()
{
Body = post.Body,
PostDate = post.DatePosted,
PostedBy = post.Author,
PostId = post.Id
};
return View(model);
I have verified via debugger that the exact URL in the post.Body before being passed back to the View is of the form "http://www.foo.com/events" (no trailing slash). I have also verified via debugger that the value is unchanged before it is passed into #Html.Raw.
It sounds like the urls that are pointing to other pages on your site are non-absolute. Are you certain they start with a / or http? If not, it's behaving exactly as it's supposed and treating them like relative urls -- and thus appending them to the current url.
(Html.Raw will not manipulate the string, so it's not at fault here)
Also, it wouldn't hurt to show us your code.
No, in fact I am an idiot. The URLs were indeed stored in relative form without a leading /, which is why they ended up being relative to the current page. The text displayed was absolute, which is what I saw when I looked at the db. That's what I get for debugging on a few hours' sleep ;)

how to print an MVC3 "page" to pdf

What is the best way to output an MVC page as PDF and switching the master page to a different master page?
I was thinking I could add a parameter to the query string like format=pdf
and set some sort of filtering that capture the output and transform it to a pdf.
Using classic ASP.NET I did that in an HttpModule.
What is the best way of doing it in MVC?
I have to be able to "print" all the pages in my application, so a FileResult controller method would not work.
I need something generic that can work with every url adding that specific query string parameter
Write a FileResult controller method.
How to create file and return it via FileResult in ASP.NET MVC?
As part of the return result you set the MIME type.

Resources