How to show previewtext in JSF components? - java

Is it possible to show previewtext in JSF 1.2 components, for example in inputfields, selectmenue or in outputtext and the preview text disappears, if the user clicks into one of these components?
I saw that the attribute defaultLabel can be used in Rich faces for example? Does JSF component have a similar attribute?
Thanks & Greetz
Marwief

If you mean to use placeholders, you can look for Primefaces watermark or RichFaces placeholder
another way to go : take a look at passthrough with JSF2.2
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://xmlns.jcp.org/jsf/passthrough">
<h:inputText value="#{item.name}" p:placeholder="my item"/>
...
that will be rendered as :
<input type="text" ... placeholder="my item">
but beware, this is HTML5

Related

Add html tags to Struts 1.3.10 sources

Hello StackOverflow community,
I'm stuck with Struts 1.3.10 (no, I can't try migration to a better framework since my project was given to my team with 70% completion). I'd like to add HTML tags to the Struts taglib.
I already know that for some tags (like placeholder) you can use jQuery but I'm not interested in that. I want to modify Struts sources. I've been looking for on the internet but nothing showed up that can help me. Maybe you can give me a hand with these matter.
Examples of what I want to do:
<html:text property="someProperty" placeholder="Hi..." length="30">
or
<html:checkbox property="choice" data-on-color="primary" data-off-color="info">
or add any HTML property I want.
Thanks in advance, I hope you can guide me.
You do not need to rewrite Struts 1.3.10, only you need to write the desired HTML5 form. You can convert the Struts tag:
<html:text property="someProperty" placeholder="Hi..." length="30">
to HTML5 (using some tags for the value):
<input type="text"
name="someProperty"
value="<bean:write name="nameOfForm" property="someProperty" />"
placeholder="Hi..."
maxlength="30"
size="15">
Note: nameOfForm must the name of the form defined in the file struts-config.xml.
or (using EL):
<input type="text"
name="someProperty"
value="${nameOfForm.someProperty}"
placeholder="Hi..."
maxlength="30"
size="15">
Check the generated HTML with Struts tags, and performs the conversion to HTML5.

primefaces components not working with own css

i am using ready-made template(with css and j-queries) in my java ee app. all the primefaces components are rendered properly except the panelgrid control of primefaces 3.2.
it is displayed with border. i want it without border.
i have removed all the table styling from the css of custom ready-made template.
still the border is there.
when i remove the readymade template, the panelgrid is rendered perfectly without any border. how do i remove the border and what is the cause of this problem?
edited:
xhtml file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AP administration panel - A massive administration panel</title>
</h:head>
<h:body>
<div>
<h:form>
<p:panelGrid columns="2" style="border: none">
<h:outputText value="scrip symbol"/>
<p:inputText value=""/>
<p:commandButton value="submit"/>
</p:panelGrid>
</h:form>
</div>
</h:body>
</html>
When overriding PrimeFaces default styles, you have to specify a CSS selector of at least the same strength or to specify a stronger selector. The strength of a CSS selector (the cascading rules) is specified in the W3 CSS specification and clearly explained in this article: Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade.
Based on PrimeFaces own CSS, the following selectors should do:
.ui-panelgrid tr, .ui-panelgrid td {
border: none;
}
Just put them in a .css file which you include by <h:outputStylesheet> inside the beginning of the <h:body> so that it will be included after PrimeFaces own style.
<h:body>
<h:outputStylesheet name="layout.css" />
...
</h:body>
See also:
How to remove border from specific PrimeFaces p:panelGrid?
How do I override default PrimeFaces CSS with custom styles?
Update: As per your update, your CSS doesn't seem to be loaded at all. You should have noticed this by verifying the HTTP traffic in browser builtin webdeveloper toolset (press F12 in Chrome/IE9/Firebug) and seeing that it returned a HTTP 404 error. When using <h:outputStylesheet> you need to put the CSS file in the /resources folder of the webcontent. So you must have a /resources/css/mycss.css in order to be able to use <h:outputStylesheet name="css/mycss.css" />.
See also:
How to reference CSS / JS / image resource in Facelets template?

JSF 2 - construct a page from two files - main and current content

I'm quite new to using JSF and I'm not sure if that's the right way to go, but in Rails you usually have a main application file into which the current page is loaded. That way I don't have to worry about copy-pasting the menu, etc. every time.
How can I achieve that with JSF 2? Can I navigate to the same main page every time and tell it to load a current content? Or do I tell the current page that I navigate to to load the "main frame around the content"?
Thanks!
Yes of course, JSF 2.0 has page-templating feature. You define a template that defines a generic layout to all the view pages.
Facelets tags to create basic page:
ui:insert – defines content that is going to replace by the file that load the template;
ui:define – defines content that is inserted into tag ui:insert;
ui:include – includes content from another page;
ui:composition – the specified template is loaded, if used with template attribute, and the children of this tag defines the template layout. In other case, it’s a group of elements, that can be inserted somewhere.
For example:
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/templates/myLayout.xhtml">
<ui:define name="menu">
<ui:include src="/mypath/menu.xhtml"/>
</ui:define>
<ui:define name="content">
<ui:include src="/mypath/content.xhtml"/>
</ui:define>
</ui:composition>
or
<ui:insert name="content">
<ui:include src="/mypath/mycontent.xhtml"/>
</ui:insert>
JSF doesn't support what you want to archive. Instead, it support the views and basic layout(template). What you need it this:
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
template="path/to/template.xhtml>
<your custom content here/>
<ui:composition/>

How to create shadow in JSF input field

I registered account into oracle.com web site and I saw something very interesting:
See the input field of the telephone number into the form. How I can reproduce the same into JSF input form? Is this made by CSS?
CODE UPDATE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<script type="text/javascript">
<!-- input field shadow -->
var placeholder = "test field"
$("input").on({
focus: function() {
if (this.value == placeholder) {
$(this).val("").removeClass("shadow");
}
},
blur: function() {
if (this.value == "") {
$(this).val(placeholder).addClass("shadow");
}
}
}).trigger("blur");​
</script>
</h:head>
<h:body>
<h1>JSF 2 textbox example with shadow</h1>
<h:form>
<h:inputText value="#{userBean.userName}" />
<h:commandButton value="Submit" action="user" />
</h:form>
</h:body>
</html>
I tested this code but it's not working. Maybe I need to call the JavaScript code as function into the <h:inputText> tag?
This is new since HTML5. It's the placeholder attribute, which is also known as "watermark" before the HTML5 era.
<input type="text" name="phone" placeholder="e.g. +994 (12) 491 2345" />
This works of course only in browsers supporting HTML5. When using JSF, this works only with JSF components supporting the new HTML5 placeholder attribute. The standard JSF <h:inputText> doesn't support this natively (yet). You'd need to look for a 3rd party component library supporting this. Among them is PrimeFaces with its <p:watermark> component:
<h:inputText id="phone" value="#{register.user.phone}" />
<p:watermark for="phone" value="e.g. +994 (12) 491 2345" />
This will check if HTML5 is supported and then use placeholder, otherwise it will bring in some JS/jQuery magic to simulate the same. Nothing of this all is done by CSS.
If using PrimeFaces is not an option for some reason, you'd need to reinvent it yourself in flavor of a custom component and/or a custom piece of JS/jQuery, exactly like <p:watermark> is doing under the covers.
I think they are using jquery's watermark to do that way. Attached is the firebug view of that page which shows that
In case if you have no possibility to use PrimeFaces, as #BalusC stated, you can use JavaScript/jQuery code like this:
var placeholder = "e.g. +358 (11) 123-45-67"
$("input").on({
focus: function() {
if (this.value == placeholder) {
$(this).val("").removeClass("shadow");
}
},
blur: function() {
if (this.value == "") {
$(this).val(placeholder).addClass("shadow");
}
}
}).trigger("blur");​
CSS class shadow has font ​color: #bbb.
DEMO: http://jsfiddle.net/rYAbU/

How to define an onLoad function in JSF template that will be defined elsewhere

I'm designing the view for my site, which has a standard login and landing page, and I want to have an onLoad function called for my login page, but not for my other pages (yet). I've got a template.xhtml file, which has this insert:
<div id="content">
<ui:insert name="content"/>
</div>
Then in login.xhtml I have:
<ui:define name="content">
...
</ui:define>
Normally I would put this in login.xhtml:
<body onload="document.getElementById('login_form:name').focus();">
But since I'm using JSF's ui composition tags, I can't have the <body/> tag in login.xhtml (at least the way I am attempting to do it).
Is there a way to accomplish this with the structure I've described? The way I would think of doing it is to have onLoad call a function in the template, and then each page with ui:define would populate this function. Is that possible?
Thanks!
I can think of at least two ways:
define the header section with <ui:define name="header">, and put a javascript function (function bodyLoaded(){..}) in it - different on every page, and then reference it via <body onload="bodyLoaded();">
use facelets params. I.e. <body onload="#{onLoadJS}"/> and on each page including the template use <ui:param name="onLoadJS" value="document.getElementById(..)" />

Categories