Remember me functionality using cookies [duplicate] - jsf-2

This question already has answers here:
How to implement "Stay Logged In" when user login in to the web application
(4 answers)
Closed 7 years ago.
I am trying to implement remember me functionality using JSF 2.0 and not sure how to implement COOKIES to do that. Can you share a working sample code?

Edit: Do not store password nor username in cookies !
See this post by BalusC for a better implementation.
I believe this might help you:
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:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<h:body>
<h:form id="LoginForm">
<h:panelGrid id="lpg" columns="4" >
<h:outputText value="User Id"/>
<h:inputText id="username" value="#{loginBean.userId}"/>
<h:outputText value=""/>
<h:outputText value="Password"/>
<h:inputSecret id="password" value="#{loginBean.password}"/>
<h:outputText value=""/>
<h:outputText value=""/>
<h:outputText value=""/>
<h:outputText value=""/>
<h:outputText value=""/>
<h:selectBooleanCheckbox value="#{loginBean.checkBox}" />
<h:outputText value="Remember me" />
<h:outputText value=""/>
<h:outputText value=""/>
<h:commandButton value="Login" action="#{loginBean.doLogin}"/>
</h:form>
</h:body>
</html>
LoginBean.java
public class LoginBean {
private String userId;
private String password;
private boolean checkBox = false;
private String virtualCheck;
// Setter and getter
public LoginBean() {
isChecked();
}
public void isChecked() {
FacesContext fc = FacesContext.getCurrentInstance();
Cookie[] cookiesArr = ((HttpServletRequest)(fc.getExternalContext().getRequest())).getCookies();
if(cookiesArr != null && cookiesArr.length > 0)
for(int i =0; i < cookiesArr.length; i++) {
String cName = cookiesArr[i].getName();
String cValue= cookiesArr[i].getValue();
System.out.println("***cValue***"+cValue);
if(cName.equals("cUserId")) {
setUserId(cValue);
} else if(cName.equals("cPassword")) {
setPassword(cValue);
} else if(cName.equals("cVirtualCheck")) {
setVirtualCheck(cValue);
if(getVirtualCheck().equals("false")) {
setCheckBox(false);
setUserId(null);
setPassword(null);
} else if(getVirtualCheck().equals("true")) {
System.out.println("Here in doLogin() line 99");
setCheckBox(true);
}
}
}
}
public String doLogin() {
if(userId != null && password!= null){
FacesContext fc = FacesContext.getCurrentInstance();
if(checkBox == true) {
virtualCheck = "true";
Cookie cUserId = new Cookie("cUserId", userId);
Cookie cPassword = new Cookie("cPassword", password);
Cookie cVirtualCheck = new Cookie("cVirtualCheck", virtualCheck);
cUserId.setMaxAge(3600);
cPassword.setMaxAge(3600);
cVirtualCheck.setMaxAge(3600);
((HttpServletResponse)(fc.getExternalContext().getResponse())).addCookie(cUserId);
((HttpServletResponse)(fc.getExternalContext().getResponse())).addCookie(cPassword);
((HttpServletResponse)(fc.getExternalContext().getResponse())).addCookie(cVirtualCheck);
} else {
virtualCheck = "false";
Cookie cVirtualCheck = new Cookie("cVirtualCheck", virtualCheck);
((HttpServletResponse)(fc.getExternalContext().getResponse())).addCookie(cVirtualCheck);
}
return "success";
}
NOTE In order to save password browser will prompt to save password or not irrespective of Java Web Technologies and browsers setting and retrieving cookies will play a major role.

Related

rich:dataScroller only displaying first page

my datascroller displays only first 5 records whenever i try to navigate.. i Am sure m making some silly mistake..
this my 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:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:composition template="/template/BasicTemplate.xhtml">
<ui:define name="content">
<h:form>
<h:panelGrid>
<rich:dataScroller for="sampleData" maxPages="20" id="scroller"
page="#{richBean.scrollerPage}">
<rich:dataTable id="sampleData" value="#{richBean.employees}"
var="loc" rows="5">
<rich:column>
<h:outputText value="#{loc.empId}" />
</rich:column>
<rich:column>
<h:outputText value="#{loc.empName}" />
</rich:column>
<rich:column>
<h:outputText value="#{loc.dept}" />
</rich:column>
</rich:dataTable>
</h:panelGrid>
</h:form>
</ui:define>
</ui:composition>
</html>
and this my java file
#ManagedBean(name = "richBean", eager = true)
#SessionScoped
public class JavaBeatRichfacesBean {
private List<employee> employees;
private int scrollerPage=1;
public int getScrollerPage() {
System.out.println("getScrollerPage");
return scrollerPage;
}
public void setScrollerPage(int scrollerPage) {
System.out.println("setScrollerPage");
this.scrollerPage = scrollerPage;
}
public JavaBeatRichfacesBean() {
this.employees = new ArrayList<employee>();
employee employee = null;
for (int i = 1; i < 50; i++) {
employee = new employee();
employee.setEmpId(String.valueOf(i));
employee.setEmpName("Name : " + i);
employee.setDept("JSF");
employees.add(employee);
}
}
public List<employee> getEmployees() {
System.out.println(employees.size());
return employees;
}
public void setEmployees(List<employee> employees) {
this.employees = employees;
}
}
EDITED
Two thing i have noticed is
I have to refresh each time to change page navigation.
if use h:dataTable instead of rich:dataTable.. it is working
properly ,Surprisingly ..
and if i add execute="#form" render="#form" in rich:datatable is working properly but i have double click everything..
any explanation why this happening would be useful..

JSF 2 sending parameters via URL

I'm trying to send parameter via URL and it fails, because I somewhat can't assign value to the bean or I can't concat value with URL string. By the way when I write color value by hand everything works, for example:
<h:button outcome="viewParams?c=red"/>
This is the page code from which I'm trying to send parameter.
<h:form>
<h:selectOneMenu value="#{viewMngr.selectedColor}">
<f:selectItems value="#{viewMngr.colors}" var="c"
itemLabel="#{c}" itemValue="#{c}" />
<f:ajax listener="#{viewMngr.valueChanged}" />
</h:selectOneMenu>
<h:button outcome="viewParams?c=#{viewMngr.selectedColor}"/>
</h:form>
This is viewManager bean code (as far as I can see bean value is set, because I'm able to print it out)
#ManagedBean
public class ViewMngr {
private ArrayList<String> colors = new ArrayList<String>();
private String selectedColor;
public ViewMngr()
{
getColors().add("red");
getColors().add("green");
getColors().add("blue");
getColors().add("gray");
getColors().add("yellow");
getColors().add("orange");
}
public ArrayList<String> getColors() {
return colors;
}
public void setColors(ArrayList<String> colors) {
this.colors = colors;
}
public String getSelectedColor() {
return selectedColor;
}
public void setSelectedColor(String selectedColor) {
System.out.println("Selected color: " + selectedColor);
this.selectedColor = selectedColor;
}
public void valueChanged() {
System.out.println("Value changed!");
}
}
Now here is the code from second page which tries to catch that parameter:
<?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">
<f:metadata>
<f:viewParam name="c" value="#{color.color}"/>
</f:metadata>
<h:body style="background-color:#{color.color}">
<fieldset>
<h:form>
<h:outputText value="Some text!"/>
</h:form>
</fieldset>
</h:body>
</html>
And here is color bean:
#ManagedBean
public class Color {
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
if(color != null)
System.out.println(color);
this.color = color;
}
}
Being your destination page viewParams.xtml:
<h:button outcome="viewParams">
<f:param name="c" value="#{viewMngr.selectedColor}" />
</h:button>
Takes the view id and appends the params to the url.
It turned out that button needed to be rerendered because c value didn't get set. The tricky part was that viewMngr.selectedColor was set but to set parameter c, button needed to be rerendered. Here is the edited code:
<h:body>
<fieldset>
<h:form prependId="false">
<h:selectOneMenu value="#{viewMngr.selectedColor}">
<f:selectItems value="#{viewMngr.colors}" var="c"
itemLabel="#{c}" itemValue="#{c}" />
<f:ajax listener="#{viewMngr.valueChanged}" render="buttonId"/>
</h:selectOneMenu>
<h:button id="buttonId" outcome="viewParams?c=#{viewMngr.selectedColor}"/>
</h:form>
</fieldset>
</h:body>

JSF Message on SUBMIT action

I begin with my case:
JSF 2.1
Tomcat 7.0.27
Netbeans as IDE
JSF and PRIMEFace (but optional)
Level JSF beginner
Level JAVA good and not god
I have done a simple JSF site to learn how to works with JSF. My question is based on Login example at this step the problem is not related about that login that is obviously insicure.
A bit of code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
/**
*
* #author
*/
#ManagedBean
#SessionScoped
//#RequestScoped
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private final String userName = "User";
private final String userPassword = "12345";
private String name;
private String password;
private boolean isLogged=false;
public String getName() {
return name;
}
public void setName( String name ) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword( String password ) {
this.password = password;
}
public String login() {
if( !(userName==null || password==null)
&&
(userName.equals( name ) && userPassword.equals( password ))) {
isLogged=false;
return "main";
} else {
isLogged=true;
return "index";
}
}
public boolean getIsLogged(){
return isLogged;
}
}
The page index
<?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>TSAM 7.5 Login</title>
</h:head>
<h:body>
Login system
<br />
<!--<h:link outcome="welcomePrimefaces" value="Primefaces welcome page" />-->
<h:form>
User : <h:inputText value="#{user.name}" />
Password : <h:inputSecret value="#{user.password}" />
<h:commandButton action="#{user.login()}" value="Submit" />
<h:commandButton value="reset" type="reset" />
<h:commandButton value="otherpage" action="otherpage"></h:commandButton>
</h:form>
</h:body>
</html>
The main page
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.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">
<f:event type="preRenderView" listener="#{user.isLogged}"/>
<f:view contentType="text/html">
<h:head>
<f:facet name="first">
<meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
<title>PrimeFaces</title>
</f:facet>
</h:head>
<h:body>
<p:layout fullPage="true">
<p:layoutUnit position="north" size="100" resizable="true" closable="true" collapsible="true">
Header
</p:layoutUnit>
<p:layoutUnit position="south" size="100" closable="true" collapsible="true">
Footer
</p:layoutUnit>
<p:layoutUnit position="west" size="175" header="Left" collapsible="true">
<p:menu>
<p:submenu label="Resources">
<p:menuitem value="Demo" url="http://www.primefaces.org/showcase-labs/ui/home.jsf" />
<p:menuitem value="Documentation" url="http://www.primefaces.org/documentation.html" />
<p:menuitem value="Forum" url="http://forum.primefaces.org/" />
<p:menuitem value="Themes" url="http://www.primefaces.org/themes.html" />
</p:submenu>
</p:menu>
</p:layoutUnit>
<p:layoutUnit position="center">
Welcome to PrimeFaces
</p:layoutUnit>
</p:layout>
</h:body>
</f:view>
</html>
First
I use action="#{user.login()}" to make navigation action is this correct or there is a better pattern?
But the real question is: How to show a message If I redirect?
I like to show a message I know the example by PrimeFace http://www.primefaces.org/showcase/ui/dialogLogin.jsf. but it doesn't redirect or show anything.
But if i use only "plain" JSW without PrimeFace, i like to put a
because it is similar to PrimeFaces so switch is simple.
I'd like to adopt a "pattern" so i can reuse it for eg when i do a serch an no data is present, or when i call "erease DB" and application say work in progress an then say OK all bank account are esreased (Only an example!! but interesting because there are 2 message).
Thanks
I try
public String login() {
if (!(userName == null || password == null)
&& (userName.equals( name ) && userPassword.equals( password ))) {
isLogged = true;
return "main";
} else {
isLogged = false;
FacesMessage facesMsg;
facesMsg = new FacesMessage( FacesMessage.SEVERITY_ERROR, "No login", "No login because username or passsword are incorrect etc" );
FacesContext fc = FacesContext.getCurrentInstance();
fc.addMessage( "loginError", facesMsg );
return "index";
}
}
And edited the page
<h:body>
Login system
<br />
<!--<h:link outcome="welcomePrimefaces" value="Primefaces welcome page" />-->
<h:form>
User : <h:inputText value="#{user.name}" />
Password : <h:inputSecret value="#{user.password}" />
<h:commandButton action="#{user.login()}" value="Submit" />
<h:commandButton value="reset" type="reset" />
<h:commandButton value="Cambio Password" action="changePassword"></h:commandButton>
</h:form>
<h:message for="" style="color:red;margin:8px;"/>
</h:body>
It works but it's not ok because it's not ok to put a string in the bean. And I need the multilanguage this string has to generated by the beans.... mmm this is not OK something i'm missing.
There's extremely a lot of noise in the question. I understand that your question ultimately boils down to:
How do I create a localized faces message in bean action method?
In that case, just get the message from the current resource bundle yourself.
As you're talking about localization, you should surely already have something like as
<resource-bundle>
<base-name>com.example.i18n.text</base-name>
<var>text</var>
</resource-bundle>
in your faces-config.xml. You could just get it in the bean as well with help of ResourceBundle which JSF itself is actually also using under the covers
Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
ResourceBundle text = ResourceBundle.getBundle("com.example.i18n.text", locale);
This way you could compose your message as
String summary = text.getString("messages.no_login_summary");
String detail = text.getString("messages.no_login_detail");
new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, detail);

inputText validator doesn't display error message

I have a simple form that get code then display his libelle, I added a validator bean that check if the code exist.
My problem is I can't display the error message whith when the code doesn't exist.
Here is the code:
test.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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head><title>Test</title>
</h:head>
<body class="bodyMain">
<h:form>
<h:panelGrid columns="3">
<h:outputText value="Code: " />
<h:inputText id="code" value="#{myBean.code}"
validator="#{myBean.validateCode}">
<f:ajax execute="#this" render="libelle" listener="#{myBean.setLibelle()}"/>
</h:inputText>
<h:message for="code" style="color:red"/>
</h:panelGrid>
<h:panelGrid id="libelle" columns="2">
<h:outputText value="Libelle: " />
<h:outputText value="#{myBean.libelle}" />
</h:panelGrid>
</h:form>
</body>
</html>
MyBean.java
#ManagedBean
#ViewScoped
public class MyBean implements java.io.Serializable{
private static final long serialVersionUID = 1L;
private String code="";
private String libelle="";
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code=code;
}
public String getLibelle() {
return this.libelle;
}
public void setLibelle(String libelle) {
this.libelle=libelle;
}
public void setLibelle() {
if (code.compareTo("1")==0)
libelle="One";
else
libelle="";
}
public void validateCode(FacesContext context, UIComponent toValidate, Object value) throws ValidatorException {
String code = (String)value;
if (code.compareTo("1") != 0) {
FacesMessage message = new FacesMessage("Code doesn't exist");
throw new ValidatorException(message);
}
}
}
Thank you for your help to resolve this problem
You're not updating the <h:message> component by the <f:ajax>. You need to give the <h:message> an id and include it in the <f:ajax render>.
<h:inputText id="code" value="#{myBean.code}" validator="#{myBean.validateCode}">
<f:ajax execute="#this" render="libelle codeMessage" listener="#{myBean.setLibelle()}"/>
</h:inputText>
<h:message id="codeMessage" for="code" style="color:red"/>
Unrelated to the concrete problem, don't initialize properties to empty strings. Let them by default null. Also, comparing objects should be done with equals() method, not with compareTo(). Finally, using a dropdown list with all available values instead of an input field would be more user friendly.

When adding a faces message in JSF my actions aren't performed?

I have the following backing bean:
#ViewScoped
#ManagedBean
public class WeighFamilyBacking2 implements Serializable {
private static final long serialVersionUID = 1L;
private String[] children = new String[] { "Child1", "Child2", "Child3" };
private HashMap<String, Integer> newWeights;
public WeighFamilyBacking2() {
newWeights = new HashMap<String, Integer>();
for (String s : getChildren())
newWeights.put(s, new Integer(0));
}
public void distributeWeightsWithoutMessage(ActionEvent event) {
for (String s : newWeights.keySet()) {
newWeights.put(s, newWeights.get(s) + 1);
}
}
public void distributeWeights(ActionEvent event) {
for (String s : newWeights.keySet()) {
newWeights.put(s, newWeights.get(s) + 1);
}
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("Succesful", "Weights redistributed."));
}
public HashMap<String, Integer> getNewWeights() {
return newWeights;
}
public List<String> getChildren() {
return Arrays.asList(children);
}
}
... And the following xhtml page:
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<h:form>
<ui:repeat var="child" value="#{weighFamilyBacking2.children}">
<h:outputText value="#{child}" />
<h:outputText value="#{weighFamilyBacking2.newWeights[child]}" /> -
<h:outputText value="#{weighFamilyBacking2.newWeights[child]}" /> -
<h:inputText id="oz" value="#{weighFamilyBacking2.newWeights[child]}" />
<h:inputText id="lbs"
value="#{weighFamilyBacking2.newWeights[child]}" />
<br />
</ui:repeat>
<h:commandButton
actionListener="#{weighFamilyBacking2.distributeWeights}"
value="Redistribute" />
<h:commandButton
actionListener="#{weighFamilyBacking2.distributeWeightsWithoutMessage}"
value="Redistribute Without Message" />
</h:form>
</h:body>
</html>
This is a simple reproducible test case. When you click on the redistribute without message, things work as expected. When you click on the redistribute button it displays the success message but the input fields are not updated. However, the text output field is updated just one time.
I have tried using immediate=true on both buttons and that doesn't affect this. This is a very simple case, I can't understand why it doesn't work.
I have tried this with all recent versions of Mojarra including 2.1.3.
This is another <ui:repeat> anomaly. I haven't nail down the exact root cause yet so that I can check if this is already reported to the JSF guys and if necessary report it, but I can tell that it works when I replace the <ui:repeat> by a <h:dataTable>.
<h:dataTable var="child" value="#{weighFamilyBacking2.children}">
<h:column>
<h:outputText value="#{child}" />
<h:outputText value="#{weighFamilyBacking2.newWeights[child]}" /> -
<h:outputText value="#{weighFamilyBacking2.newWeights[child]}" /> -
<h:inputText id="oz" value="#{weighFamilyBacking2.newWeights[child]}" />
<h:inputText id="lbs"
value="#{weighFamilyBacking2.newWeights[child]}" />
</h:column>
</h:dataTable>
Maybe a <table> is not semantically correct for you. If this is really undesireable, you might want to check if it works without problems with Tomahawk's <t:dataList>, RichFaces' <rich:dataList>, PrimeFaces' <p:dataList>, etc each which supports rendering the children without additional markup.
Update: I reported it as issue 2157.

Resources