I was building a very simple WebApp with Maven, to create a small REST Api (while using JSF).
In one of my beans (amongst other methods and variables) I created a very simple List just to test if I could get its options in JSF <p:selectItems>
Here's the bean code:
#ManagedBean(name="er", eager=true)
public class ExchangeRates {
private List<String> frase = new ArrayList<>();
public List<String> getFrase() {
frase.add("opcao1");
frase.add("opcao2");
return frase;
}
public void setFrase(List<String> frase) {
this.frase = frase;
}
/*some more code that doesn't use "frase"*/
And here's the (xhtml) JSF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Currency Conversion REST Api</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</h:head>
<h:body>
<h1>Currency Conversion</h1>
<h:form>
<h:outputLabel for="txtName">
<h:outputText value="Quantity: " />
</h:outputLabel>
<h:inputText id="txtNamea" value="#{er.quantity}" />
<br></br><br></br>
<h:outputLabel for="currency1" value="Convert from: "/>
<h:selectOneMenu id="currency1" value="#{er.currency1}" style="width:70px">
<f:selectItems value="#{er.frase}" />
</h:selectOneMenu>
</h:form>
</h:body>
</html>
The dropdown created by <f:selectItems> was supposed to give me the 2 options ("opcao1" and "opcao2"). However, when I click it it acts as if I didn't assign any variable or values to it, what could I be doing wrong?
Related
Using Primefaces 5.0, JSF 2.2.7, deployed on EAP 6.1.
I have this Managed Bean below.
import hh.bean.Service;
import hh.dao.ServiceDao;
import hh.dao.impl.ServiceDaoImpl;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class View1 implements Serializable {
private static final long serialVersionUID = 1L;
private ServiceDao serviceDao = new ServiceDaoImpl();
#PostConstruct
public void init() {
System.out.println(View1.class.getName() + ": init() " + this);
}
public List<Service> getServices(){
return serviceDao.getAllServices();
}
}
I'm calling it from the xhtml below.
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Home Web</title>
<f:facet name="first">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8" />
<meta name="viewport"
content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
</f:facet>
</h:head>
<h:body>
<h:outputStylesheet library="css" name="newcss.css" />
<p:dataTable var="service" value="#{view1.services}">
<p:column style="width:16px">
<p:rowToggler />
</p:column>
<p:column headerText="Id">
<h:outputText value="#{service.id}" />
</p:column>
<p:column headerText="xxxx">
<h:outputText value="#{service.description}" />
</p:column>
<p:rowExpansion>
<p:dataTable var="sv" value="#{view1.services}">
<p:column headerText="Id">
<h:outputText value="#{sv.id}" />
</p:column>
</p:dataTable>
</p:rowExpansion>
</p:dataTable>
</h:body>
</html>
I noticed every time I expand the row my init() gets called. I thought #ViewScoped lives on when the request stays on the same page.
When I switch to #SessionScoped, init() does not get called when I expand a row.
Edit 1: Put the entire xhtml in, specify jsf version/impl
Edit 2: Fixed this issues by surrounding the p:dataTable with h:form. Not sure why that fixed it...
Fixed this issues by surrounding the p:dataTable with h:form. Not sure why that fixed it...
The JSF view state is maintained by javax.faces.ViewState hidden input field of the <h:form>. If you don't use a <h:form>, then PrimeFaces won't be able to find that hidden input field in order to pass its value along with the jQuery ajax request.
If this information is absent in the (ajax) request, then JSF will simply create a brand new view and inherently also all view scoped beans associated with it.
I would like to execute a simple command with ajax but without any inputs.
This is my code:
<h:form>
<h:commandButton class="button"
value="Invite"
action="#{trainingController.inviteProfile(p)}">
</h:commandButton>
</h:form>
The variable p is from a which is filled also via an ajax request.
Any ideas how to achieve my goal?
You can archive by using f:ajax.
The example show below.
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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Submit</title>
</h:head>
<h:body>
<h:form>
<h:commandButton value="Submit"
action="#{coffeeBean.submit('abc')}">
<f:ajax execute="#this"
render="#this" />
</h:commandButton>
</h:form>
</h:body>
</html>
ManagedBean
#ManagedBean(name = "coffeeBean")
#SessionScoped
public class CoffeeBean implements Serializable {
public void submit(String s){
System.out.println("s:" + s);
}
}
You can see more information about EL library from this like, and this like is present How to check version of EL is server.
I am new here :)
I have got a problem with one of my first web-apps written using JSF.
I wrote a simple template, it looks 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:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<p:layout fullPage="true">
<p:layoutUnit position="north" header="North - header" style="font-size: 15px;">
<h:form>
<ui:include src="header.xhtml"/>
</h:form>
</p:layoutUnit>
<p:layoutUnit position="west" header="West - menu" style="font-size: 15px;">
<h:form>
<ui:include src="menu.xhtml"/>
</h:form>
</p:layoutUnit>
<p:layoutUnit position="center" header="Center - content" style="font-size: 15px;">
<h:form id="contentForm">
<ui:insert name="content"/>
</h:form>
</p:layoutUnit>
<p:layoutUnit position="east" header="East - nothing" style="font-size: 15px;">
</p:layoutUnit>
<p:layoutUnit position="south" header="South - footer" style="font-size: 15px;">
<ui:include src="footer.xhtml"/>
</p:layoutUnit>
</p:layout>
</h:head>
<h:body>
</h:body>
</html>
And in another file (obrazek.xhtml) I wrote this 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"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Simple JSF Facelets page</title>
</h:head>
<h:body>
<ui:composition template="template.xhtml">
<ui:define name="content">
<h:form id="PHOTOS">
<p:panelGrid columns="2">
<h:commandLink action="#{pic.addValue1}">
<p:graphicImage url="resources/images/1.png">
<p:ajax update="wynik1" event="click"/>
</p:graphicImage>
</h:commandLink>
<h:commandLink action="#{pic.addValue2}">
<p:graphicImage url="resources/images/2.png">
<p:ajax update="wynik1" event="click"/>
</p:graphicImage>
</h:commandLink>
<h:outputText value="Obrazek 1: #{pic.val1}" id="wynik1"/>
<h:outputText value="Obrazek 2: #{pic.val2}" id="wynik2"/>
</p:panelGrid>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
I wanted to insert some code into "content" primefaces layoutUnit.
IDEA: It has to be 2 pictures and if I click on one of them, AJAX should show the value from ManagedBean using h:outputText. (it always adds +1 to the value :))
The problem is: if I dont add templating** into obrazek.xhtml everything works fine, but if I add:
<ui:composition template="template.xhtml">
<ui:define name="content">
CODE FROM FORM "PHOTOS" (the one about I called upper that "is no templated"**)
</ui:define>
</ui:composition>
while I am clicking on some picture nothing happens. Just "#" is added to my URL and it looks like that:
http://localhost:8080/obrazek.xhtml#
And my ManagedBean also:
#ManagedBean(name = "pic", eager = true)
#SessionScoped
public class PictureBean {
private int val1;
private int val2;
public void addValue1(){
val1 += 1;
}
public void addValue2(){
val2 += 1;
}
GETTERS AND SETTERS FOR VAL1 VAL2
}
I have rummaged all the web looking for the answer but uselessly.
Could anyone here help me? :)
Pardon for so long post and not coloured syntax, have to figure out "how to" do it, first :D
My name is Tomasz and this is my first question here. Here is my problem.
I am a junior Java EE developer and I have 4 moths experience with Java EE, Spring, Spring-Webflow, JSF 2 and Primefaces. I want to learn more by doing my own web application.
I have no experience with starting a new project with technologies I mentioned, so basically I copied a template application and changed it a little bit (mostly removed unnecessary features which I won't need now). So my application contains:
- Spring 3.2.2
- Spring Webflow 2.3.2
- JSF 2.1.22
- Primefaces 3.5
- and Hibernate JPA (it doesn't matter, this part works good).
As a server I use Tomcat 7.0.40.
I configured my application using many configuration tutorials. Application works, it doesn't have warnings but it does not work as it should. Let me show you screen shots:
Firt one, when I start the application, it looks like this:
http://oi44.tinypic.com/2wd4od3.jpg
But after I refresh the browser by pressing F5, it looks like it should from the beginning:
http://oi39.tinypic.com/17fr06.jpg
It is a really strange behaviour for me, I don't know why it happens. Browser doesn't matter. I've tried it on Chrome, FireFox and IE - the same effect.
Also p:commandLink seems to ignore me. I click it and nothing happens. When I have option
ajax="false"
enabled it looks like the page is refreshed, but still nothing happens. I work a lot with primefaces and swf at work, and that never happend to me.
Here are some details from my application, I can post everything on request:
template (main.xhtml):
<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<ui:include src="/WEB-INF/layout/fragments/layout.xhtml" />
</html>
layout (layout.xhtml):
<!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:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html" encoding="UTF-8">
<h:head>
</h:head>
<h:body>
<!-- outputStyle have to be here, otherwise css order is wrong -->
<h:outputStylesheet id="cssStandard" library="css" name="standard.css" />
<!-- <h:outputStylesheet id="cssStandard" library="css" name="simple.css" /> -->
<h:form>
<div id="glass"
style="position: absolute; width: 100%; height: 100%; z-index: 2000; display: none;"></div>
<ui:include src="/WEB-INF/layout/fragments/ajaxError.xhtml" />
<p:layout id="layout" fullPage="false"
style="width: 1000px; height: 900px; margin: 0 auto;">
<p:layoutUnit position="north" size="150">
<ui:include src="/WEB-INF/layout/fragments/north.xhtml" />
</p:layoutUnit>
<p:layoutUnit position="center">
<h:form>
<ui:insert name="content" />
</h:form>
</p:layoutUnit>
</p:layout>
<p:outputPanel id="dialogs" style="display:block; overflow: auto;">
<ui:insert name="dialogs" />
</p:outputPanel>
</h:form>
</h:body>
</f:view>
</html>
navigation menu (north.xhtml):
<!DOCTYPE html 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:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:form>
<a href="#{facesContext.externalContext.requestContextPath}"> <p:graphicImage
name="logo.png" library="images/application" />
</a>
<h:panelGrid columns="3" style="width: 100%">
<h:column>
<p:menubar style="width: 300px">
<p:menuitem value="Strona Główna" action="forwardTo" ajax="false">
<f:setPropertyActionListener target="#{flowController.flowId}"
value="main" />
</p:menuitem>
<p:submenu label="MENU_2">
</p:submenu>
</p:menubar>
</h:column>
<h:column>
<h:outputText value="Zalogowany jako: " style="float: right" />
</h:column>
<p:column>
<p:menuButton
value="#{mainController.loggedUser != null ? mainController.loggedUser.login : 'Gość'}">
<p:menuitem value="Zaloguj" action="forwardTo" ajax="false">
<f:setPropertyActionListener target="#{flowController.flowId}"
value="login" />
</p:menuitem>
<p:menuitem value="Wyloguj" />
</p:menuButton>
</p:column>
</h:panelGrid>
</h:form>
</ui:composition>
base-flow.xml:
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd"
abstract="true">
<end-state id="endAndForwardTo" view="flowRedirect:#{flowController.flowId}" />
<global-transitions>
<transition on="forwardTo" to="endAndForwardTo" validate="false" />
<transition on="endAndForwardTo" to="endAndForwardTo" />
</global-transitions>
</flow>
FlowController.java:
package tomashoov.polskielowiska.flow;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
#Component
#Scope("session")
public class FlowController {
private Logger LOG = Logger.getLogger(FlowController.class);
public final static String MAIN = "main";
public final static String LOGIN = "login";
private String flowId = MAIN;
public String getFlowId() {
return flowId;
}
public void setFlowId(String flowId) {
this.flowId = flowId;
}
}
If more details are needed, please tell me.
I faced the exact same problem and I dug a little in this problem and I made a work around to fix this bug (since I am very late in my project). I will come back with a robust solution later, but here is what I got:
You are using Spring Webflow the start page is index.jsp with <meta http-equiv="Refresh" content="0; URL=MAIN_WEB_FLOW_URL" /> to force redirect to the main web flow.
You will find in your browser console 2 JavaScript errors:
ReferenceError: $ is not defined
ReferenceError: PrimeFaces is not defined
These errors are because missing JavaScript files:
JQuery JavaScript.
Primefaces JavaScript.
Layout JavaScript.
Note that there is also CSSs files missing and that is why the page layout sizes and locations are missed up.
So, it is obvious the the <h:head></h:head> tags didn't fetch some Primefaces JavaScript files and CSSs on the redirect. I found an issue here with the redirect resources fetch failure. The proposed solution was to add <h:outputScript library="primefaces" name="jquery/jquery.js" target="head" /> in the <h:head></h:head>. So, I added the missing scripts and the CSS files in the <h:head></h:head> and it worked like a charm.
<h:outputStylesheet library="primefaces" name="primefaces.css" />
<h:outputStylesheet library="primefaces" name="layout/layout.css" />
<h:outputScript library="primefaces" name="jquery/jquery.js" target="head" />
<h:outputScript library="primefaces" name="jquery/jquery-plugins.js" target="head" />
<h:outputScript library="primefaces" name="primefaces.js" target="head" />
<h:outputScript library="primefaces" name="layout/layout.js" target="head" />
I can't get the preRenderView event listener to work on a GET request in JSF 2.1.
I have found a lot about it but nothing seems to work e.g.:
Conditional redirection in JSF
http://andyschwartz.wordpress.com/2009/07/31/whats-new-in-jsf-2/#get-prerenderview-event
http://developer.am/j2eetutorial/jsf/?page=jsf-2-prerenderviewevent-example
JSF, Spring, and the PreRenderViewEvent
http://balusc.blogspot.dk/2011/09/communication-in-jsf-20.html#ProcessingGETRequestParameters
I have a template with 4 insert blocks and I have tried to insert the event code at all those places but without any luck. I have tried both with and without the f:metadata tag surrounding it.
<f:event type="preRenderView" listener="#{applicationData.redirectIfNoResults}" />
Bean:
#ManagedBean
#ApplicationScoped
public class ApplicationData implements Serializable {
public void redirectIfNoResults() throws IOException {
if (getTotal() < 1) {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "/noResults.xhtml");
}
}
...
}
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://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:insert name="beforeHeader" />
<f:view>
<ui:insert name="inView" />
</f:view>
<h:head>
<meta http-equiv="cache-control" content="no-store" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title>Quick Poll</title>
<ui:insert name="header" />
</h:head>
<h:body>
<h1>Quick Poll</h1>
<ui:insert name="content" />
</h:body>
</html>
View:
<ui:define name="content">
#{applicationData.question}?<p/>
<h:panelGrid columns="3" border="0">
Yes:
<h:panelGrid bgcolor="black" height="20" width="#{300*applicationData.yes/applicationData.total}"/>
#{applicationData.yes}
<h:outputText value="No:"/>
<h:panelGrid bgcolor="black" height="20" width="#{300*applicationData.no/applicationData.total}"/>
#{applicationData.no}
</h:panelGrid>
</ui:define>
</ui:composition>
Please help me figure out how to get it working..
Update 1:
I have made changes as suggested by BalusC but it is still not working..
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://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<meta http-equiv="cache-control" content="no-store" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title>Quick Poll</title>
<ui:insert name="header" />
</h:head>
<h:body>
<h1>Quick Poll</h1>
<ui:insert name="content" />
</h:body>
</html>
View:
<?xml version='1.0' encoding='UTF-8' ?>
<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:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
template="template.xhtml">
<ui:define name="content">
<f:event listener="#{applicationData.redirectIfNoResults}" type="preRenderView"></f:event>
#{applicationData.question}?<p/>
<h:panelGrid columns="3" border="0">
Yes:
<h:panelGrid bgcolor="black" height="20" width="#{300*applicationData.yes/applicationData.total}"/>
#{applicationData.yes}
<h:outputText value="No:"/>
<h:panelGrid bgcolor="black" height="20" width="#{300*applicationData.no/applicationData.total}"/>
#{applicationData.no}
</h:panelGrid>
</ui:define>
</ui:composition>
That <f:view> isn't rightly used, it has to wrap the entire view. Remove it (JSF will implicitly create one), or at least let it wrap the entire view, including <h:head> and <h:body> tags.
By the way, the <f:event> does not need to go in a <f:metadata>. That applies only to <f:viewParam>. A <f:event> listener which depends on results of <f:viewParam> is indeed often for sole self-documentary purposes also placed in the same <f:metadata> block, but that is thus not a requirement of <f:event> itself.
In your case, it'd be easier to just put it in <ui:define name="content">.
ComponentSystemEvent seems to be missing in the method signature.
Method needs to look like this:
public void newRequest(final ComponentSystemEvent event) {
System.out.println("Someone requested me");
}
And then place a caller in the template or in the seperate views, that won't do any difference.
<f:event listener="#{userSessionAction.newRequest}" type="preRenderView"></f:event>
I ended up making a solution with a PostConstruct method in a view scope bean.
Like this: Initializng a Backing Bean With Parameters on Page Load with JSF 2.0
Bean:
#ManagedBean
#ViewScoped
public class ResultsController {
#ManagedProperty(value="#{applicationData.total}")
private int total;
#ManagedProperty(value="#{applicationData.yes}")
private int yes;
#ManagedProperty(value="#{applicationData.no}")
private int no;
#PostConstruct
public void postConstruct() {
if (getTotal() < 1) {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
try {
ec.redirect(ec.getRequestContextPath() + "/noResults.jsf");
} catch (IOException e) {
System.out.println("noResults.jsf redirect failed.");
e.printStackTrace();
}
}
}
...
}
View:
<?xml version='1.0' encoding='UTF-8' ?>
<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:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
template="template.xhtml">
<ui:define name="content">
#{applicationData.question}?<p/>
<h:panelGrid columns="3" border="0">
Yes:
<h:panelGrid bgcolor="black" height="20" width="#{300*resultsController.yes/resultsController.total}"/>
#{resultsController.yes}
<h:outputText value="No:"/>
<h:panelGrid bgcolor="black" height="20" width="#{300*resultsController.no/resultsController.total}"/>
#{resultsController.no}
</h:panelGrid>
</ui:define>
</ui:composition>