Spring MVC with Facelets views - jsf

I have tried a dumb solution that use two "DispatcherServlets" to use facelets as a view technology for a Spring MVC web application, it was nice for a moment, facelets pages can get models from controllers, the problem is when a facelets page send a form object, here is how this web application is set up:
web.xml:
<servlet>
<servlet-name>Example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Example</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
With this configuration this controller's method is working fine:
#RequestMapping(value = "/garage", method = RequestMethod.GET)
public ModelAndView get(){
ModelAndView mav = new ModelAndView() ;
mav.setViewName("/faces/garage.xhtml") ;
mav.addObject("listOfCars", carService.getCarList()) ;
mav.addObject("hello", "Hello World From Spring Controller") ;
return mav ;
}
The problem in this application, is when submitting a form, the jsf tag , is always trying to resolve a "faces/*.xhtml" view.
solutions like this simply don't change anything.
<h:form>
<p>
<h:inputText
id="carMarque"
title="Quelle est la marque du nouveau véhicule"
value="#{NewCar.marque}">
</h:inputText>
<h:commandButton id="submit" value="Ajouter" action="/garage/new" />
</p>
</h:form>
It just ends up with a ServletException
javax.servlet.ServletException: /newcar.xhtml #22,50 value="#{NewCar.marque}": Target Unreachable, identifier 'NewCar' resolved to null
I don't know if there is a way to fix that up.

Related

Spring Security with JSF always return HTTP Status 404 when logging in

I'm kinda new to Spring Security, i tried to view some tutorials, but most of them are talking about Spring web mvc + spring security, or spring + spring security,
now i just want to add to my JSF application some role management, authentication and authorization which Spring Security provide very well.
Here is my login page:
<h:form>
<h:outputLabel value="username" for="j_username"
style="float:left" />
<h:inputText id="j_username" style="float:left" />
<h:outputLabel value="password" for="j_password"
style="float:left; clear:both" />
<h:inputSecret id="j_password" style="float:left" />
<h:commandButton value="login"
actionListener="#{loginController.login}" style="float:left;clear:both" />
</h:form>
my logging bean:
#Named(value = "loginController")
#SessionScoped
public class LoginController implements Serializable {
private String username;
private String password;
public String login() throws ServletException, IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.dispatch("/j_spring_security_check");
facesContext.responseComplete();
return null;
}
and for my spring security file:
<form-login login-page="/faces/login.xhtml" default-target-url="/faces/index.xhtml"
authentication-failure-url="/faces/login.xhtml?status=failed"/>
<logout logout-success-url="/faces/logout.xhtml"/>
</http>
and if for some reason someone wanted to look at my web.xml, here it is:
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-security.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
One last thing, can Spring Security prevent users from navigating to "/index.xhtml" (instead of "/faces/index.xhtml") ? or maybe redirect them to the "/faces/..." if it is missing?

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

PrimeFaces image upload [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 5 years ago.
I am trying to save the image file but the call is not going to the fileHandler method. We have already added filter to the web.xml as mentioned in the primefaces document. This demo I am trying. I also have commons-io and commons-fileupload. How can I correct it?
XHTML page
<h:form enctype="multipart/form-data">
<p:fileUpload
fileUploadListener="#{fileUploadController.handleFileUpload}"
mode="advanced" update="messages" label="Choose a file"
auto="true" sizeLimit="10485760"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
invalidSizeMessage="The maximum file size allowed is 10 Megabytes !"
invalidFileMessage="You are allowed to upload only images !" />
<p:growl id="messages" showDetail="true" life="5000" />
</h:form>
Bean
#ManagedBean(name = "topicUploadBean")
#RequestScoped
public class TopicUploadBean {
//Primitives
private static final int BUFFER_SIZE = 6124;
/** Create a new instance of UploadBean */
public TopicUploadBean(){
System.out.println("created FileUploadController instance");
}
public void handleFileUpload(FileUploadEvent event){
ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
File result = new File(extContext.getRealPath("//WEB-INF//files//"+event.getFile().getFileName()));
System.out.println(extContext.getRealPath("//WEB-INF//files//"+event.getFile().getFileName()));
try {
FileOutputStream fileOutputStream = new FileOutputStream(result);
byte[] buffer = new byte[BUFFER_SIZE];
int bulk;
InputStream inputStream = event.getFile().getInputstream();
while(true){
bulk = inputStream.read(buffer);
if (bulk < 0){
break;
}
fileOutputStream.write(buffer,0,bulk);
fileOutputStream.flush();
}
fileOutputStream.close();
inputStream.close();
FacesMessage msg = new FacesMessage("File Description", "file name: " + event.getFile().getFileName() +" File size: "+ event.getFile().getSize()/1024 +" Kb Content type: "+ event.getFile().getContentType() + " the file was uploaded.");
} catch(IOException e){
e.printStackTrace();
FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR, " the files were not uploaded!", "");
FacesContext.getCurrentInstance().addMessage(null, error);
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
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_0.xsd">
<display-name>emyed-web</display-name>
<context-param>
<param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
<param-value>1</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>javax.faces.SEPARATOR_CHAR</param-name>
<param-value>_</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>#{userPreferences.theme}</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.allowTextChildren</param-name>
<param-value>true</param-value>
</context-param>
<!-- Custom filter to check for user login -->
<filter>
<filter-name>EmyEdAccessFilter</filter-name>
<filter-class>com.zreflect.emyed.filter.EmyEdAccessFilter</filter-class>
</filter>
<!-- Filter for uploading files -->
<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>10240</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>EmyEdAccessFilter</filter-name>
<url-pattern>/pages/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</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> <servlet-name>Resource Servlet</servlet-name> <servlet-class>com.icesoft.faces.webapp.CompatResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup> </servlet -->
<servlet>
<servlet-name>ImageRenderServlet</servlet-name>
<servlet-class>com.zreflect.emyed.servlets.ImageRenderServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DownloadAttachmentServlet</servlet-name>
<servlet-class>com.zreflect.emyed.servlets.DownloadAttachmentServlet</servlet-class>
</servlet>
<!-- Atmos newly added -->
<servlet>
<description>MeteorServlet</description>
<servlet-name>MeteorServlet</servlet-name>
<servlet-class>org.atmosphere.cpr.MeteorServlet</servlet-class>
<init-param>
<param-name>org.atmosphere.servlet</param-name>
<param-value>com.zreflect.emyed.managedbean.circle.MeteorAtmosphereSharePostServlet</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.useNative</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MeteorServlet</servlet-name>
<url-pattern>/meteor</url-pattern>
</servlet-mapping>
<!-- Atmos done -->
<!--servlet> <servlet-name>Resource Servlet</servlet-name> <servlet-class>org.primefaces.resource.ResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Resource
Servlet</servlet-name> <url-pattern>/primefaces_resource/*</url-pattern>
</servlet-mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<!--url-pattern>*.iface</url-pattern -->
<url-pattern>*.jspx</url-pattern>
<url-pattern>/xmlhttp/*</url-pattern>
<url-pattern>*.faces</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ImageRenderServlet</servlet-name>
<url-pattern>/photo/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DownloadAttachmentServlet</servlet-name>
<url-pattern>/download/attachment/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>500</error-code>
<location>/errors/500.xhtml</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errors/500.xhtml</location>
</error-page>
<!-- error-page> <exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/errors/viewExpired.xhtml</location> </error-page -->
</web-app>
Because of your managed bean is "topicUploadBean", not "fileUploadController". Please check your beans, or also post your fileUploadController bean if you have.

p:fileUpload does not set uploaded file in backing bean [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 am facing a problem with <p:fileUpload> of PrimeFaces. I created a Facelet page to upload a file as below:
<h:form id="welcomeForm">
<p:fileUpload value="#{fileUploadController.uploadedFile}" mode="simple" />
<h:commandButton value="Submit" action="#{fileUploadController.submit}" />
<h:message for="welcomeForm" />
</h:form>
And a backing bean as below:
public class FileUploadController {
private UploadedFile uploadedFile;
public FileUploadController() {
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public void submit() {
// Get information you from the uploaded file
System.out.println("Uploaded file name : " + uploadedFile);
}
}
When I click the Submit button, the method submit() is called but it the result is as below :
INFO: Uploaded file name : null
How is this caused and how can I solve it?
Please read the <p:fileUpload> chapter of the PrimeFaces User Guide.
Getting started with FileUpload
First thing to do is to configure the fileupload filter which parses the multipart request. FileUpload filter should map to Faces 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>Faces Servlet</servlet-name>
</filter-mapping>
Simple File Upload
Simple file upload mode works in legacy mode with a file input whose value should be an
UploadedFile instance.
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{fileBean.file}" mode="simple" />
<p:commandButton value="Submit" ajax="false"/>
</h:form>
import org.primefaces.model.UploadedFile;
public class FileBean {
private UploadedFile file;
//getter-setter
}
Please note the enctype="multipart/form-data" attribute of the form. This is mandatory for HTML in order to be able to send files to the server. The filter is mandatory for JSF in order to extract the data from multipart/form-data requests. Without either of them, either the command action won't be invoked, or all properties will be null.
I think that the problem is that the simple upload doesn't support ajax.
you should add ajax="false":
<h:form id="welcomeForm">
<p:fileUpload value="#{fileUploadController.uploadedFile}" mode="simple" />
<h:commandButton value="Submit" action="#{fileUploadController.submit}" ajax="false" />
<h:message for="welcomeForm" />
</h:form>
Or use the primefaces autouploader.
I have the same issue, I have solved it by adding
<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>
like BalusC said.
but adding this :
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
Because by default in J2EE 6 this part is optional JSF 2.0 Servlet activates automatically when the WEB-INF/faces-config.xml descriptor is present.
But it necessary to active correctly PrimeFaces Filter
Jboss 6.1.0.Final / PrimeFaces 3.0.RC2
Try This:
<h:form id="welcomeForm" enctype="multipart/form-data">
<p:fileUpload value="#{fileUploadController.uploadedFile}" mode="simple" />
<h:commandButton value="Submit" action="#{fileUploadController.submit}" />
<h:message for="welcomeForm" />
</h:form>
(enctype="multipart/form-data" is very important for file upload.)
And:
public class FileUploadController {
private UploadedFile uploadedFile;
public FileUploadController() {
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public void submit() {
// Get information you from the uploaded file
System.out.println("Uploaded file name : " + uploadedFile.getFileName());
}
}
uploadedFile.getFileName() used to get the file name.
I had the same issue and just a very little THING saved me:
I did all the internet says about the prime faces and file upload thing
After applying the needed filter mappings and needed dependencies, I saw that dispacher information must also be added to the filter mappings:
<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>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
Maybe this very little configuration can save lives just like mine, good luck!

I have faces the javax.faces.el.EvaluationException: java.lang.NullPointerException

I am using jsf2.0 with tomcat7 in eclipse ide.I have just do a image cropping,when i crop the image and submit the form there is a error in my code.i dont know where i am struct.
My view page is:
<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.prime.com.tr/ui">
<h:head>
<script src="jquery-1.4.2.js"></script>
<script src="jquery.Jcrop.js"></script>
<script src="jquery.min.js"></script>
<script src="jquery.Jcrop.min.js"></script>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2">
<p:imageCropper value="#{imageCropperBean.croppedImage}"
image="/detroit-nights.jpg" />
<p:graphicImage id="localCroppedImage"
value="#{imageCropperBean.newFileName}.jpg" />
</h:panelGrid>
<p:commandButton value="Crop"
update="localCroppedImage" action="#{imageCrop.crop}"
/>
</h:form>
</h:body>
</html>
The Backing Bean is:
package bean;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.imageio.stream.FileImageOutputStream;
import javax.servlet.ServletContext;
import org.primefaces.model.CroppedImage;
#ManagedBean (name ="imageCrop")
#RequestScoped
public class ImageCropperBean {
private CroppedImage croppedImage;
private String newFileName;
public String getNewFileName() {
return newFileName;
}
public void setNewFileName(String newFileName) {
this.newFileName = newFileName;
}
public CroppedImage getCroppedImage() {
return croppedImage;
}
public void setCroppedImage(CroppedImage croppedImage) {
System.out.println("cRRRRRRRRRRRRR"+croppedImage);
this.croppedImage = croppedImage;
}
public String crop() {
System.out.println("WELCOMEMMMMMMMMMMMMMM");
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
newFileName = servletContext.getRealPath("") + File.separator +"croppedImage.jpg";
System.out.println("FILE ANE"+newFileName);
FileImageOutputStream imageOutput;
try {
//File file = new File(newFileName);
imageOutput = new FileImageOutputStream(new File(newFileName));
System.out.println("HHHHHHHHHH=="+imageOutput);
imageOutput.write(croppedImage.getBytes(), 0, croppedImage.getBytes().length);
imageOutput.close();
} catch (FileNotFoundException e) {
System.out.println("NOT FOUND EXCEPTION"+e);
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOEXCEPTION"+e);
e.printStackTrace();
}
//return newFileName;
return null;
}
}
The web.xml is:
<?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">
<context-param>
<description>Context param for JSTL 1.2 to work in Tomcat 6 sun RI
</description>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
<context-param>
<description>Parameter required by Mojarra 2.0</description>
<param-name>com.sun.faces.allowTextChildren</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
<param-value>true</param-value>
</context-param>
<servlet>
<servlet-name>Resource Servlet</servlet-name>
<servlet-class>
org.primefaces.resource.ResourceServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resource Servlet</servlet-name>
<url-pattern>/primefaces_resource/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<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>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>/temp</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>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>image.xhtml</welcome-file>
</welcome-file-list>
</web-app>
Your references to the managed bean are wrong. Use the name as written in the name attribute of the #ManagedBean annotation.
So replace:
<p:imageCropper value="#{imageCropperBean.croppedImage}"
image="/detroit-nights.jpg" />
<p:graphicImage id="localCroppedImage"
value="#{imageCropperBean.newFileName}.jpg" />
with
<p:imageCropper value="#{imageCrop.croppedImage}"
image="/detroit-nights.jpg" />
<p:graphicImage id="localCroppedImage"
value="#{imageCrop.newFileName}.jpg" />
(You did it correctly for the p:commandButton).

Resources