Why does my form open in a new Tab? - jsf

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>.

Related

JSF Example Doesn't Work

I have a simple JSF example application from Core JavaServer Pages, 3rd Edition (page 122). The example is a simple JavaScript example that I had asked about before, but was unable to post the code for at that time.
This message is a follow up, with the actual code, to see if anyone can see why the html page does not correctly call the JavaScript function (in fact, nothing happens when I click submit).
All the paths are recognized by Intellij IDEA 14. In fact, if I try to change a variable name like outputScript library, Intellij goes "Red".
This code is right out of the Core JavaServer Pages, 3rd Edition where the same source code is freely available.
Thanks in advance.
index.xhtml:
<?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"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>#{msgs.windowTitle}</title>
<h:outputStylesheet library="css" name="styles.css"/>
<h:outputScript library="javascript" name="checkPassword.js"/>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2" columnClasses="evenColumns, oddColumns">
#{msgs.namePrompt}
<h:inputText/>
#{msgs.passwordPrompt}
<h:inputSecret id="password"/>
#{msgs.confirmPasswordPrompt}
<h:inputSecret id="passwordConfirm"/>
</h:panelGrid>
<!--Execute JavaScript function on click of the button-->
<h:commandButton type="button" value="Submit Form" onclick="checkPassword(this.form)"/>
</h:form >
</h:body>
</html>
----------
JavaScript Function called by index.xhtml above:
checkPassword.js
function checkPassword(form)
{
alert("inside js");
var password = form[form.id + ":password"].value;
var passwordConfirm = form[form.id + ":passwordConfirm"].value;
if(password == passwordConfirm)
form.submit();
else
alert("Password and password confirm fields don't match");
}

ArrayIndexOutOfBoundsException for composite component with ajax invocation inside

My 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://java.sun.com/jsf/html"
xmlns:compo="http://java.sun.com/jsf/composite/test">
<h:head>
</h:head>
<h:body>
<h:form>
<compo:composite changeListener="#{testBean.invoke}"/>
</h:form>
</h:body>
</html>
My composite component:
<?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:composite="http://java.sun.com/jsf/composite"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<composite:interface>
<composite:attribute name="changeListener" required="true"
method-signature="java.lang.Object action(javax.faces.event.AjaxBehaviorEvent)"/>
</composite:interface>
<composite:implementation>
<h:commandButton value="Click">
<f:ajax event="click" listener="#{cc.attrs.changeListener}"/>
</h:commandButton>
</composite:implementation>
</html>
My java bean (prototype scope in spring):
public class Bean {
public void invoke(javax.faces.event.AjaxBehaviorEvent event) {
// some logic
}
}
When I trying to press button, I see
02-Jul-2014 01:24:22.242 WARNING [http-apr-8080-exec-43] com.sun.faces.lifecycle.InvokeApplicationPhase.execute 0
java.lang.ArrayIndexOutOfBoundsException: 0
at org.apache.el.parser.AstValue.convertArgs(AstValue.java:287)
at org.apache.el.parser.AstValue.invoke(AstValue.java:241)
in log. For some cases I've got: java.lang.NumberFormatException: For input string: testBean.invoke So, it is JSF bug or I do something wrong? How I can pass method expression inside composite component?
Looks like JSF bug https://java.net/jira/browse/JAVASERVERFACES-2758
Exceptions are different, but if you opens response in browser, you will see PropertyNotFoundException.
Workaround for me: pass not MethodExpression, but bean and methodName separately and execute listener as
listener="#{cc.attrs.bean[cc.attrs.method]}"
The stack trace it looks like the Apache Expression Language parser form Tomcat (org.apache.el.parser). I think it is this issue: https://bz.apache.org/bugzilla/show_bug.cgi?id=57855 ( Invoke MethodExpression with wrong pram count results in ArrayIndexOutOfBoundsException).

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?

Possible Scope problems in a JSF2 web application

I´ve a JSF web application using SPRING3 + JSF2 (mojarra 2.1.12) and I´m having some problems that I think can be derived of a bad choose of the Scope of one controller in one combo of the JSF2+primefaces interface.
This combo functionality allows the user to choose one of the internal xhtml subpages and a button that automatically redirects you to that page.
The website uses a template technology: there is template (standard.xhtml) with the JSF2 code and using this template there are several pages: a inicio.xhtml that is the home page and a contact page (contacto.xhtml). In both pages the combo is displayed properly.
The following code it is working properly only if you´re navigating at the home page (incio.xhtml) but when you´re in other part of the web (i.e. contacto.xhtml) it displays the combo with all the options, but the button doesn´t work at all.
Here it´s my code in standard.xhtml file:
<?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"
xml:lang="${userContext.locale}" lang="${userContext.locale}"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:sec="http://www.springframework.org/security/tags">
<f:view contentType="text/html">
<h:head>
//HEAD
</h:head>
<body>
<div id="…">
<div id="…">
//CODE
//CODE to go from one page (inicio.xhtml) to other (contacto.xhtml)
<li><h:commandLink value="${msg.app_menu_contacto}" action="mContacto" /></li>
//MORE CODE
<h:form>
//MORE CODE
<h:panelGrid id="buscar" columns="2" style="margin-top:20px;" cellpadding="5">
<p:selectOneMenu id="selectBuscar" value="#{buscador.procedimiento}" panelStyle="width:150px;"
var="p" style="width:220px; margin-right: 10px; " filter="true" filterMatchMode="startsWith">
<f:selectItem itemLabel="${msg.app_comun_seleccionar}" itemValue="" />
<f:selectItems value="#{buscador.procedimientos}" var="proc" itemLabel="#{proc.codigo}" itemValue="#{proc.valor}"/>
</p:selectOneMenu>
<p:commandButton id="btnIr" style="width:40px" value="#{msg.app_comun_btn_ir}" title="#{msg.app_comun_btn_ir_title}"
action="#{buscador.direccion}">
</p:commandButton>
</h:panelGrid>
</h:form>
</div>
</div>
</body>
</f:view>
</html>
In the faces-navigation.xml we have these lines (faces-navigation is only used in several inherited parts of the code):
<navigation-case>
<from-outcome>mContacto</from-outcome>
<to-view-id>/views/comun/contacto.xhtml</to-view-id>
<redirect />
</navigation-case>
Code of the xhtml home page where it is working:
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui"
xmlns:sec="http://www.springframework.org/security/tags"
template="/WEB-INF/layouts/standard.xhtml">
//Code independent of the combo or button failing
// it uses ui:define to show different components in the page.
</ui:composition>
and this is the controller code:
#Controller("buscador")
//#Scope(“Session”)  Do not work with “request”, “view” or without scope
public class BuscadorController implements Serializable {
public List<ValorCombo> getProcedimientos() {
//Code that returns a list of pair String to show in the combo, name of the xhtml page should be redirected
}
public String direccion() {
//CODE
String salida = "proc/"+procedimiento+"?faces-redirect=true";
//more CODE
return salida;
}
}
I have tried this but it doesn´t work neither.
http://balusc.blogspot.com.es/2011/09/communication-in-jsf-20.html
how can I solve this problem?
Thanks!

Check User-Agent with 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>

Resources