PrimeFaces Upload file not uploading - upload

I am using PrimeFaces 3 and trying to upload a file but when I debug the file is always null.
Below you can see my code. Can anyone spot what is the issue?
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{uploadFileMB.file}" mode="simple" />
<p:commandButton value="Submit" ajax="false" action="#{uploadFileMB.submit()}"/>
<h:outputLabel value="#{uploadFileMB.text}" />
</h:form>
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.primefaces.model.UploadedFile;
#ManagedBean
#SessionScoped
public class UploadFileMB {
UploadedFile file;
String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void submit(){
System.out.println("Trial "+file);
UploadedFile a=file;
if(file==null)
text="not uploaded";
else
text=file.getFileName()+" uploaded";
}
/** Creates a new instance of UploadFileMB */
public UploadFileMB() {
}
}
<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>
And the filter in both web.xml and faces.config
I have tried a number of suggestions and debugged it many times but I can't figure it out.
This is my faces-config:
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config version="2.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-facesconfig_2_0.xsd"
>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId> commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId> commons-io</artifactId>
<version>2.1</version>
</dependency>
<lifecycle>
<phase-listener>security.SecurityFilter</phase-listener>
</lifecycle>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>C:/home/vanessa/Desktop</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
</faces-config>

I think these two libraries are missing in your project ; commons-fileupload and commons-io . if your project is maven you can add these to your pom.xml ;
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId> commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId> commons-io</artifactId>
<version>2.1</version>
</dependency>
else then download them from http://commons.apache.org and add your lib .

Well, I see three mistakes in your code, it could solve your problem I don't guarantee anything.
First, you are importing #SessionScope from wrong package, it should be javax.faces.bean.SessionScoped, the other class is ment to use with CDI.
Second, make your properties in bean private I am not sure if it counts as a property like this. Also this is a good practise to hiding field as much as possible.
Third, and most important, change actionto actionListenerand try it. If still doesn't work, try to add to your method parameter ActionEvent event (and be carefull to choose right package, I once imported ActionEventfrom javax.awt. and spend two hours figuring out where could be the problem:-)

Related

can we move some swagger configuration from web.xml

In swagger 1.2.9-1.2.3 or old versions we have config reader com.wordnik.swagger.jaxrs.ConfigReader class, we can extend this class and we can declare swagger properties swagger.api.basepath , api.version , swagger.version etc.
But in current version of swagger 2.10-1.3.0 this class is not present. Is there any way we can move above configurations from web.xml, I want to have them in property file instead of hard coding it in web.xml.
Thanks in advance.
There's a thread explaining how to do this on the Swagger google group.
Basically, in Swagger 1.3, you need to use the SwaggerConfig class, like so:
SwaggerConfig config = new SwaggerConfig();
config.setBasePath(yourBasePathVariable);
ConfigFactory.setConfig(config);
However, you need this to occur after Swagger loads and sets the default basePath, because otherwise (if your basePath gets set first) it will be overwritten.
Here is how you can move swagger configuration to a customization code
This is particularly helpful if you are hosting your micro service on cloud foundry or IBM Bluemix or Some PaaS Cloud solution
Dependency in your pom.xml
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
Swagger configuration
package com.ibm.api;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.config.DefaultJaxrsConfig;
public class SwaggerConfigReader extends DefaultJaxrsConfig {
/**
*
*/
private static final long serialVersionUID = 1638783798880874518L;
#Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
//contextPath will be null for host2 and /xyz for host1.
String contextPath = config.getServletContext().getContextPath();
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.0");
beanConfig.setTitle(Result.IMPLEMENTATION + " API Documentation");
beanConfig.setSchemes(new String[] {
"http", "https"
});
beanConfig
.setResourcePackage("com.ibm.api");
beanConfig.setBasePath(contextPath + "/rest");
beanConfig.setScan(true);
}
}
Web.xml entries
<web-app id="WebApp_ID" version="2.4"
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">
<display-name>Restful Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>io.swagger.jaxrs.listing,com.ibm.api</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SwaggerBootstrap</servlet-name>
<servlet-class>com.ibm.api.SwaggerConfigReader</servlet-class>
<init-param>
<param-name>scan.all.resources</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
</web-app>
Note the com.ibm.api.SwaggerConfigReader registered as swagger configuration and com.ibm.api registered in packages to be scanned for rest APIs. Also note beanConfig.setBasePath(contextPath + "/rest");
Now your swagger.JSON configuration will show up as http://localhost:8080/jax_rs/rest/swagger.json. Point swagger UI to this URL and you will see swagger documentation.
The code is here: https://github.com/sanketsw/jax_rs_REST_Example

Tiles Integration with Struts 2 Annotation

I have been trying to integrate Tiles with Struts 2 annotation based action but it's not working correctly.
As I don't have struts-config.xml and in every tutorial available at web they are referencing it with struts-config.xml.
First is it possible to integrate annotation based struts action with tiles. If yes then how?
#Action(value="/login",results={#Result(name="success",location="/home",type=TilesResult.class),
#Result(name="login",location="/jsp/userLogin.jsp")})
public String execute() {
This is what my code is but it always gives me error in MyEclipse at TilesResult.class that
Type mismatch: cannot convert from Class<TilesResult> to String
I have dependency in my pom:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-tiles-plugin</artifactId>
<version>2.1.8</version>
</dependency>
Can anyone help me how to add tiles in annotation based actions
I used type="tiles" instead of type=TilesResult.class then it has given me below exception
Caused by: The Result type [tiles] which is defined in the Result annotation on the class [class com.actions.LoginAction] or determined by the file extension or is the default result type for the PackageConfig of the action, could not be found as a result-type defined for the Struts/XWork package [com.actions#convention-default#] - [unknown location]
at org.apache.struts2.convention.DefaultResultMapBuilder.createResultConfig(DefaultResultMapBuilder.java:422)
at org.apache.struts2.convention.DefaultResultMapBuilder.createFromAnnotations(DefaultResultMapBuilder.java:394)
at org.apache.struts2.convention.DefaultResultMapBuilder.build(DefaultResultMapBuilder.java:202)
at org.apache.struts2.convention.PackageBasedActionConfigBuilder.createActionConfig(PackageBasedActionConfigBuilder.java:800)
at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildConfiguration(PackageBasedActionConfigBuilder.java:586)
at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildActionConfigs(PackageBasedActionConfigBuilder.java:318)
at org.apache.struts2.convention.ClasspathPackageProvider.loadPackages(ClasspathPackageProvider.java:53)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:204)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)
Try these :
Use type="tiles" instead of type="TilesResult.class"
Use your target tile definition, location="tiles-definition-name", instead of JSP page, location="/jsp/userLogin.jsp", in your result location
Have following in your web.xml:
<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
Have following in your struts.xml (If you are using annotations alone and no struts.xml, then you have to create a minimal one for this because there's no annotation available to define a custom result type)
<struts>
<constant name="struts.convention.default.parent.package" value="codeoftheday.blogspot.com"/>
<package name="codeoftheday.blogspot.com" extends="struts-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
</package>
</struts>
NOTE: I've written a detailed blog post here on this issue - Maven, Struts2 Annotations and Tiles Integration Example via Convention / Codebehind / Zero Config plugin using Eclipse IDE
"org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG" this definition is not available with Strut 2.5.10.1
I have used following jars in my project.
struts2-core-2.5.10.1
struts2-convention-plugin-2.5.10.1
struts2-tiles-plugin-2.5.10.1
javax.servlet-api-3.0.1
Please once you compare your web.xml with following code.
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.demo.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<!-- <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name> -->
<param-name>org.apache.tiles.definition.DefinitionsFactory.DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/config/tiles.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>

primefaces 3.5 uploadfile not working, i using netbean 7.2.1

the problem is with file uploader from primefaces
web.xml
<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>
.xhtml
</h:form>
...
</h:form>
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{contratosMB.fileContrato}" mode="simple"/>
<p:commandButton value="Submit" ajax="false"
actionListener="#{contratosMB.upload}" update=":form2:formgen:growl"/>
</h:form>
contratosMB.java
public void upload() {
if(fileContrato != null) {
JsfUtil.addSuccessMessage("Se ha cargado correctamente el archivo: " + fileContrato.getFileName());
}
}
I was read some question like this, but nothing was help me...
i was add commons-fileupload and commons-io to the project,
but dont work not go inside upload method, dont send exception.
Try changing
actionListener="#{contratosMB.upload}"
to
action="#{contratosMB.upload}" .
Check this out :
Differences between action and actionListener
it working creating and editing faces-config:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId> commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId> commons-io</artifactId>
<version>1.4</version>
</dependency>
but dont work full for me because form uploader is inside to other form...
<h:form>
...
<h:form enctype="multipart/form-data">
<p:messages showDetail="true"/>
<p:fileUpload value="#{contratosMB.fileContrato}" mode="simple"/>
<p:commandButton value="Submit" ajax="false"
actionListener="#{contratosMB.upload}"/>
</h:form>
</h:form>
any body can tell me why??
Nesting 'form' elements is illegal. Try to upload file in advanced mode and use FileUploadListener to access FileUploadEvent and data.

Spring PageNotFound and URLMapping in web.xml

I have been reading a lot of questions like this, but I'm not getting out of the trouble, and I'm starting to think that the problem is tomcat playing around.
First of all, here are my web.xml, my *-servlet.xml and my controller.
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>Spring REST Server</display-name>
<description>Spring REST Server</description>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:env/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/applicationContext.xml
classpath:config/kimboo-servlet.xml
</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>Kimboo</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>kimboo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/applicationContext.xml
classpath:config/kimboo-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>kimboo</servlet-name>
<url-pattern>/home/</url-pattern>
</servlet-mapping>
</web-app>
This is my kimboo-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="ar.com.kimboo.server.ui.controller" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/AppClient/pages/" />
<property name="suffix" value=".html" />
</bean>
And this is my controller
#Controller
#RequestMapping(value = "/home")
public class HomeController {
#RequestMapping(value = "/")
public String home() {
return "login";
}
#RequestMapping(value = "/main")
public String main() {
return "main";
}
#RequestMapping(value = "/about")
public String main() {
return "about";
}
}
This is a pretty simple example, I don't know why isn't working. I have readed a lot of questions like this, here and in another sites, I don't know what I'm missing.
By the way, in the controller I tried all the convinations to match the url; put "/home/" or "home/" at the class level, and put "login" or "main" at method level.
The only thing that works is when I hit localhost:8080/myServer/home/. I tried using "/home/" in the url-pattern of the web.xml instead "/home/". I also tried use "/" and "/", but is the same.
The only way that this works is when in the url-pattern of the web.xml I use
<url-pattern>/home/</url-pattern>
<url-pattern>/home/main/</url-pattern>
<url-pattern>/home/login/</url-pattern>
Then I can hit all the url's. I don't know what to do, maybe this is a tomcat 7 problem?
you were not configured mapping for controller and annotation handler.Add this below bean configuration in the kimboo-servlet.xml and also add the respective jars.It may work.
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/home/**=homeController
</value>
</property>
</bean>
Rather than this being a TomCat problem I suspect this is a problem with how you have annotated the controller. Please replace your controller annotations as follows
#Controller
public class HomeController {
#RequestMapping(value = "/home")
public String home() {
return "login";
}
#RequestMapping(value = "/home/main")
public String main() {
return "main";
}
#RequestMapping(value = "home/about")
public String main() {
return "about";
}
}
This is only a guess. Sorry if I got it wrong. I remember seeing this problem in REST services. Maybe its the same.Dont have time to test it myself now.
Look at this question: Basic Spring MVC config: PageNotFound using InternalResourceViewResolver. The reason is your mapping which is too wide and interfere to JspServlet mapping.

Calling ActionClass in tiles.xml using Struts 2.0

I reviewed the example of tiles with struts2.0 and found that in tiles.xml jsp pages are called like:
<definition name="welcome" extends="baseLayout">
<put-attribute name="title" value="Welcome"/>
<put-attribute name="body" value="/welcome.jsp"/>
BUT my question is if I want to call the action class instead of .jsp pages than how to call it like
<definition name="friends" extends="baseLayout">
<put-attribute name="title" value="Friends"/>
<put-attribute name="body" value="/checkActionLink.action"/>
when I am trying to write to execute the above code than its showing the error that checkActionLink.action is not found....thanks in advance for the help.....
Following is the web.xml file
<?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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Struts2Example15</display-name>
<servlet>
<servlet-name>tiles</servlet-name>
<servlet-class>org.apache.tiles.web.startup.TilesServlet</servlet-class>
<init-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
After so much research I found out that adding the following tag:
<dispatcher>FORWARD</dispatcher> in the web.xml the issue gets resolved.
Now the question arises why?
As per my understanding when we add any action as the value of value attribute of the <put-attribute/> tag the request is forwarded to the mentioned action so the action is executed successfully.
Previously <dispatcher>FORWARD</dispatcher> tag was missing so this issue was caused.
I would really appreciate if any correction is there in my understanding.
Thanks. Happy Coding :).
I don't think you can. You'll need to create a jsp and use the struts2 action tag in it. That can call an action and render part of it's page. If you make a jsp only using the action tag, you'll probably get the effect you want. Have never tried this, but you can probably insert the name of the action and namespace from tiles into the action tag before the jsp is invoked.
I would be very interested to hear how this works out for you.

Resources