f:ajax will not render anything inside of an h:inputFile tag - ajax

I have been plagued with an issue for the past few days that I cannot solve. I am using Mojarra 2.2.12 on Wildfly 10.
My problem is that when I use an f:ajax tag inside of an h:inputFile tag and attempt to re-render any component, using either the component's ID, #form, #all, or anything else, nothing is re-rendered. Ever. However, it works absolutely fine within an h:commandButton or other components.
Here is a simple example.
I have an xhtml file called index.xhtml. This file contains an h:message, h:inputFile, and h:commandButton.
<!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:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</h:head>
<h:body>
<h:form id="test" enctype="multipart/form-data">
<h:message for="test" />
<br />
<h:inputFile value="#{uploadController.uploadedFile}">
<f:ajax render=":test" />
</h:inputFile>
<h:commandButton value="Push me!"
action="#{uploadController.buttonPushed}">
<f:ajax render=":test" />
</h:commandButton>
</h:form>
</h:body>
</html>
I also have a ManagedBean class called UploadController, which logs messages upon an action taking place, and is supposed to display a message inside of the h:message tag once the form is re-rendered.
#ManagedBean
#ViewScoped
public class UploadController {
private Part uploadedFile;
public Part getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(Part uploadedFile) {
this.uploadedFile = uploadedFile;
System.out.println("File Uploaded!");
FacesContext.getCurrentInstance().addMessage("test", new FacesMessage("File Uploaded!"));
}
public void buttonPushed()
{
System.out.println("Button pushed!");
FacesContext.getCurrentInstance().addMessage("test", new FacesMessage("Button Pushed!"));
}
}
The expected result is that when you select a file, it will "upload" the file and then display "File Uploaded!" in the h:message once the ajax re-renders the page. Then if you push the button, it will display "Button Pushed!" in that same h:message.
The actual result is that, yes, "File Uploaded." is printed to the console, but the page never re-renders and the message never appears. The button, on the other hand, works perfectly, and the message appears just like it should.
I have absolutely no idea what to do from here. I'm a beginner to JSF. Why does the ajax not re-render inside the h:inputFile, but it works perfectly fine in the h:commandButton? Is this a bug? How can I achieve the desired results without refreshing the page?

Related

Displaying validation Errors in form with Spring MVC

I've searched through several tutorials and answers to this forum to try to solve my problem: I want to show the validation errors from my bean in my form using spring MVC.
No matter what I try, I can't get it to work. Im not using redirections, my binding results are directly after the model class and so on.
Here's what I have so far:
Login Class:
public class LoginUser implements Serializable {
#NotNull
#Column(name="username", unique=true)
#Size(min=5)
private String username;
#NotNull
#Size(min=5)
private String password;
Login Controller:
#Transactional
#Controller
public class LoginController {
#Autowired
UserDao dao;
#RequestMapping(value = "enter", method = RequestMethod.POST)
public String doLogin(#ModelAttribute("user") #Valid LoginUser user, BindingResult result, HttpSession session) {
if (result.hasErrors()) {
return "login/loginForm";
} else {
if (dao.authenticate(user)) {
session.setAttribute("userLoggedIn", user.getUsername());
return "forward:index";
} else {
return "redirect:login";
}
}
}
Login Form:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="resources/js/bootstrap.js"></script>
<script type="text/javascript" src="resources/js/jquery-3.1.0.min.js"></script>
<link rel="stylesheet" href="resources/css/bootstrap.css">
<title><spring:message code="title.login" /></title>
</head>
<body>
<spring:message code="login.message.login" />
<form:form action="enter" commandName="loginForm" method="POST">
<spring:message code="username.login" />
<input type="text" name="username" id="username" /><br />
<form:errors path="username" />
<spring:message code="password.login" />
<input type="password" name="password" id="password" /><br /> <input
type="submit" value="<spring:message code='button.login'/>"><br />
<spring:message code="does.not.have.account.login" />
<spring:message code="register.link.login" />
</form:form>
</body>
</html>
Ohh, I forgot to add - I have a messages.properties configured (working fine, tested) and the messages are comming from there. Here is the line related to the form:
messages_en.Properties
NotEmpty.loginForm.username= Please fill the username field
By the way, it may be worth to note that my view is mounted trough a composite view (made of JSP includes of header, mainpage and footer) that overrides the customary viewloading in Sping-MVC.
You are using RedirectAttributes but you are not redirecting user any where. Try to use "redirect:/<register_path>" or just add Model model to method params and use model.addAtribute("someName", result);
Try below code..
<form:form action="enter" commandName="loginForm" method="POST">
commandName is loginForm so #ModelAttribute("loginForm").
#RequestMapping(value = "enter", method = RequestMethod.POST)
public String doLogin( #Valid #ModelAttribute("loginForm") LoginUser user, BindingResult result, Map<String, Object> model, HttpSession session) {
if (result.hasErrors()) {
return "login/loginForm";
} else {
if (dao.authenticate(user)) {
session.setAttribute("userLoggedIn", user.getUsername());
return "forward:index";
} else {
return "redirect:login";
}
}
}
UPDATE :
You are using NotEmpty.loginForm.username= Please fill the username field which is wrong,It should be NotNull.loginForm.username= Please fill the username field
And also
#NotNull will not validate for empty string..Request from the front end will be "" which is not null.So use #NotEmpty instead.
If you still want to use #NotNull add InitBinder in your controller as shown below
#InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
Then it will validate for empty string.
That is the reason you are getting size error directly and since you have not mentioned any message for that no message is displaying.

JSF- i login and when i navigate it automathically logout the session [duplicate]

This question already has answers here:
Difference between h:button and h:commandButton
(5 answers)
Closed 6 years ago.
Hello i am working with JSF2 and i am having some trouble with the session.
My problem is that i login and the user name appears like its logged but when i navigate to other page the name does not appear anymore. This started happening when i make the logout option. I have the login and logout methods in the same controller. And the text where its shows the name of the user and logout button is a facelet template, because i wanted it to be shown in all the pages that have that template.
this is the code for the login and the logout:
public String login(){
try {
user = ejb.Autenticar(this.mail, this.pass);
if(user != null){
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, Object> sessionMap = externalContext.getSessionMap();
sessionMap.put("USER", user);
return "LoginOK";
}
} catch (Exception e) {
return null;
}
return null;
}
public String logout(){
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "LoginView";
}
This is the code of the facelet template (The user name and the logout button are in the divSessionUser):
<?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:p="http://primefaces.org/ui">
<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="divSessionUser">
<span>
<b>Session user: </b>
<p:outputLabel value="#{loginControlador.user.name}"/>
</span>
<p:button value="Logout" outcome="#{loginControlador.logout()}"/>
</div>
<div id="top" class="top">
<h:graphicImage library="images" name="header1.png" styleClass="topImage"></h:graphicImage>
</div>
<div>
<div id="left">
<ul>
<li> <h:link outcome="indexView" value="Home" /></li>
<li> <h:link outcome="registroCView" value="Register Comunity" /></li>
<li> <h:link outcome="LoginView" value="Login" /></li>
<li> <h:link outcome="registroUserView" value="Register User" /></li>
</ul>
</div>
<div id="content" class="left_content">
<ui:insert name="content">Content</ui:insert>
</div>
</div>
</h:body>
I dont know if this is the correct way of doing this, but it got solved by inserting the div=divSessionUser of the facelet template inside a form and change the button for commandButton.
Hope it helps anyone, and if someone have a better answer please tell us!!!
Thanks!

Ajax call inside dataTable not working

I am trying to make ajax call inside jsf dataTable on checkobx value change.
But the corresponding backing bean is not getting fired.
When I tried the same thing outside dataTable, the ajax call is getting invoked and SOP's are getting printed.
Following is my code
<!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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
>
<h:body>
<h:head></h:head>
<section class="box">
<h:form>
<f:event type="preRenderView" listener="#{adminAccounts.getAppInfoNew()}" />
<div class="row-fluid">
<div class="span7" style="">
<span class="heading">Manage Accounts</span>
</div>
</div>
<!-- outside dataTable ajax call is working -->
<h:selectBooleanCheckbox checked="checked"
value="#{adminAccounts.testValue}">
<f:ajax listener="#{adminAccounts.changingTabVisiblity}"></f:ajax>
</h:selectBooleanCheckbox>
<h:dataTable id="diagnosis_list_2"
value="#{adminAccounts.appsList}" var="apps">
<h:column>
<f:facet name="header">Position</f:facet>
#{apps.appname}
</h:column>
<h:column>
<f:facet name="header">Campaign Management</f:facet>
<!-- inside dataTable ajax call is not working -->
<h:selectBooleanCheckbox checked="checked"
value="#{adminAccounts.testValue}">
<f:ajax listener="#{adminAccounts.changingTabVisiblity}"></f:ajax>
</h:selectBooleanCheckbox>
</h:column>
</h:dataTable>
</h:form>
</section>
</h:body>
</html>
Also following is the bean class
#ManagedBean(name = "adminAccounts")
#RequestScoped
public class AdminAccounts {
private List<AppDummyList> appsList = new ArrayList<AppDummyList>();
public boolean testValue;
public boolean testValue1;
public boolean isTestValue1() {
return testValue1;
}
public void setTestValue1(boolean testValue1) {
this.testValue1 = testValue1;
}
public boolean isTestValue() {
return testValue;
}
public void setTestValue(boolean testValue) {
this.testValue = testValue;
}
public List<AppDummyList> getAppsList() {
return appsList;
}
public void setAppsList(List<AppDummyList> appsList) {
this.appsList = appsList;
}
public void getAppInfoNew() {
System.out.println("get info called");
appsList.add(new AppDummyList("amazon"));
appsList.add(new AppDummyList("bookmyshow"));
appsList.add(new AppDummyList("flipkart"));
appsList.add(new AppDummyList("foodpanda"));
}
public void changingTabVisiblity() {
System.out.println("value=" + testValue);
System.out.println("value1=" + testValue1);
}
}
Need help on why the ajax call not working inside dataTable.

Proper use of Spring tag library in jsp

If I don't use <% # taglibprefix = "sf" uri = "http://www.springframework.org/tags/form"%>
the application works the same. The User user object is filled with the fields of the form. Is it correct to use this approach?
Is the use of <sf:form method="POST"modelAttribute="user"> more correct?
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Inserisci nuovo utente</title>
</head>
<body>
<h2>Dati utente</h2>
<form action="/SpringMVCFormHibernate/add" method="post">
<label>Cognome</label><br/><input type="text" name="cognome"/><br/>
<label>Nome</label><br/><input type="text" name="nome"/><br/>
<label>Eta</label><br/><input type="text" name="eta"/><br/><br/>
<input type="submit" value="submit"/>
</form>
<p>Visualizza utenti</p>
<sf:label path=""></sf:label>
</body>
</html>
#Controller
public class UtenteController {
#Autowired
UtenteDAO utenteDAO;
#RequestMapping(value="/add",method=RequestMethod.POST)
public String addUtente(#ModelAttribute Utente user){
utenteDAO.inserisciUtente(user);
return "index";
}//addUtente
}//UtenteController
The major use of spring:form tag is formbacking object . If you wish to bind the model attribute object with the view fields , you can go for it .
For simple form objects you can use html forms instead . Also you can make use of spring:form tags error attributes as well.
for ex,
path attribute binds the model field name . so changes made to them are can be easily updated in the server side with your model attribute.
simply they provide dyanamic binding of objects easily . spring does those instead of manual works
A nice example to understand form handling and model attribute usage.

Oracle ADF h:commandButton f:ajax not working

Trying to update a page through Ajax. Click a button and print out a counter on the page.
The following code works when deployed with JSF 2.0 mojarra on a Tomcat 7. It does not work when deployed from JDeveloper 11g to the built in Weblogic server. The count variable does get incremented, but the page is reloaded each time when using ADF.
Backing Bean:
import javax.faces.bean.*;
import javax.faces.event.ActionEvent;
#ManagedBean(name="countBean")
#SessionScoped
public class CountBean {
Integer count=1;
public void incrementCount(ActionEvent event) {
++count;
}
public Integer getCount() { return count;}
public void setCount(Integer count) { this.count = count; }
}
JSF-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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head><title>Ajax commandButton test</title></h:head>
<h:body>
<h3>Ajax count</h3>
<h:form>
<h:commandButton id="cb" value="Increment count"
actionListener="#{countBean.incrementCount}">
<f:ajax event="click" execute="cb" render="ot" />
</h:commandButton>
<br/><br/>
Counter = <h:outputText id="ot" value="#{countBean.count}"/>
</h:form>
</h:body>
</html>
I found the answer myself:
<af:commandButton text="increment count" id="cb1" actionListener="#{countBean.incrementCount}" partialSubmit="true"/>
Counter = <af:outputText id="ot" value="#{countBean.count}" partialTriggers="cb1"/>

Resources