I am struggling whit Jersey MVC but anything does not work for me in Tomcat 7. I searched in Google but I have not found any that works for me.My jsp pages are placed in WEB-INF/protected/ directory, That is my code :
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<session-config>
<session-timeout>
5
</session-timeout>
</session-config>
<filter>
<filter-name>REST Service</filter-name>
<filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.onlinebook.servey.services.resources</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.scanning.recursive</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.mvc.templateBasePath.jsp</param-name>
<param-value>/WEB-INF/protected/</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.server.mvc.jsp.JspMvcFeature</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.tracing</param-name>
<param-value>ALL</param-value>
</init-param>
<init-param>
<param-name>jersey.config.servlet.filter.staticContentRegex</param-name>
<param-value>(/index.jsp)|(/(content|(WEB-INF/jsp))/.*)</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>REST Service</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/onlineBookDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<security-constraint>
<display-name>Admin</display-name>
<web-resource-collection>
<web-resource-name>tomcat datasourcerealm administration</web-resource-name>
<description/>
<url-pattern>/protected/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<description/>
<role-name>admin</role-name>
</auth-constraint>
<!-- <user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>-->
</security-constraint>
<security-constraint>
<display-name>User</display-name>
<web-resource-collection>
<web-resource-name>Online Book administration</web-resource-name>
<description/>
<url-pattern>/protected/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<description/>
<role-name>user</role-name>
</auth-constraint>
<!-- <user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>-->
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>jdbc-realm</realm-name>
<form-login-config>
<form-login-page>/signin.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
<security-role>
<description/>
<role-name>admin</role-name>
</security-role>
<security-role>
<description/>
<role-name>user</role-name>
</security-role>
<error-page>
<error-code>403</error-code>
<location>/access_denied.jsp</location>
</error-page>
</web-app>
And my REST service:
#Path("userdata")
public class UserDataRestService {
#Inject
private AccessCodeBean accessCodeBean;
#Inject
UserDataBean userDataBean;
public UserDataRestService() {
}
#GET
public Viewable redirectTo() throws ServletException, IOException {
return new Viewable("accessCodeEntry");
}
}
Related
In order to change the authorities for authenticated users I need to retrieve all of these users, so I used SessionRegistry as shown in this link.
But the sessionRegistry.getAllPrincipals() method returns an empty list.
The configuration of my project is as follows :
Web.xml :
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
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>loyfeey</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<!-- <context-param> <param-name>contextConfigLocation</param-name> <param-value>com.mkyong.web.config</param-value>
</context-param> -->
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<location>/errors</location>
</error-page>
<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<!-- if true then browser script won't be able to access the cookie -->
<http-only>false</http-only>
<!--!if true then the cookie will be sent only over HTTPS connection -->
<secure>false</secure>
</cookie-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
</web-app>
WebSecurityConfigurerAdapter
#Configuration
#EnableWebSecurity
#Transactional
#Service
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
#Autowired
private CompteDetailsService compteDetailsService;
#Autowired
private IMatcherService matcherDetailsService;
#Autowired
private CustomLogoutSuccessHandler logoutSuccessHandler;
#Bean
public SessionRegistry sessionRegistry() {
return new SessionRegistryImpl();
}
#Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(compteDetailsService).passwordEncoder(passwordEncoder());
}
#Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests().antMatchers(/*some paths*/).permitAll();
http.authorizeRequests().antMatchers("/**").access("hasRole('ROLE_ADMIN')");
List<Matcher> matchers = matcherDetailsService.getMatchersConfig();
for (Matcher matcher : matchers) {
http.authorizeRequests()//
.antMatchers("/" + matcher.getPath())//
.hasAnyAuthority(matcher.getPermission().toString());
}
http.authorizeRequests().anyRequest().denyAll();
http.authorizeRequests().and().formLogin()
.loginProcessingUrl("/login")
.loginPage("/")
.defaultSuccessUrl("/authentification/successful")
.failureUrl("/?error=true")
.usernameParameter("login")
.passwordParameter("password");
http.authorizeRequests().and().logout()
.logoutUrl("/authentification/logout")
.logoutSuccessUrl("/");
.logoutSuccessHandler(logoutSuccessHandler);
http.authorizeRequests().and().exceptionHandling().accessDeniedPage("/authentification/accessDenied");
SessionRegistry sr = sessionRegistry();
http.sessionManagement().maximumSessions(1).sessionRegistry(sessionRegistry());
}
}
If anyone can help me find out what I'm missing
Thanks to #M. Denium for his answer, I separated spring-servlet.xml into two files : spring-context.xml and spring-servlet.xml as it is shown below :
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"
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>loyfeey</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>
<!-- <context-param> <param-name>contextConfigLocation</param-name> <param-value>com.mkyong.web.config</param-value>
</context-param> -->
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<location>/errors</location>
</error-page>
<session-config>
<session-timeout>30</session-timeout>
<cookie-config>
<!-- if true then browser script won't be able to access the cookie -->
<http-only>false</http-only>
<!--!if true then the cookie will be sent only over HTTPS connection -->
<secure>false</secure>
</cookie-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
</web-app>
Hi am migrating my spring application from jboss 6 to wildfly 14.
I'm using Spring version 3
The application was deplyed with no issu in Jboss 6, When I tried de deploy it in Wildfly 14 I got the this exception:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicationSessionListener' defined in "/C:/Produits/LDEV-BACK/wildfly-14.0.1.Final-EXA/bin/content/muses-web.war/WEB-INF/classes/fr/bdf/muses/manager/ApplicationSessionListener.class": Initialization of bean failed; nested exception is java.lang.IllegalStateException: UT010041: The servlet context has already been initialized, you can only call this method from a ServletContainerInitializer or a ServletContextListener:
Is it possible de deploy a spring 3 application on Wildfly 14 ? If yes can someOne please guides me to resolve this.
Here is my web.xml
<web-app 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"
version="3.1">
<servlet>
<servlet-name>myApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>catalogue-archive.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>myApp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>403</error-code>
<location>/erreur/403.jsp</location>
</error-page>
<error-page>
<error-code>405</error-code>
<location>/erreur/405.jsp</location>
</error-page>
<error-page>
<error-code>408</error-code>
<location>/erreur/408.jsp</location>
</error-page>
<error-page>
<location>/erreur/default.jsp</location>
</error-page>
<security-constraint>
<display-name>MYAPP USERS</display-name>
<web-resource-collection>
<web-resource-name>Public</web-resource-name>
<url-pattern>/css/*</url-pattern>
<url-pattern>/lib/*</url-pattern>
<url-pattern>/img/*</url-pattern>
<url-pattern>/wro/*</url-pattern>
<url-pattern>/erreur/*</url-pattern>
<url-pattern>/login-page.jsp</url-pattern>
<url-pattern>/logout.jsp</url-pattern>
<url-pattern>/auth.html</url-pattern>
<url-pattern>/auth/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<display-name>MYAPP USERS</display-name>
<web-resource-collection>
<web-resource-name>AUTHENTICATED_RESOURCE</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method-omission>OPTIONS</http-method-omission>
</web-resource-collection>
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<display-name>MYAPP admin</display-name>
<web-resource-collection>
<web-resource-name>AUTHENTICATED_RESOURCE</web-resource-name>
<url-pattern>/admin-MYAPP/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>superviseur</role-name>
<role-name>FU1UA1416</role-name>
<role-name>FU_UA1416</role-name>
<role-name>FU1UA2113</role-name>
<role-name>FU_UA2113</role-name>
<role-name>FU1UA1482</role-name>
<role-name>FU_UA1482</role-name>
<role-name>FU1UA2504</role-name>
<role-name>FU_UA2504</role-name>
<role-name>FU1UA2109</role-name>
<role-name>FU_UA2109</role-name>
<role-name>AU_MYAPP_RSP</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<display-name>MYAPP debug</display-name>
<web-resource-collection>
<web-resource-name>AUTHENTICATED_RESOURCE</web-resource-name>
<url-pattern>/debug-MYAPP/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>superviseur</role-name>
<role-name>FU_UA2109</role-name>
<role-name>FU_UA2113</role-name>
<role-name>FU1UA2113</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-role>
<role-name>*</role-name>
</security-role>
<security-role>
<role-name>FU1UA2113</role-name>
</security-role>
<security-role>
<role-name>FU1UA1416</role-name>
</security-role>
<security-role>
<role-name>FU_UA2109</role-name>
</security-role>
<security-role>
<role-name>FU1UA2109</role-name>
</security-role>
<security-role>
<role-name>FU_UA2113</role-name>
</security-role>
<security-role>
<role-name>FU_UA1416</role-name>
</security-role>
<security-role>
<role-name>FU1UA1482</role-name>
</security-role>
<security-role>
<role-name>FU_UA1482</role-name>
</security-role>
<security-role>
<role-name>FU1UA2504</role-name>
</security-role>
<security-role>
<role-name>FU_UA2504</role-name>
</security-role>
<security-role>
<role-name>AU_MYAPP_RSP</role-name>
</security-role>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>ldapm</realm-name>
<form-login-config>
<form-login-page>/loginController.html</form-login-page>
<form-error-page>/erreur/auth.jsp</form-error-page>
</form-login-config>
</login-config>
<mime-mapping>
<extension>woff</extension>
<mime-type>application/font-woff</mime-type>
</mime-mapping>
<mime-mapping>
<extension>tff</extension>
<mime-type>application/font-tff</mime-type>
</mime-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>WebResourceOptimizer</filter-name>
<filter-class>ro.isdc.wro.http.WroFilter</filter-class>
</filter>
<!-- <filter> <filter-name>WebResourceOptimizer</filter-name> <filter-class>ro.isdc.wro.http.WroFilter</filter-class>
<init-param> <param-name>configuration</param-name> <param-value>${muses.wro4j.profile}</param-value>
</init-param> <init-param> <param-name>gzipResources</param-name> <param-value>FALSE</param-value>
</init-param> <init-param> <param-name>cacheUpdatePeriod</param-name> <param-value>60</param-value>
</init-param> <init-param> <param-name>modelUpdatePeriod</param-name> <param-value>600</param-value>
</init-param> <init-param> <param-name>jmxEnabled</param-name> <param-value>false</param-value>
</init-param> <init-param> <param-name>disableCache</param-name> <param-value>true</param-value>
</init-param> </filter> -->
<filter-mapping>
<filter-name>WebResourceOptimizer</filter-name>
<url-pattern>/wro/*</url-pattern>
</filter-mapping>
</web-app>
Here is myApp-servlet.xm file
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<import resource="classpath*:/data-access-config.xml" />
<context:component-scan base-package="fr.sg.myApp"/>
<mvc:resources mapping="/**" location="/" />
<mvc:annotation-driven />
<bean id="sessionListener" class="fr.sg.myApp.manager.ApplicationSessionListener"></bean>
<mvc:interceptors>
<bean class="fr.sg.myApp.manager.CurrentUserInterceptor"></bean>
</mvc:interceptors>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
<property name="order" value="0"/>
</bean>
</beans>
And here is my Bean Spring
#Component
public class ApplicationSessionListener implements HttpSessionListener,
ApplicationContextAware {
private static int totalActiveSessions;
#Autowired
private ReportService reportService;
public static int getTotalActiveSession() {
return totalActiveSessions;
}
#Override
public void sessionCreated(HttpSessionEvent arg0) {
totalActiveSessions++;
HttpSession session = arg0.getSession();
Date now = new Date(session.getCreationTime());
reportService.createSession(session.getId(), now);
}
#Override
public void sessionDestroyed(HttpSessionEvent arg0) {
totalActiveSessions--;
HttpSession session = arg0.getSession();
Date now = new Date(session.getLastAccessedTime());
reportService.updateSession(session.getId(), now);
}
#Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
if (applicationContext instanceof WebApplicationContext) {
((WebApplicationContext) applicationContext).getServletContext()
.addListener(this);
} else {
// Either throw an exception or fail gracefully, up to you
throw new RuntimeException(
"Must be inside a web application context");
}
}
}
I'm trying to implement the primefaces file upload but isn't invoking the bean method, by the way I'm using spring framework and prettyfaces:
faces-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.2" 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-facesconfig_2_2.xsd">
<application>
<resource-bundle>
<base-name>label</base-name>
<var>msg</var>
</resource-bundle>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>log4j-config-location</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>co.com.core.commons.LogContextListener</listener-class>
</listener>
<!-- ############################################# -->
<!-- # File upload # -->
<!-- ############################################# -->
<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>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<!-- ############################################# -->
<!-- # QUARTZ # -->
<!-- ############################################# -->
<!-- listener>
<listener-class>
org.quartz.ee.servlet.QuartzInitializerListener
</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>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
.xhtml code:
<h:form enctype="multipart/form-data" prependId="false">
<p:fileUpload mode="simple" value="#{templateController.file}" />
<p:commandButton value="Upload" actionListener="#{templateController.upload}" ajax="false" />
</h:form>
bean method:
public void upload() {
FacesMessage msg = new FacesMessage("Success! is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
I tried adding the FORWARD to the filter because I'm also using pretty faces, but still not working, thanks.
Try implementing followings,
add this method to your bean,
public void handleProfileFileUpload(FileUploadEvent event) {
if (event != null) {
try {
InputStream inputStream = event.getFile().getInputstream();
// Set inputstream to your object
} catch (IOException e) {
e.printStackTrace();
}
}
}
Remove CommandButton and modify fileUpload config similar to this, fileUploadListener handle the uploading
<p:fileUpload fileUploadListener="#{<your bean>.handleProfileFileUpload}"
mode="advanced" sizeLimit="2097152"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
Well finally after a lot of readings I solved this, in my case the solution was to add a context-param (primefaces.UPLOADER) into the web.xml file (check this answer please), this file looks like this, also could be helpful to set the project stage to develop (commented in this file), this could give you some additional information related to the debug process:
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<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-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>log4j-config-location</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>co.com.core.commons.LogContextListener</listener-class>
</listener>
<!-- ############################################# -->
<!-- # File upload # -->
<!-- ############################################# -->
<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>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</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>
If you are using Spring MVC, your filter mapping should point to the Spring DispatcherServlet instead of Faces Servet.
<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>Spring MVC Servlet</servlet-name>
</filter-mapping>
For some reason when I changed the mapping of my jsp in my spring controller to use a path variable none of the javascript or the css files will load. I can't find any other examples on the net of this happening. I'm guessing it's some mapping issue in my web.xml but I don' really know.
Here is my controller code..
#RequestMapping(value = "common/taskSummary/{taskInstanceId}.do", method=RequestMethod.GET)
public ModelAndView taskSummary(
#PathVariable("taskInstanceId") int taskInstanceId) {
// Get user from Spring security
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
ModelAndView modelAndView = new ModelAndView("taskSummary");
modelAndView.addObject("username", user.getUsername());
return modelAndView;
}
Here is my web.xml...
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<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/applicationContext.xml
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<resource-ref>
<description>ArcFlashMap DB Connection</description>
<res-ref-name>jdbc/AFM_DB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet>
<servlet-name>LoadResourcesServlet</servlet-name>
<servlet-class>ie.premiumpower.services.reports.common.LoadResourcesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
And here is the errors I'm getting on firebugs console...
Figured it out.
I had been mapping my css and js files using href="../css/style.css"
but this needed to be changed to href="../../css/style.css". Adding the extra "../".
I'm doing a Web application using Spring 3.1.0.RELEASE, JSF 2.x, JPA 2 with Hibernate Provider. The application run on Tomcat 6.35.
I use PrettyFaces 3.3.2 for friendly URL and Spring security 3.1.0.RELEASE.
I use Primefaces 3.1 and I try to use the fileupload component of Primefaces. Sadly its doesn't work.
I have the good dependencies in my 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>1.4</version>
</dependency>
My web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!-- Use this definition if using a Java EE 6 container This also stops Eclipse
from complaining that 3.0 is not a valid version <web-app 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"> -->
<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"
version="2.5">
<!-- Theme pour prime -->
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>glass-x</param-value>
</context-param>
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/applicationContext.xml
/WEB-INF/spring/security-app-context.xml
</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEVELOPMENT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<!-- jboss-el -->
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>org.jboss.el.ExpressionFactoryImpl</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>51200</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>/tmp/primefacesFileupload</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<url-pattern>/*</url-pattern>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
<!-- Filter for setting all the requests and responses to UTF-8 encoding-->
<filter>
<filter-name>spring-encoding-filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>spring-encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Localisation -->
<filter>
<filter-name>localizationFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>localizationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring security -->
<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>
<!-- Pretty faces -->
<filter>
<filter-name>Pretty Filter</filter-name>
<filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Pretty Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- place constraints on a single user's ability to log in to your application -->
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/app/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/spring/</url-pattern>
</servlet-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>*.jsf</url-pattern>
</servlet-mapping>
<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>
<resource-ref>
<res-ref-name>jsf/ProjectStage</res-ref-name>
<res-type>java.lang.String</res-type>
<jndi-name>javax.faces.PROJECT_STAGE</jndi-name>
</resource-ref>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
In the view I try the following:
<h:form enctype="multipart/form-data" id="testFileupload">
<pou:fieldset legend="Upload de fichiers">
<pou:messages showDetail="true"/>
<pou:fileUpload id="fileUploadInputTest" fileUploadListener="#{siteFormManager.handleFileUpload}" auto="true" dragDropSupport="false"/>
</pou:fieldset>
</h:form>
Or
<h:form enctype="multipart/form-data">
<pou:messages showDetail="true"/>
<pou:fileUpload value="#{siteFormManager.file}" mode="simple"/>
<pou:commandButton value="Submit" ajax="false" actionListener="#{siteFormManager.upload}" action="#{siteFormManager.uploadAction}"/>
</h:form>
In my bean
#Component
#Scope(value = "request")
public class SiteFormManager {
private static final Logger logger = LoggerFactory.getLogger(SiteFormManager.class);
/**
* Le file to upload
*/
private UploadedFile file;
public void handleFileUpload(FileUploadEvent event) {
final UploadedFile lfile = event.getFile();
logger.debug("SiteFormManager::handleFileUpload {} --> File equals {}", event, lfile);
}
public void handleFileUpload() {
logger.debug("SiteFormManager::handleFileUpload without params");
}
public String uploadAction() {
logger.debug("SiteFormManager::uploadAction --> File equals {}", file);
return null;
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
}
The problem is that the file in my bean is always null! I have try in Tomcat 7 and nothing change.
I have try the fileUpload component of Richfaces and it's work, but I wont include the two components libraries to avoid conflicts.
I have debug my application on Netbeans 7 and insert a breakpoint on FileUploadFilter::doFilter. I have notice that the doFilter is called twice. The first time the MultipartRequest is well created, parsing the inputs of the form and detect the file.
But the second time the request seems to be consume and nothing is fill in the MultipartRequest. The Primefaces FileUploadRenderer use the second MultipartRequest and so no file is present in that one.
Is it normal that the FileUploadFilter is called twice ? How make the fileUpload component work ?
SOLUTION
I have only change the web.xml, added the ASYNC in the prettyfaces filter and remove all the
dispatchers in fileupload filter.
The final web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Use this definition if using a Java EE 6 container This also stops Eclipse
from complaining that 3.0 is not a valid version <web-app 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"> -->
<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"
version="2.5">
<!-- Theme pour prime -->
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>glass-x</param-value>
</context-param>
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/applicationContext.xml
/WEB-INF/spring/security-app-context.xml
</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEVELOPMENT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<!-- jboss-el -->
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>org.jboss.el.ExpressionFactoryImpl</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>51200</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>/tmp/primefacesFileupload</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<url-pattern>/*</url-pattern>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<!-- Filter for setting all the requests and responses to UTF-8 encoding-->
<filter>
<filter-name>spring-encoding-filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>spring-encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Localisation -->
<filter>
<filter-name>localizationFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>localizationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring security -->
<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>
<!-- Pretty faces -->
<filter>
<filter-name>Pretty Filter</filter-name>
<filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Pretty Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- place constraints on a single user's ability to log in to your application -->
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/app/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/spring/</url-pattern>
</servlet-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>*.jsf</url-pattern>
</servlet-mapping>
<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>
<resource-ref>
<res-ref-name>jsf/ProjectStage</res-ref-name>
<res-type>java.lang.String</res-type>
<jndi-name>javax.faces.PROJECT_STAGE</jndi-name>
</resource-ref>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Thanks you.
I believe you need to add the ASYNC dispatcher to your PrettyFaces filter mapping:
<!-- Pretty faces -->
<filter>
<filter-name>Pretty Filter</filter-name>
<filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Pretty Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
The PrimeFaces upload component uses ASYNC uploads if I am not mistaken.