load html pages on client side on button click - html

I am new to GWT. How do i load different and static HTML pages on the client side via button clicks. Have read up and do not wish to go into RPC, frames, client bundles and the following page:
best way to externalize HTML in GWT apps?
If client bundles are the closest i can get, may I have a very simple example, assuming that i have 4 HTML pages to be loaded on the client side, navigable by button clicks?
From my understanding, these individual pages may be created by UIBinders - please correct me if I'm wrong.
I have only the following code to display another page upon click, which is not working the way I want it. Also it gives a 403 error:
button.addClickHandler (new ClickHandler(){
#Override
public void onClick (ClickEvent event){
//Window.alert("Hello again");
String winUrl = GWT.getModuleBaseURL();
String winName = "Testing Window";
openNewWindow (winName, winUrl);
}
});

If you don't care about the state of your browser client page, then something like:
Window.Location.replace(newUrl);
Then the html page you point to in newUrl may or may not load the same java script as the current page.
If you do care about keeping the state of your browser client page and just want to avoid loading your static page at initial load time (by default all your pages get loaded at once), then code splitting is your friend. The principle is to wrap the code showing your new page around an async call defining the code split, something like:
GWT.runAsync(new RunAsyncCallback()
{
#Override
public void onFailure(Throwable reason)
{
Window.alert("Error in fetching the split javascript for page ...");
}
#Override
public void onSuccess()
{
// Code to setup and show your new static page instead of the current page.
// This code will be in a javascript file that won't be loaded at initial page load.
// You must make sure though that the below code does not use/include common code.
RootPanel.get().clear();
RootPanel.get().add(newWidget);
}
});

I guess it is as simple as:
Window.Location.assign("MenuPage.html");

Related

How to dynamically write the svg data to a file from an HTML page using JXBrowser as a background process

I am using JXBrowser and I have a valid license. I have a requirement to extract svg data from the HTML in the background using JXBrowser. HTML gets loaded from browser.loadURL(). When I print the HTML content , I see that all the data from ajax requests does not exist at all. SVG data is rendered using ajax request . I have also used the below method, but the response is same. Can anyone please help me to fix this issue ?
Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>() {
#Override
public void invoke(Browser browser) {
browser.loadURL("");
}
});

Send head before the controller completes rendering

How can one send <head> contents before the controller finishes? The idea is to start loading CSS as soon as possible (don't wait for controller action).
Sample scenario:
// in the controller
sleep(5);
This gives:
blank page for 5 seconds -> display the head -> start loading CSS -> body
The flow I want to get is:
Send head -> start loading CSS -> wait for the controller -> send rest of the page (body)
The <head> is now in layout.phtml, which later includes the index controller script (index.phtml).
Maybe I could have <head> as a partial and send it somehow before the whole layout?
One approach is to create an abstract controller that all controllers extend, and in the onDispatch function render the head template and flush:
public function onDispatch(MvcEvent $e) {
$renderer = $this->getServiceLocator()->get('ViewRenderer');
$content = new ViewModel();
$content->setTemplate('path/to/head.phtml');
$content = $renderer->render($content);
echo $content;
flush();
parent::onDispatch($e);
}
Drawbacks to this approach:
You have no access to the headTitle, headMeta, headLink, headScript and other view helpers elsewhere in your application (it is possible in a controller or viewscript to add a style sheet and js plugin for just that page).
You will be unable to perform redirects as a response has already been sent
You can't gzip the content as well as flushing it
Some versions of Microsoft Internet Explorer will only start to display the page after they have received 256 bytes of output, so you may need to send extra whitespace before flushing to get those browsers to display the page.
In theory, you could use this approach to load all static content in the layout before echoing $this->content - such as logo, navigation, search bar, etc etc.
As I've stated, this breaks redirects meaning helpers and plugins such a PostRedirectGet will not work.

Create Dynamic View with static HTML in MVC

I have been researching dynamic content for MVC views and partial views but have not successfully found an architecture to fit my needs.
Basically I am required to create a landing page based on parameters pass by the URL.
For basics
http://mydns.com/myconroller/myview/?landingpage=Param1
The controller will need to find the HTML that will be used to create the view.
The view is going to be different based on the landing page.
(for the sake of the question, I am using landingpage as an example)
My goal is to be able to deploy a Landing page and based on the URL use that HTML Landing page in the view based on the landingpage parameter that is passed.
There are other views that are working currently in the controller. I am trying to add functionality to be able to add a new one time page without having to recompile.
I have searched through various ideas on how to load dynamic views but cannot seem to find a solution that fits this need based on what I have read.
I can possibly RedirectToAction but I am still in the dark on where to deploy and I am getting several problems with Razor as it is not in the shared directory and then I am stuck with deployment issues as I want to organize the landing pages differently than I am organizing the views.
Solution:
I decided to take a different approach and use the ContentResult Action in the controller. I still have the Main View and I use the HTML extensions to render the HTML pages that I have deployed in my customer's directory.
#{
Html.RenderAction("LandingPageContent", "Controller", Model);
}
Then in the controller I load the HTML directly and return the ContentResult
public ContentResult LandingPageContent(object model, FormCollection collection)
{
MySRCHelper helper = new MySRCHelper();
ContentVariables variables = helper.getContentSRC(model.EntryCode);
model.ContentSRC = variables.LandingPageSRC;
return Content(System.IO.File.ReadAllText(Server.MapPath(model.ContentSRC)));
}
I can then configure the path to the raw HTML file to be used and it will be loaded into the View. The View can then house all of the paths to load jQuery, CSS and other necessary javascript to integrate with the raw HTML and allow me to deploy the HTML files into any directory structure that I want. The configuration XML file allows me to find XML elements and use those values for any HTML that I am looking for, like a welcome and thank you page. The helper object will open the XML and find the configuration based on the parameters passed to the View.
<ContentLandingItem entrycode="1" customerID="Cutomer1">
<ContentLandingPageSRC>~/Customers/Customer1/Customer1Landing.htm</ContentLandingPageSRC>
<ContentThankyouSRC>~/Content/Default/GenericThankyou.htm</ContentThankyouSRC>
</ContentLandingItem>
<ContentLandingItem entrycode="2" customerID="Cutomer2">
<ContentLandingPageSRC>~/Customers/Customer2/Customer2Landing.htm</ContentLandingPageSRC>
<ContentThankyouSRC>~/Customers/Customer2/Customer2Thankyou.htm</ContentThankyouSRC>
</ContentLandingItem>
The view still performs its duties and works independently on it own letting the raw HTML decorate the View. The model is still intact and can be used as I wish. The FormCollection is there in case a form submit posts the values to the view and provides some things that I omitted from this question as it did not pertain to this subject.
I don't want to answer my own question and I found the pieces that helped me on another site, so I am putting what I did here in case anyone needs this functionality.
This sounds like using the you can inherit from the virtual path provider view engine and decide based on the URL parameters (or other) which view to return. Some example that you can adjust to your needs:
public class CustomViewEngine : VirtualPathProviderViewEngine
{
public MyViewEngine()
{
this.ViewLocationFormats = new string[] { "~/Views/{1}/{2}.mytheme ", "~/Views/Shared/{2}.mytheme" };
this.PartialViewLocationFormats = new string[] { "~/Views/{1}/{2}.mytheme ", "~/Views/Shared/{2}. mytheme " };
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
var physicalpath = controllerContext.HttpContext.Server.MapPath(partialPath);
return new RazorView(controllerContext, physicalpath);
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
var physicalpath = controllerContext.HttpContext.Server.MapPath(viewPath);
return new RazorView(controllerContext, physicalpath);
}
}
In there you can return a RazorView or WebFormView and set your desired path for the view to use.

Browser does not fetch page or link not working if link is to current page

Browser does not fetch page or link not working if link is to current page. In the code sample below, I have a link (in a menu). If the user is currently in the GetData page, and the user clicks on the "Get Data" link again, nothing happens. The browser does not attempt to retrieve the page. I've monitored this in IE8 and Chrome developer tools. Any idea?
//Link
<i class="glyphicon glyphicon-tag"></i>Get Data
//Controller has a custom Authorization attribute
[AuthorizeRedirect]
public class DataController : Controller
// Controller Action Invoked
[HttpGet]
public ActionResult GetData(int parameter)
{
...
}
I created a test web application from scratch and the page is refreshed even when I click on a link to a page that is currently loaded.
I compared both response headers from the application and the test application and noticed a difference in the cache header.
My application contains the following:
Cache-Control:no-cache, no-store
While my test application contains only the following:
Cache-Control:private
Additional Info: I am using Angular JS and Bootstrap client side.
I thing it happens because of caching of Browser. Clear the Browser cache and try again. hope that i will help
How about setting the Headers inside the Application_PreSendRequestHeaders event in Global.asax?
You can use Response.Cache.SetCacheability rather than setting the Headers directly.*
void Application_PreSendRequestHeaders(Object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
}
Alternative way by setting the Headers manually.
void Application_PreSendRequestHeaders(Object sender, EventArgs e)
{
Response.Headers.Set("Cache-Control", "no-cache");
}
If you don't want no caching everywhere then you might try something like this:
[OutputCache(Location = OutputCacheLocation.None)]
public class HomeController : Controller
{
...
}

PHP redirect page

i have a kohana website, and i want that, if someone modifies the current url (in the browser when he visits the website), and that specific page doesn't exist, he should be redirected to the current page (where he is now), or on the homepage, but without displaying any error.
any idea about how this can be done?
The guide gives the basic steps you need to take.
You basically replace the existing exception handler by defining a class called Kohana_Exception.
In that handler, you would check the error number and if it's a 404, then do a redirect based on the http referer.
class Kohana extends Kohana_Core
{
public static function handler(Exception $e)
{
if($e instanceof Kohana_Request_Exception)
{
Request::current()->redirect(Request::initial()->referrer());
}
}
}
This should be placed in for example application/classes/kohana.php
Note that this is the basic gist. You should expand on this and check if HTTP_Referer is set and based that the user actually came from your site.
Also note that this can cause confusion as people often don't notice they have been redirected.
Check the guide for other things you should do in the exception handler (for example, pass it on to the default handler.