Hi i'm trying to develop my first application with j2ee using oracle jdeveloper i want to redirect a jsf page by clicking on the login button!!
this is my function in the sessionBean
public String authentification(String login, String pwd) {
try{
Query query;
query = em.createQuery("select o from UserEntity o where " + " o.login = :LOGIN AND o.pwd = :PWD");
query.setParameter("LOGIN",login);
query.setParameter("PWD",pwd);
UserEntity ue = (UserEntity) query.getSingleResult();
return"/untitled2?faces-redirect=true";
}catch(Exception e){
return "/untitled3?faces-redirect=true";
}
}
this is my button in the jsf page
<af:button actionListener="#{bindings.authentification.execute}" text="authentification"
disabled="#{!bindings.authentification.enabled}" id="b1"/>
my faces-config would be like
<faces-config version="2.1" xmlns="http://java.sun.com/xml/ns/javaee">
<application>
<default-render-kit-id>oracle.adf.rich</default-render-kit-id>
</application>
<navigation-rule>
<from-view-id>/untitled1.jsf</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/untitled2.jsf</to-view-id>
<redirect/>
</navigation-case>
<navigation-case>
<from-outcome>fail</from-outcome>
<to-view-id>/untitled3.jsf</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
this is my untitled2.jsf
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE html>
<f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
<af:document title="untitled2.jsf" id="d1">
<af:messages id="m1"/>
<af:form id="f1">
<af:panelGridLayout id="pgl1">
<af:gridRow height="50px" id="gr3">
<af:gridCell width="auto" halign="stretch" valign="stretch" id="gc3">
<!-- Header -->
</af:gridCell>
</af:gridRow>
<af:gridRow height="100%" id="gr1">
<af:gridCell width="auto" halign="stretch" valign="stretch" id="gc2">
<!-- Content -->
<af:panelFormLayout id="pfl1">
<af:panelFormLayout id="pfl2"
inlineStyle="background-color:transparent; width:752px; height:193px;">
<af:inputText value="#{bindings.id.inputValue}" label="#{bindings.id.hints.label}"
required="#{bindings.id.hints.mandatory}"
columns="#{bindings.id.hints.displayWidth}"
maximumLength="#{bindings.id.hints.precision}"
shortDesc="#{bindings.id.hints.tooltip}" id="it1">
<f:validator binding="#{bindings.id.validator}"/>
<af:convertNumber groupingUsed="false" pattern="#{bindings.id.format}"/>
</af:inputText>
<af:inputText value="#{bindings.pwd.inputValue}" label="#{bindings.pwd.hints.label}"
required="#{bindings.pwd.hints.mandatory}"
columns="#{bindings.pwd.hints.displayWidth}"
maximumLength="#{bindings.pwd.hints.precision}"
shortDesc="#{bindings.pwd.hints.tooltip}" id="it2">
<f:validator binding="#{bindings.pwd.validator}"/>
</af:inputText>
<af:inputText value="#{bindings.login.inputValue}" label="#{bindings.login.hints.label}"
required="#{bindings.login.hints.mandatory}"
columns="#{bindings.login.hints.displayWidth}"
maximumLength="#{bindings.login.hints.precision}"
shortDesc="#{bindings.login.hints.tooltip}" id="it3">
<f:validator binding="#{bindings.login.validator}"/>
</af:inputText>
<af:button actionListener="#{bindings.persistUserEntity.execute}"
text="persistUser" disabled="#{!bindings.persistUserEntity.enabled}"
id="b4"/>
<af:button actionListener="#{bindings.Delete.execute}" text="Delete"
disabled="#{!bindings.Delete.enabled}" id="b3"/>
<af:button text="Submit" id="b1"/>
<f:facet name="footer">
<af:button actionListener="#{bindings.Create.execute}" text="Create"
disabled="#{!bindings.Create.enabled}" id="b2"/>
</f:facet>
</af:panelFormLayout>
</af:panelFormLayout>
<af:button actionListener="#{bindings.First.execute}" text="First"
disabled="#{!bindings.First.enabled}" partialSubmit="true" id="b5"/>
<af:button actionListener="#{bindings.Last.execute}" text="Last"
disabled="#{!bindings.Last.enabled}" partialSubmit="true" id="b6"/>
<af:button actionListener="#{bindings.Next.execute}" text="Next"
disabled="#{!bindings.Next.enabled}" partialSubmit="true" id="b7"/>
<af:button actionListener="#{bindings.Previous.execute}" text="Previous"
disabled="#{!bindings.Previous.enabled}" partialSubmit="true" id="b8"/>
</af:gridCell>
</af:gridRow>
<af:gridRow height="50px" id="gr2">
<af:gridCell width="100%" halign="stretch" valign="stretch" id="gc1">
<!-- Footer -->
</af:gridCell>
</af:gridRow>
</af:panelGridLayout>
</af:form>
</af:document>
</f:view>
when i excute my page jsf i get the error Nom id introuvable dans l'objet donné : /untitled2.jsf.
Related
I have a button, when i click a modal panel opened - it's work fine. Now i tried to add a button to hide the panel - it's work also, but the problem is when i tried to show a text "panel closed" after button click it doesn't work. I use Jsf 1.2 and richfaces 3.3.3.
I have the following error message:
org.apache.jasper.el.JspELException: /index.jsp(35,7) 'javascript:Richfaces.hideModalPanel('myModalPanel');#{welcomeBean.showText(true)}' Method not found: class com.firstjsf.backingbeans.WelcomeBean.showText(java.lang.Boolean) at org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:123)
above the code:
index.jsp
<%#taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%#taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%# taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%# taglib uri="http://richfaces.org/rich" prefix="rich"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<f:view>
<h:form>
<rich:panel>
<f:facet name="header">
<h:outputText value="Product"></h:outputText>
</f:facet>
<h:form>
<a4j:commandButton
id="newWid"
value="New Widget..."
immediate="true" ajaxSingle="true"
reRender="text"
oncomplete="javascript:Richfaces.showModalPanel('myModalPanel');"
styleClass="verboseButton noprint" />
</h:form>
<rich:modalPanel id="myModalPanel">
<f:facet name="header">
<h:outputLabel value="123" />
</f:facet>
From Modal Panel
<a4j:commandButton value="Hide" id="btn_hide"
oncomplete="javascript:Richfaces.hideModalPanel('myModalPanel');#{welcomeBean.setShowText(true)}" />
</rich:modalPanel>
<h:outputText id="text"
value="Panel closed"
rendered="#{helloMessage.showText eq true}">
</h:outputText>
</rich:panel>
</h:form>
</f:view>
</html>
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
<managed-bean>
<managed-bean-name>welcomeBean</managed-bean-name>
<managed-bean-class>com.firstjsf.backingbeans.WelcomeBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>helloMessageBean</managed-bean-name>
<managed-bean-class>com.firstjsf.backingbeans.HelloMessageBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<description>Welcome page to message page</description>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>helloMessage</from-outcome>
<to-view-id>/message.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<description>Welcome page to message page</description>
<from-view-id>/message.jsp</from-view-id>
<navigation-case>
<from-outcome>back</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
BeanAction
public class WelcomeBean {
private boolean showText =false;
public String sayHello(){
return "helloMessage";
}
public boolean isShowText() {
return showText;
}
public void setShowText(boolean showText) {
this.showText = showText;
}
}
You're mixing JavaScript and EL expressions, #oncomplete is for executing JavaScript, if you want to do something on the server use #action or #actionListener. Otherwise the expression will be evaluated and the browser will try to execute the return value as if it was JavaScript.
By the way, your code shows you're using setShowText(true) (which is correct), but the exception says you're using just showText(true), which is it then?
Im trying to test a sample JSF project but for some reason the command button are not being displayed that is given in the below 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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<h2>Implicit Navigation</h2>
<hr />
<h:form>
<h3>Using Managed Bean</h3>
<h:commandButton action="#{navigationController.moveToPage1}"
value="Page1" />
<h3>Using JSF outcome</h3>
<h:commandButton action="page2" value="Page2" />
</h:form>
<br/>
<h2>Conditional Navigation</h2>
<hr />
<h:form>
<h:commandLink action="#{navigationController.showPage}"
value="Page1">
<f:param name="pageId" value="1" />
</h:commandLink>
<h:commandLink action="#{navigationController.showPage}"
value="Page2">
<f:param name="pageId" value="2" />
</h:commandLink>
<h:commandLink action="#{navigationController.showPage}"
value="Home">
<f:param name="pageId" value="3" />
</h:commandLink>
</h:form>
<br/>
<h2>"From Action" Navigation</h2>
<hr />
<h:form>
<h:commandLink action="#{navigationController.processPage1}"
value="Page1" />
<h:commandLink action="#{navigationController.processPage2}"
value="Page2" />
</h:form>
<br/>
<h2>Forward vs Redirection Navigation</h2>
<hr />
<h:form>
<h3>Forward</h3>
<h:commandButton action="page1" value="Page1" />
<h3>Redirect</h3>
<h:commandButton action="page1?faces-redirect=true"
value="Page1" />
</h:form>
</h:body>
</html>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtm</url-pattern>
</servlet-mapping>
</web-app>
faces.config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<navigation-rule>
<from-view-id>home.xhtml</from-view-id>
<navigation-case>
<from-action>#{navigationController.processPage1}</from-action>
<from-outcome>page</from-outcome>
<to-view-id>page1.jsf</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{navigationController.processPage2}</from-action>
<from-outcome>page</from-outcome>
<to-view-id>page2.jsf</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
view source html file
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<h2>Implicit Navigation</h2>
<hr />
<h:form>
<h3>Using Managed Bean</h3>
<h:commandButton action="#{navigationController.moveToPage1}"
value="Page1" />
<h3>Using JSF outcome</h3>
<h:commandButton action="page2" value="Page2" />
</h:form>
<br/>
<h2>Conditional Navigation</h2>
<hr />
<h:form>
<h:commandLink action="#{navigationController.showPage}"
value="Page1">
<f:param name="pageId" value="1" />
</h:commandLink>
<h:commandLink action="#{navigationController.showPage}"
value="Page2">
<f:param name="pageId" value="2" />
</h:commandLink>
<h:commandLink action="#{navigationController.showPage}"
value="Home">
<f:param name="pageId" value="3" />
</h:commandLink>
</h:form>
<br/>
<h2>"From Action" Navigation</h2>
<hr />
<h:form>
<h:commandLink action="#{navigationController.processPage1}"
value="Page1" />
<h:commandLink action="#{navigationController.processPage2}"
value="Page2" />
</h:form>
<br/>
<h2>Forward vs Redirection Navigation</h2>
<hr />
<h:form>
<h3>Forward</h3>
<h:commandButton action="page1" value="Page1" />
<h3>Redirect</h3>
<h:commandButton action="page1?faces-redirect=true"
value="Page1" />
</h:form>
</h:body>
</html>
Navigationcontroller
package com.tutorialspoint.test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
#ManagedBean(name = "navigationController", eager = true)
#RequestScoped
public class NavigationController implements Serializable {
private static final long serialVersionUID = 1L;
#ManagedProperty(value="#{param.pageId}")
private String pageId;
public String moveToPage1(){
return "page1";
}
public String moveToPage2(){
return "page2";
}
public String moveToHomePage(){
return "home";
}
public String processPage1(){
return "page";
}
public String processPage2(){
return "page";
}
public String showPage(){
if(pageId == null){
return "home";
}
if(pageId.equals("1")){
return "page1";
}else if(pageId.equals("2")){
return "page2";
}else{
return "home";
}
}
public String getPageId() {
return pageId;
}
public void setPageId(String pageId) {
this.pageId = pageId;
}
}
page1.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">
<h:body>
<h2>This is Page1</h2>
<h:form>
<h:commandButton action="home?faces-redirect=true"
value="Back To Home Page" />
</h:form>
</h:body>
</html>
page2.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">
<h:body>
<h2>This is Page2</h2>
<h:form>
<h:commandButton action="home?faces-redirect=true"
value="Back To Home Page" />
</h:form>
</h:body>
</html>
It's already been answered above, but I'll be specific. In your web.xml the <url-pattern> is specified as *.xhtm and your jsf files have a .xhtml extension. Note the missing letter in the pattern specification. Because of that your files aren't recognized and routed through the faces servlet. Just add the 'l' to the url-pattern in the web.xml and it should start working.
I have made a Login.xhtml by using Apache Myfaces 2.1 (using JSF 1.x)
When I have given my username and password on login.xhtml and one submit button is there.
When I click on goodbye button, the control goes to wsListing.xhtml but the URL in the browser remain as
http://hostid:8080/TestClient/faces/login.xhtml
I want it to be changed as
http://hostid:8080/TestClient/faces/wsListing.xhtml
I have attached the code for the same, please suggest me some solution, I am new to JSF.
Login.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"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui" xmlns:lang="en">
<f:view>
<head>
<title>Test Client Login</title>
</head>
<h:form id="loginForm">
<table>
<tr>
<h:outputLabel for="username">
<h:outputText id="usernameLabel" value="Enter Username:" />
</h:outputLabel>
<h:inputText id="username" value="#{loginBean.username}"
required="true">
<f:validateLongRange minimum="1" maximum="500" />
</h:inputText>
</tr>
<tr>
<h:outputLabel for="password">
<h:outputText id="passwordLabel" value="Enter Password:" />
</h:outputLabel>
<h:inputText id="password" value="#{loginBean.password}"
required="true">
<f:validateLongRange minimum="1" maximum="500" />
</h:inputText>
</tr>
<tr>
<h:commandButton id="goodbyeCommand" type="submit" value="Goodbye"
action="#{loginBean.goodbye}" immediate="true" />
</tr>
</table>
</h:form>
</f:view>
</html>
LoginBean.java
package com.example;
/**
* Managed Bean for Login
*
*/
public class LoginBean {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String goodbye() {
return "success";
}
}
faces-config.xml
<?xml version="1.0"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
<managed-bean>
<description>Login Bean</description>
<managed-bean-name>loginBean</managed-bean-name>
<managed-bean-class>com.example.LoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<description>Navigation from the hello page.</description>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/wsListing.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
You need to send a redirect. Add <redirect /> to the <navigation-case />.
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/wsListing.xhtml</to-view-id>
<redirect />
</navigation-case>
Unrelated to the concrete problem, MyFaces 2.1 is a JSF 2.1 implementation. Why are you forcing the webapp to run in JSF 1.2 modus? You miss so many JSF 2.x advantages.
I am looking for a another way of JSF navigation other than mentioning navigation-cases in faces-config.xml.
At present i am using faces-config.xml to navigate. I want to clean it up.
Please suggest all other ways so that i can use whatever suits my need.
For simple page-to-page navigation (without submitting anything) you should be using <h:outputLink> instead of <h:commandLink>.
So, instead of
<h:form>
<h:commandLink value="Page 1" action="page1" />
<h:commandLink value="Page 2" action="page2" />
<h:commandLink value="Page 3" action="page3" />
</h:form>
and those navigation cases
<navigation-rule>
<navigation-case>
<from-outcome>page1</from-outcome>
<to-view-id>page1.jsf</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>page2</from-outcome>
<to-view-id>page2.jsf</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>page3</from-outcome>
<to-view-id>page3.jsf</to-view-id>
</navigation-case>
</navigation-rule>
you should use
<h:outputLink value="page1.jsf">Page 1</h:outputLink>
<h:outputLink value="page2.jsf">Page 2</h:outputLink>
<h:outputLink value="page3.jsf">Page 3</h:outputLink>
For real form submits you should rewrite the action methods to return void or null instead of an outcome. So, instead of
<h:form>
<h:inputText value="#{bean.query}" />
<h:commandButton value="Search" action="#{bean.search}" />
</h:form>
with
public String search() {
results = searchService.find(query);
return "results";
}
on one page and
<h:dataTable value="#{bean.results}" var="result">
...
</h:dataTable>
on other page and this navigation case
<navigation-rule>
<from-view-id>search.jsf</from-view-id>
<navigation-case>
<from-outcome>results</from-outcome>
<to-view-id>results.jsf</to-view-id>
</navigation-case>
</navigation-rule>
you should use
<h:form rendered="#{empty bean.results}">
<h:inputText value="#{bean.query}" />
<h:commandButton value="Search" action="#{bean.search}" />
</h:form>
<h:dataTable value="#{bean.results}" var="result" rendered="#{not empty bean.results}">
...
</h:dataTable>
with
public void search() {
results = searchService.find(query);
}
You can if necessary include page fragments by <jsp:include>.
See also:
When should I use h:outputLink instead of h:commandLink?
//JSF
<h:outputLink value="login.xhtml" >
Login page
</h:outputLink>
//HTML output
<a href="login.xhtml">
Login page
</a>
Refer this URL for more info:-
commandLink and outputLink example
You can set the return value of a navigation action to the name of the page you want to go to (e.g. return "page2"; to switch to page2.jsf). But as far as I know, this feature has been implemented first in JSF 2.0.
I have what I thought was simple JSF navigation setup but when I hit the h:commandButton the page reloads, not the page I want to load. The faces-config snippet is here:
<navigation-rule>
<from-view-id>/index.jsf</from-view-id>
<navigation-case>
<from-outcome>hello</from-outcome>
<to-view-id>/next.jsf</to-view-id>
</navigation-case>
</navigation-rule>
the index.xhtml file contains this:
<f:view>
<a4j:region id="topRegion">
<rich:page pageTitle="myapp" markupType="xhtml" id="top">
<f:facet name="header">
<h:form>
<rich:toolBar height="45" itemSeparator="disc">
<rich:toolBarGroup location="left">
<h:form name="selectForm">
<h:panelGrid columns="5" style="padding: 2px;">
<h:outputText style="text-align: center" value="Node Select " />
<h:selectOneMenu id="nodes" value="#{MyBacking.chosenNode}">
<f:selectItems value="#{MyBacking.nodes}" />
</h:selectOneMenu>
<h:commandButton value="Retrieve" styleClass="ctrlBtn"
id="retrieveBtn" style="margin-bottom: 2px;"
action="hello"
image="/img/btnRetrieve26.png" />
</h:panelGrid>
</h:form>
</rich:toolBarGroup>
</rich:toolBar>
</h:form>
It's as simple as that. Can anyone tell me why it's not working?
As far as I'm aware, the from-outcome of hello should be used by the h:commandbutton and load next.xhtml file.
Your files are .xhtml not .jsf
have you tried:
<navigation-rule>
<from-view-id>/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>hello</from-outcome>
<to-view-id>/next.xhtml</to-view-id>
</navigation-case>
</navigation-rule>