doesn't show up bean's attributes in .xhtml [duplicate] - jsf

This question already has an answer here:
JSF returns blank/unparsed page with plain/raw XHTML/XML/EL source instead of rendered HTML output
(1 answer)
Closed 8 years ago.
I started to learn JSF. I use netbeans and glass fish as server. I have this problem:
my code of start.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en"
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>
<h:outputStylesheet library="css" name="default.css"/>
<title>some text</title>
</h:head>
<h:body>
<h:graphicImage library="images" name="wave.med.gif"
alt="duke waving"/>
<h2>
hi #{user.minimum} a #{user.maximum} .
</h2>
</h:body>
code of User.java:
#Named(value = "user")
#SessionScoped
public class User implements Serializable {
private long minimum=0;
private long maximum=10;
public User() {
}
public long getMinimum(){ return (this.minimum);}
public long getMaximum(){ return (this.maximum);}
public void setMaximum(long m){ this.maximum=m;}
public void setMinimum(long m){ this.minimum=m;}
}
code of web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>start.xhtml</welcome-file>
</welcome-file-list>
</web-app>
when I deploy the app I get this message only:
hi #{user.minimum} a #{user.maximum}.
Instead of a picture: wave.med.gif and message:
hi 0 a 10.
where could be the problem ?

I guess the problem is that your JSF code isn't evaluated.
You can prove this by looking at the source code of the generated HTML, if it is the same as the code in your start.xhtml, the code isn't evaluated.
To fix this, change the servlet-mapping in your web.xml to the following:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
and access the page via http://localhost:8080/YOUR_WEBAPP/start.xhtml

Related

JSF tags not executed

I just started to work with JSF and trying some example implementation.
I got the following Bean, web.xml and index.xhtml files:
HelloBean.java:
package jsflearning;
import javax.inject.Named;
import javax.enterprise.context.*;
#Named(value= "myHelloBean")
#RequestScoped
public class HelloBean {
private String name = "dear reader";
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
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-app_3_1.xsd">
<display-name>JSFLearning</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.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
index.xhtml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<head>
<title>Simplest JSF Page</title>
</head>
<body>
<div align="center">
<br />
<h:form>
<h:inputText value="#{myHelloBean.name}"/>
</h:form>
<br />
Hello to you. #{myHelloBean.name} !
</div>
</body>
</html>
The #{myHelloBean.name} expression is correctly executed but the <h:form>tag is not interpreted and no editbox displayed. Can't find the solution for this problem.
Thanks in advance!
The http://xmlns.jcp.org/jsf/* XML namespace is only supported since JSF 2.2. Your problem symptoms suggest that you're still using JSF 2.0 or 2.1.
You've 2 options:
Upgrade JSF libraries to a JSF 2.2 compatible version.
Downgrade XML namespace to a JSF 2.0/2.1 compatible http://java.sun.com/jsf/*.

JSF not loading managed beans

I got a simple demo project with embedded Jetty and JSF (Primefaces). The problem is that my beans are not loaded at all.
/src/main/java/HelloBean.java
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
#ManagedBean(eager=true)
#ApplicationScoped
public class HelloBean implements Serializable {
private static final long serialVersionUID = 1L;
...
#PreDestroy
public void preDestroy(){
System.out.println("PRE DESTROY");
}
#PostConstruct
public void postConstruct(){
System.out.println("POST CONSTRUCT");
}
public HelloBean() {
System.out.println("Hello Bean instantiated");
}
...
}
/src/main/webapp/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: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:head>
<title>Conti Diagnose</title>
</h:head>
<h:body>
<h:outputText value="#{helloBean.name}"/>
...
/src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<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>primefaces.THEME</param-name>
<param-value>dark-hive</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
/src/main/webapp/WEB-INF/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">
<application>
<locale-config>
<default-locale>de</default-locale>
</locale-config>
<resource-bundle>
<base-name>lang.lang</base-name>
<var>msg</var>
</resource-bundle>
</application>
</faces-config>
The output is
2014-10-30 10:13:11.525:INFO::main: Logging initialized #164ms
2014-10-30 10:13:11.633:INFO:oejs.Server:main: jetty-9.3.0.M0
Okt 30, 2014 10:13:11 AM com.sun.faces.config.ConfigureListener contextInitialized
INFORMATION: Mojarra 2.2.8-02 ( 20140915-1602 https://svn.java.net/svn/mojarra~svn/tags/2.2.8-02#13678) für Kontext '/ui' wird initialisiert.
Okt 30, 2014 10:13:12 AM com.sun.faces.spi.InjectionProviderFactory createInstance
INFORMATION: JSF1048: PostConstruct/PreDestroy-Annotationen vorhanden. Verwaltete Bean-Methoden, die mit diesen Annotationen markiert sind, lassen die entsprechenden Annotationen verarbeiten.
Okt 30, 2014 10:13:13 AM com.sun.faces.config.ConfigureListener$WebConfigResourceMonitor$Monitor <init>
INFORMATION: Monitoring file:/C:/.../src/main/webapp/WEB-INF/faces-config.xml for modifications
Okt 30, 2014 10:13:13 AM org.primefaces.webapp.PostConstructApplicationEventListener processEvent
INFORMATION: Running on PrimeFaces 5.1
2014-10-30 10:13:13.157:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext#538540a3{/ui,[file:///.../target/classes/../../src/main/webapp/, file:///.../target/],AVAILABLE}
2014-10-30 10:13:13.221:INFO:oejs.ServerConnector:main: Started ServerConnector#5aa85b18{HTTP/1.1}{localhost:8080}
2014-10-30 10:13:13.222:INFO:oejs.Server:main: Started #1864ms
The class to start the embedded server is /src/main/JettyRunner.java. The main method is:
InetSocketAddress address = InetSocketAddress.createUnresolved("localhost", 8080);
Server server = new Server(address);
WebAppContext wac = new WebAppContext();
wac.setContextPath("/ui");
wac.setWelcomeFiles(new String[] { "index.xhtml" });
String webappDir=JettyRunner.class.getClassLoader().getResource(".").toString();
wac.setBaseResource(new ResourceCollection(new String[] {
webappDir+"../../src/main/webapp","./target" }));
server.setHandler(wac);
server.setStopAtShutdown(true);
server.start();
server.join();
The messages in postConstruct or the Constructor are not printed at all. Accessing a property of this bean in my xhtml gives no error and just prints nothing.
Even if I access a bean that does not exist, there's no error message.
Why are my beans not loaded?
After three days I finally found the answer myself:
Just added the line
wac.setResourceAlias("/WEB-INF/classes/", "/classes/");
to my runner class. Now everything works as expected.
Try adding the #Named annotation to your bean. This annotation makes the managed bean accessible through the EL. Then change
<h:outputText value="HelloBean.name"/>
to
<h:outputText value="#{HelloBean.name}"/>
in /src/main/webapp/index.xhtml

Primefaces fileUploadListener not working [duplicate]

This question already has answers here:
How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable
(11 answers)
Closed 7 years ago.
I tried to upload a file with primefaces into my tomcat7 server. I'm using primfaces4. file upload listener doesn't call handleFileUpload and hiii not apeared in console
my bean is this:
package Pin;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import org.primefaces.component.fileupload.FileUpload;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import Util.U;
#ManagedBean(name="pinBean")
#SessionScoped
public class PinBean{
private UploadedFile file;
public PinBean(){
U.wl("Start");
}
public UploadedFile getFile() {
U.wl("get");
return file;
}
public void setFile(UploadedFile file) {
U.wl("set");
this.file = file;
}
public void handleFileUpload(FileUploadEvent event) {
UploadedFile file = event.getFile();
U.wl("hiii");
//application code
}
}
and my xhtml is:
<?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:t="http://myfaces.apache.org/tomahawk"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:p="http://primefaces.org/ui">
<h:form>
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{pinBean.handleFileUpload}" auto="true" mode="advanced"/>
</h:form>
</h:form>
</html>
and web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>testUpload</display-name>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>2097152</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
</web-app>
Finaly it worked, I used simple type of upload.
Important thing:
1- <h:head></h:head> was necessary
2- this is necessary in web.xml:
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
3- in h:form it's important to write enctype="multipart/form-data"
4- ajax="false" for the command button is necessary
I used the simple upload type:
<?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:t="http://myfaces.apache.org/tomahawk"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{pinBean.file}" mode="simple" />
<p:commandButton value="Submit" ajax="false"/>
</h:form>
</html>
and my bean is:
package Pin;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import org.primefaces.component.fileupload.FileUpload;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import Util.U;
#ManagedBean(name="pinBean")
#SessionScoped
public class PinBean{
private UploadedFile file;
public PinBean(){
U.wl("Start");
}
public UploadedFile getFile() {
U.wl("get");
return file;
}
public void setFile(UploadedFile file) {
U.wl("set");
this.file = file;
}
}
and my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>WebOffice</display-name>
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<description>
This parameter tells MyFaces if javascript code should be allowed in
the rendered HTML output.
If javascript is allowed, command_link anchors will have javascript code
that submits the corresponding form.
If javascript is not allowed, the state saving info and nested parameters
will be added as url parameters.
Default is 'true'</description>
<param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<description>
If true, rendered HTML code will be formatted, so that it is 'human-readable'
i.e. additional line separators and whitespace will be written, that do not
influence the HTML code.
Default is 'true'</description>
<param-name>org.apache.myfaces.PRETTY_HTML</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<description>
If true, a javascript function will be rendered that is able to restore the
former vertical scroll on every request. Convenient feature if you have pages
with long lists and you do not want the browser page to always jump to the top
if you trigger a link or button action that stays on the same page.
Default is 'false'
</description>
<param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<context-param>
<param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name>
<param-value>false</param-value>
</context-param>
<filter>
<filter-name>Filters</filter-name>
<filter-class>UserManagement.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Filters</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<error-code>404</error-code>
<location>/not_exist.jsf</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/exception.jsf</location>
</error-page>
</web-app>
Do this because I also got same problem before
public void handleFileUpload(FileUploadEvent event) {
System.out.println("calling file upload...");
File targetFolder = new File(Properties.File_Uploaded_path
+ File.separator);
if (!targetFolder.exists()) {
targetFolder.mkdirs();
}
try {
InputStream inputStream = event.getFile().getInputstream();
OutputStream out = new FileOutputStream(new File(targetFolder,
event.getFile().getFileName()));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
inputStream.close();
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("file upload after catch..");
employeeFileUploadPaths[employeeFileCount] = targetFolder
.getAbsolutePath()
+ File.separator
+ event.getFile().getFileName();
System.out.println("empFileUploadPaths[check]"
+ employeeFileUploadPaths[employeeFileCount]);
employeeFileCount++;
FacesMessage msg = new FacesMessage("Succesful", event.getFile()
.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
System.out.println("last line of file upload....");
}
And In Xhtml page:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form enctype="multipart-data">
<p:fileUpload fileUploadListener="#{employeeBean.handleFileUpload}"
required="true" mode="advanced" dragDropSupport="false"
multiple="true" sizeLimit="1000000" fileLimit="5" update="messages"
allowTypes="/(\.|\/)(gif|jpe?g|png|pdf|doc|docx)$/">
<p:growl id="messages" showDetail="true" />
</p:fileUpload>
<f:facet name="footer">
<p:commandButton value="Add" ajax="false"
action="#{employeeBean.addEmployee}">
</p:commandButton>
</f:facet>
</p:panelGrid>
</f:view>
</h:form>
</h:body>
And Add this in pom.xml Dont remove Primefaces 4.0 Dependency
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0</version>
</dependency>

Non functioning Primefaces (or myfaces) fileUpload when using JSF templates

I'm having trouble with fileUpload and JSF2's templating mechanism. I've searched widely and there are other's with similar problems (eg here and here, but my problem seems to be directly related to my use of the templating mechanism as it works OK outside of that mechanism.
I have an xhtml form that I insert into a template using <ui:insert> in the template and <ui:composition / <ui:define> in the form. The Listener on the bean for the file upload is never called. However, if I mod the form so as to be capable of being a standalone page, then the upload works OK. So, apparently, something to to with the templating process is changing my results.
I know that using the templating mechanism with the code fragments below results in two nested forms, but I've tried removing one and there's no change. The resulting HTML looks sensible in the browser, so what could be going on?
My xhtml form...
<?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"
xmlns:myfaces="http://myfaces.apache.org/tomahawk"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:composition template="MJLSConsole.xhtml">
<ui:param name="title" value="Sender Info" />
<ui:define name="content">
<h:form id="senderInfo">
<h:panelGrid columns="2" >
<p:outputLabel for="imageId" id="icon" value="Organisation Icon"/>
<p:fileUpload id="imageId" value="#{senderInformationBean.file}" required="true" mode="simple"/>
</h:panelGrid>
<p:commandButton value="Save" action="#{senderInformationBean.save}" ajax="false" />
</h:form>
</ui:define>
</ui:composition>
</html>
and here's my 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"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</h:head>
<h:body>
<h:form id="testpage" enctype="multipart/form-data">
<p:growl id="messages" sticky="true" autoUpdate="true" />
<ui:insert name="content">Content</ui:insert>
</h:form>
</h:body>
</html>
and my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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-app_2_5.xsd">
<display-name>Mjls_main</display-name>
<context-param>
<description>Specifies the root of the spring application context</description>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/cxf-beans.xml</param-value>
</context-param>
<listener>
<description>Starts spring application context</description>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<description>Bridges http requests to ContextLoaderListener</description>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<description>JSF server</description>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>facesServlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>MJActionService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>MJEnvelopeService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>MJActionEventService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>SenderInformationBlockService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>MJTemplateService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>MJmlService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Notification_Portal_SignallingService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>MJInboundMessageService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>MjogServiceImplService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet>
<description>Provides soap services via Apache CXF</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<filter>
<description>Provides an ACL and role-based checks for associated services</description>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>restrictAccessFilter</filter-name>
<filter-class>com.mjog.mjls.filter.RestrictAccessFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>restrictAccessFilter</filter-name>
<url-pattern>/console/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>mjlsLogin.jsf</welcome-file>
</welcome-file-list>
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
<init-param>
<param-name>uploadMaxFileSize</param-name>
<param-value>20m</param-value>
</init-param>
</filter>
<!-- extension mapping for adding <script/>, <link/>, and other resource tags to JSF-pages -->
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<!-- servlet-name must match the name of your javax.faces.webapp.FacesServlet entry -->
<servlet-name>facesServlet</servlet-name>
</filter-mapping>
<!-- extension mapping for serving page-independent resources (javascript, stylesheets, images, etc.) -->
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<url-pattern>/faces/myFacesExtensionResource/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>MJActionService</servlet-name>
<url-pattern>/MJActionService</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MJEnvelopeService</servlet-name>
<url-pattern>/MJEnvelopeService</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MJActionEventService</servlet-name>
<url-pattern>/MJActionEventService</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SenderInformationBlockService</servlet-name>
<url-pattern>/SenderInformationBlockService</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MJTemplateService</servlet-name>
<url-pattern>/MJTemplateService</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MJmlService</servlet-name>
<url-pattern>/MJmlService</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Notification_Portal_SignallingService</servlet-name>
<url-pattern>/Notification_Portal_SignallingService</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MJInboundMessageService</servlet-name>
<url-pattern>/MJInboundMessageService</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MjogServiceImplService</servlet-name>
<url-pattern>/MjogServiceImplService</url-pattern>
</servlet-mapping>
<session-config>
<!-- logs inactive web users out after n minutes-->
<session-timeout>20</session-timeout>
</session-config>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>mjog</param-value>
</context-param>
<servlet>
<servlet-name>imageServlet</servlet-name>
<servlet-class>com.mjog.mjls.filter.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/file/*</url-pattern>
</servlet-mapping>
</web-app>
and finally my bean is
#ManagedBean(name = "senderInformationBean")
public class SenderInformationBean implements Serializable {
transient private ConsoleService consoleService;
private UploadedFile file;
public void setConsoleService(ConsoleService consoleService) {
this.consoleService = consoleService;
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public String save() throws IOException {
consoleService.save(accountName, contactAddress, contactNumber, emailId, accountUrl, file.getBytes());
populateSenderInfo();
return "/console/senderInformation.xhtml?faces-redirect=true";
}
}
After inserting the template definitions, your component tree effectively ends up like:
<h:body>
<h:form id="testpage" enctype="multipart/form-data">
<p:growl id="messages" sticky="true" autoUpdate="true" />
<h:form id="senderInfo">
<h:panelGrid columns="2" >
<p:outputLabel for="imageId" id="icon" value="Organisation Icon"/>
<p:fileUpload id="imageId" value="#{senderInformationBean.file}" required="true" mode="simple"/>
</h:panelGrid>
<p:commandButton value="Save" action="#{senderInformationBean.save}" ajax="false" />
</h:form>
</h:form>
</h:body>
Look, you end up with a nested form. This is illegal in HTML. The actual behavior on submitting of such a nested form is unspecified and dependent on the browser used. Get rid of the <h:form id="testpage"> and set the enctype on <h:form id="senderInfo"> in such way that your component tree effectively ends up like:
<h:body>
<p:growl id="messages" sticky="true" autoUpdate="true" />
<h:form id="senderInfo" enctype="multipart/form-data">
<h:panelGrid columns="2" >
<p:outputLabel for="imageId" id="icon" value="Organisation Icon"/>
<p:fileUpload id="imageId" value="#{senderInformationBean.file}" required="true" mode="simple"/>
</h:panelGrid>
<p:commandButton value="Save" action="#{senderInformationBean.save}" ajax="false" />
</h:form>
</h:body>
See also:
commandButton/commandLink/ajax action/listener method not invoked or input value not updated - point 2

ICEfaces v2.0.0 Internationalization problem - form is not redisplayed upon action listener call

I am experiencing problems with localization.
i am using
JSF 2.0 Mojarra (xhtml not jsp) (2.02 - FCS)
IceFaces Core 2.0.0 - beta1
IceFaces Compatibility Library v2.0.0. - beta1
Here is the sample of the 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:h="http://java.sun.com/jsf/html"
xmlns:ice="http://www.icesoft.com/icefaces/component">
<h:head>
<title>"#{msgs.pageTitle}"</title>
</h:head>
<h:body>
<h:form>
<br />
<div align="center"><h:commandButton
value="#{msgs.serbianLatinAlphabetName}"
actionListener="#{formSettings.swapLocale1}" immediate="true" /> <h:commandButton
value="#{msgs.serbianChyrilicAlphabetName}"
actionListener="#{formSettings.swapLocale1}" immediate="true" /><ice:commandButton
value="#{msgs.pageTitle}"
actionListener="#{formSettings.swapLocale1}" immediate="true"/></div>
</h:form>
</h:body>
</html>
and managed bean:
import java.io.*;
import java.util.*;
import javax.faces.bean.*;
import javax.faces.component.UIViewRoot;
import javax.faces.context.*;
import javax.faces.event.*;
#ManagedBean
#SessionScoped
public class FormSettings implements Serializable {
private boolean isDefault = true;
private Locale locale = new Locale("sr");
public void swapLocale1(ActionEvent event) {
switchLocale();
}
private void switchLocale() {
isDefault = !isDefault;
if (isDefault) {
locale = new Locale("sr_ME");
} else {
locale = new Locale("sr");
}
//FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot myViewRoot = context.getViewRoot();
myViewRoot.setLocale(locale);
}
public Locale getLocale() {
return locale;
}
public void swapLocale2(ValueChangeEvent event) {
Boolean flag = (Boolean)event.getNewValue();
if (flag) {
switchLocale();
}
}
public boolean isChecked() {
return(false);
}
public void setChecked(boolean flag) {}
}
my web.xml is as follows:
<?xml version="1.0" encoding="ASCII"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>WePaminus</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Resource Servlet</servlet-name>
<servlet-class>com.icesoft.faces.webapp.CompatResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Resource Servlet</servlet-name>
<url-pattern>/xmlhttp/*</url-pattern>
</servlet-mapping>
</web-app>
and 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">
<application>
<locale-config>
<default-locale>sr</default-locale>
<supported-locale>sr_ME</supported-locale>
</locale-config>
<resource-bundle>
<base-name>messages</base-name>
<var>msgs</var>
</resource-bundle>
</application>
</faces-config>
The problem is that upon the click on the button, the locale is not changed. Upon manual refresh, the correct locale has been shown.
Could you please help me with this. Have to say that the same page, implemented in pure JSF 2.0 (icefaces excluded) is working perfectly.
Thanks
You need to re-render the entire page or better, fire a synchronous request instead of an asynchronous (ajaxical) request which would only render the page partially.

Resources