Play! Framework: Layouts with different sections - java

I'm having a problem when trying to use layouts in the Play! Framework. As far as I know, a layout has a SINGLE #{doLayout /} tag that specifies where all the code of the child view should be placed. This means that all the code in a particular view of, say, list of Users (list.html) gets injected in the middle of the body of the layout. Now, I find that some of my views require javascript that is particular to those views, and so I'd like to include script tags only on those views and not on every single view that inherits from the layout. The Razor view engine in ASP.NET MVC allows for different sections in a layout that are filled in by a view that extends that layout, but I don't know if Play supports something like this.
Do you see a solution to this problem?

You can also use the #get,#set tags to define other blocks. For example:
#{set 'anyBlock'}
<h1>Main title</h1>
#{/set}
and:
#{get 'anyBlock' /}

You can use the script tag in your specific views. For instance :
#{script 'jquery.js' /}
I recommend you to read this documentation page :
http://www.playframework.org/documentation/1.1/tags#script

Related

Is it possible to add #fragment part of uri in Vaadin 14 and RouterLink?

Is it possible to add #fragment part of URI in Vaadin 14 and RouterLink?
I can't find anything in Vaadin docs.
I need this to have fast access to a specific part of the view from another view.
Is it possible to add #fragment part of URI in Vaadin 14 and RouterLink?
No, that is not possible. There is a solution to your problem*, but RouterLink does not have any means to define an url fragment so it would scroll to the HTML element with the given id.
*How could your needs be implemented instead:
Let your target view class implement HasUrlParameter<String> (use #Wildcard annotation on setParameter, so that you also can navigate to this view without specifying a fragment-parameter).
In the setParameter method in your view, you then read the given parameter and if it's not empty you could execute some javascript that scrolls to the element with the given id. Now you can specify the fragment in the RouterLink like this:
new RouterLink("MyView", MyView.class, "#my-fragment");
Yes, this is not an optimal solution, not by a long shot, but if you really need it, you could do it.
Please create a ticket for adding API to RouterLink for defining url fragments in the github repo of vaadin-flow, if you want this to happen.

Adding a java.awt.frame to a vaadin layout

I'm working on a vaadin page, but one of the elements I want to put in my VerticalLayout is a java.awt.Frame. Is there a way to do this in vaadin?
As said in the comments, you can't use Swing/AWT stuff in Vaadin since it will be converted to Javascript and DOM to be used in a browser.
If you get a hold on the JS file TeeChart uses you can basically implement a custom client-side widget that will use it.
Take the basic demo here what you have to take care of is that there is a <canvas> tag to render the content in and that the draw() is called at a phase when Vaadin is done creating the surrounding DOM structure.
Please have a look at this tutorial to get an idea on how to wrap a JavaScript library to a Vaadin component.

Basic MVC design: views

So there are two different views(controllers are merged into views):
View 1: Tabular view. There is a table and a number of buttons on top of the table.
View 2: Text view. There is a text area and a number of buttons which are distinct from the buttons in Tabular view.
There is one model file for two views files to link.
I also create a main.java file to declare the main window,a tabbed pane(to switch views) and bind views to it.
As a noob java developer and MVC design pattern learner, I was wondering:
1. What is the correct way to declare buttons, the table and text area?
For example, for View 1(Tabular), are its buttons and table declared in the view or in the main.java?
2. If declared in views, how are they added in the main window? Default UpdateAllViews() doesn't seem to go through the main window in main.java.
At this point, I am only aware that model should never ever have anything like JButton declared in it as model itself should not be aware about what the window and stuff looks and feels. I can see that controllers are sort of binded to a certain view componenet, but the view itself gets me very confused.
If anyone can provide a shortcut to get deeper understanding of MVC pattern, I'd be appreciated.
Please keep in your mind that you are dealing with objects. And the Model, View and Controller are categories/collections of objects.
Your model objects are instances of classes relating to your business domain. e.g. if you are making an address book, you would have an ADDRESS class.
Your View objects provide a connection to your users. e.g. SEARCHDIALOG class and ADDRESSDIALOG class.
Your Controller provides the bindings/interface into your system (the systems API). you will have one controller that represents the system, e.g. ADDRESSBOOKAPP class.
Enjoy.

JSF 2.0 Dynamic Views

I'm working on a web project which uses JSF 2.0, PrimeFaces and PrettyFaces as main frameworks / libraries. The pages have the following (common) structure: Header, Content, Footer.
Header:
The Header always contains the same menu. This menu is a custom component, which generates a recursive html <ul><li> list containing <a href="url"> html links, this is all rendered with a custom renderer. The link looks like 'domain.com/website/datatable.xhtml?ref=2'. Where the ref=2 used to load the correct content from the database. I use prettyfaces to store this request value in a backingbean.
Question 1: Is it ok to render the <a href> links myself, or should I better add an HTMLCommandLink from my UIComponent and render that in the encodeBegin/End?
Question 2: I think passing variables like this is not really the JSF 2.0 style, how to do this in a better way?
Content:
The content contains dynamic data. It can be a (primefaces) datatable, build with dynamic data from the database. It can also be a text page, also loaded from the database. Or a series of graphs. You got the point, it's dynamic. The content is based on the link pressed in the header menu. If the content is of type datatable, then I put the ref=2 variable to a DataTableBean (via prettyfaces), which then loads the correct datatable from the database. If the content is of type chart, I'll put it on the ChartBean.
Question 3: Is this a normal setup? Ideally I would like to update my content via Ajax.
I hope it's clear :)
It's ok to just output link yourself, commandLink is out of the question (it does a postback using javascript, it's not what you want);
Parameter are all in the param implicit object. You can insert them by a #ManagedProperty annotation, like this:
#ManagedProperty("#{param.ref}")
String ref
// .. getters, setters (obligatory!)
You can also use (if you are on JSF 2) the f:viewParam tag (a nice description http://blogs.oracle.com/rlubke/entry/jsf_2_0_bookmarability_view), you get the bonus of validation and conversion.
The way I understand it, your setup is rather complicated. Using a handwritten custom component for a menu is a huge overkill (at least judging from the provided description), a composite component would probably do. JSF has no special way of making ajax calls between views or embedding views one into another, so - unless you use iframes - your only choice would be to include all the possible pieces of content into a single view, wrapped in panels, and render them as required:
<h:panelGroup rendered='#{backingBean.ref == 2}'>
... content 2 ...
</h:panelGroup>
and so on. Careful, this would be heavy on resources.
You could also write your own ajax solution in javascript. This would require all the pieces of content to be fully independent views, with their own forms. Also, all their postbacks would have to go through ajax, so the main page does not get reloaded.

Understanding Tapestry Principle 1. "Static Structure, Dynamic Behaviour"

I'm learning tapestry 5 web framework but I don't understand the principle 1 about it:
"Static Structure, Dynamic Behaviour", what does means it ?
If I don't add components to the components, how can I create a dynamic page?
anyone can help me?
Thanks in advance
It means that you can't choose or replace components at runtime effectively.
If, say, you'd want to build a portal solution where users could arrange components on a screen any way they wanted, Tapestry would not offer an effective way to do that, because components have static structure, i.e. you must define what goes into them at compile-time in their template file.
Or you might have a specialized menu for administrators, so you might want to just replace the Menu component with a derived component, AdminMenu - but you can't, you have to use if statements in the template or use a block to inject different menus into your layout component.
There's an anti-pattern related to this limitation: The God or über-component tries to solve this problem by effectively having a giant template file with all the available components, like this:
<t:if t:test="displayComponentA">
<span t:type="ComponentA" ... />
</t:if>
<t:if t:test="displayComponentB">
<span t:type="ComponentB" ... />
</t:if>
...
This, however, is horribly ineffective, as Tapestry assembles the entire component tree, including components that are not displayed, to do the rendering of the page.
Tapestry uses templates to define static content. These templates are usually html pages with placeholder variables which are replaced by some code dynamically by the framework. Templates allow for segregation of things that not change from the ones that change. Usually structure is less prone to change then behavior. Even if you want to change some element of a component dynamically you're going to use some component that itself is defined by a template that is dynamically filled with data. This dynamic data again can insert some other component etc.
Static structure doesn't mean that you cannot output dynamic content nor that you cannot add components to components. You just cannot add a component to another at runtime. You can define a page or component structure using other components, but this is all defined in the template, before the page is rendered, never while it's rendered. A component can choose not to render itself, to render part of its template (If and Unless components), etc.
One of the few practical situations caused by the static structure of Tapestry is that a component C cannot use another instance of the same component inside it.

Categories