Check User-Agent with jsf - jsf

I want to check the user agent of my client. (If it's possible the browser type/version)
But I don't know how do that...
Thank you for your help.

It is OK, I have find.
I use :
final HttpServletRequest request =(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
final String userAgent = request.getHeader("user-agent");
System.out.println(userAgent);
Thank you for your help.

It is sent in the User-Agent http header.
HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
String userAgent = request.getHeader("user-agent");

In a JSF2 page, you can print the user agent directly via the request object:
<h:outputText value="#{request.getHeader('user-agent')}" />
But don't forget to use the h-namespace ;-)
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
You can then respond via functions to different browsers.
Example:
<?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"
xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<p>
User-Agent: <h:outputText value="#{request.getHeader('user-agent')}" />
</p>
<h:panelGroup rendered="#{fn:containsIgnoreCase(request.getHeader('User-Agent'), 'Firefox')}">
<p>
Hello Firefox!
</p>
</h:panelGroup>
</h:body>
</html>

Related

FacesMessge not clearing in WebSphere Liberty server 20.0.0.2

Am developing application using JSF 2.2.16, PrimeFaces 6.2 and WebSphere Liberty server, Now am upgrading Liberty server from 19.0.0.11 to 20.0.0.2. Here am facing issue that FacesMessage not clearing on request. Below is my sample code,
Here is my jsf page 'welcome.xhtml' looks like,
<!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:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:t="http://myfaces.apache.org/tomahawk"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h1>Hello World PrimeFaces</h1>
<h:form>
<p:message id="mainMessages" globalOnly="true" autoUpdate="true"/>
<p:commandButton action="#{mainBean.redirect}" value="submit" />
</h:form>
</h:body>
</html>
Here is my 'redirect.xhtml' looks like,
<!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:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:t="http://myfaces.apache.org/tomahawk"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h1>Redirect</h1>
<h:form>
<p:message id="mainMessages" globalOnly="true" autoUpdate="true"/>
</h:form>
</h:body>
</html>
below is my sample java code,
#Named(value="mainBean")
#ViewAccessScoped
public class MainBean {
public String redirect(){
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "save");
FacesContext.getCurrentInstance().addMessage(null, message);
return "redirect"
}
}
below is sample navigation-config.xml,
<navigation-case>
<from-action>#{mainBean.redirect}</form-action>
<form-outcome>redirect</form-outcome>
<to-view-id>/pages/redirect.xhtml</to-view-id>
<redirect/>
</navigation-case>
When I click on submit button, it successfully redirected to redirect.xhtml page. But, message was not appeared in screen when am using WebSphere Liberty server 19.0.0.11. But when I Upgrade server to 20.0.0.2. Message getting appear in the screen. I would like to know reason why message appeared in the screen after redirect.

JSF page template --html meta refresh is not working [duplicate]

This question already has an answer here:
JSF returns blank/unparsed page with plain/raw XHTML/XML/EL source instead of rendered HTML output
(1 answer)
Closed 5 years ago.
Here's template
<?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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="refresh" content="30">
<title><ui:insert name="title">Default Title</ui:insert></title>
<h:outputStylesheet library="css" name="jsfcrud.css"/>
</h:head>
<h:body>
<h1>
<ui:insert name="title">Default Title</ui:insert>
</h1>
<p>
<ui:insert name="body">Default Body</ui:insert>
</p>
</h:body>
</html>
here's a client
<?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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<ui:composition template="/template.xhtml">
<ui:define name="title">
<h:outputText value="#{bundle.ListPhonerecordTitle}"></h:outputText>
</ui:define>
<ui:define name="body">
<h:form styleClass="jsfcrud_list_form">
<p:poll interval="10" listener="#{phonerecordController.prepareList}" update="#all" />
...
...
...
</h:form>
</ui:define>
</ui:composition>
</html>
When I run client page meta refresh is not working (refresh page every 30 seconds).
I tried putting meta refresh tag in template (inside h:head) but when client page is run it is not refreshing the page.
Should I try putting the meta refresh tag inside ui:composition tag in client page?
If I add an image inside h:body in template like this
<?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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><ui:insert name="title">Default Title</ui:insert></title>
<h:outputStylesheet library="css" name="jsfcrud.css"/>
</h:head>
<h:body>
<h:graphicImage value = "resources/phonegirl1.PNG"/><br />
<h1>
<ui:insert name="title">Default Title</ui:insert>
</h1>
<p>
<ui:insert name="body">Default Body</ui:insert>
</p>
</h:body>
</html>
and try to open in client page image is not there. It's not visible when i run template page as well.
This may have the answer:
Template content not displaying
if i add a text "Hello World" in template like so
<?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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><ui:insert name="title">Default Title</ui:insert></title>
<h:outputStylesheet library="css" name="jsfcrud.css"/>
</h:head>
<h:body>
Hello World!
<h1>
<ui:insert name="title">Default Title</ui:insert>
</h1>
<p>
<ui:insert name="body">Default Body</ui:insert>
</p>
</h:body>
</html>
every client page referring to template has Hello World written on its page.
But if I try the same in one of the client pages they don't work.

Facelet client does not use assigned facelet client

Hope you can help me with this trivial problem. I have created a facelet template like this:
<?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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<h:outputStylesheet name="./css/default.css"/>
<h:outputStylesheet name="./css/cssLayout.css"/>
<title>Facelets Template</title>
</h:head>
<h:body>
<div id="top">
<ui:insert name="top"><h1>Super Cool App</h1></ui:insert>
</div>
<div id="content" class="center_content">
<ui:insert name="content">Content</ui:insert>
</div>
<div id="bottom">
<ui:insert name="bottom"><br/><br/>Powered by GlassFish & NetBeans</ui:insert>
</div>
</h:body>
</html>
Now I have a facelet client which is assigned to the template like below. However, when I remove the top and bottom part of the clients facelet, it should inherence the top and bottom stuff from the template, right? But it does not. Could you tell me, what I did wrong?
<?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:ui="http://xmlns.jcp.org/jsf/facelets">
<body>
<ui:composition template="./WEB-INF/templates/template.xhtml">
<ui:define name="content">
content
</ui:define>
</ui:composition>
</body>
</html>
Thanks in advance. See you along.

Unicode character not correctly redisplayed in request query string

I wrote this little JSF page:
<?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://xmlns.jcp.org/jsf/html">
<h:head>
<title>What's your Name?</title>
</h:head>
<h:body>
<h1>What's your name?</h1>
<h:form>
<p>Name: <h:inputText value="#{nameController.name}" /></p>
<h:commandButton value="Submit" action="#{nameController.process}" />
</h:form>
</h:body>
</html>
The name property in nameController is simple, with a variable (of type String), a getter, and a setter, just how you would expect. nameController.process passes name as a GET parameter to another JSF page.
But, if I type "ëlmer" as the name, for example, I am redirected to this URL:
http://localhost:8080/NameThing/name.jsf?name=ëlmer
instead of
http://localhost:8080/NameThing/name.jsf?name=ëlmer
How is this caused and how can I solve it?

Why does my form open in a new Tab?

Here is my code:
<?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>
<title>Guess Number</title>
</h:head>
<h:body>
The number I guessed is: #{guessNumber.generatedNumber}
<br />
<strong>Please guess the number I generated which is between 0 and 10!</strong>
<h:form target="index">
<h:commandButton type="submit" value="Guess The Number!"></h:commandButton>
</h:form>
</h:body>
</html>
So in the output I see something like:
The number I guessed is: 6 Please guess the number I generated which
is between 0 and 10!
and a button below this text. When I click the button, the page opens in a new tab. But why?
That's caused for the target attribute in the <h:form>. Note that this is defined for pure HTML, not a JSF-ish special behavior.
To solve the problem, just remove the target attribute from the <h:form>.

Resources