Summary:
When I turn on my Spring Boot application. (Running on the embedded Tomcat 8 server) I never receive the:
INFO [org.ocpsoft.rewrite.servlet.RewriteFilter] RewriteFilter starting up...
...
INFO [org.ocpsoft.rewrite.servlet.RewriteFilter] Loaded [] org.ocpsoft.rewrite.config.ConfigurationProvider [org.ocpsoft.rewrite.prettyfaces.PrettyFacesRewriteConfigurationProvider<1>]
INFO [org.ocpsoft.rewrite.servlet.RewriteFilter] RewriteFilter initialized.
Log notifications. For some reason PrettyFaces isn't starting up, and I don't know why.
Technologies: Spring Boot 1.2.0.RELEASE, Java 8, Maven for dependency management. Embedded Tomcat 8.0.15 Server.
Focusing on Java Configuration as much as possible. Previously I tried to use Rewrite, but it gave me an equal amount of gruff. Feel like I'm missing something obvious.
Here's a link to my current code base. (It's pretty small, just working on the foundation for a new project, nothing major implemented yet.)
https://github.com/MeisterGit/FoundationServer
Maven Dependency:
<dependency>
<groupId>com.ocpsoft</groupId>
<artifactId>prettyfaces-jsf2</artifactId>
<version>3.3.3</version>
</dependency>`
Other Maven Dependency Tried:
<!-- PrettyFaces -->
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-servlet</artifactId>
<version>2.0.12.Final</version>
</dependency>
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-config-prettyfaces</artifactId>
<version>2.0.12.Final</version>
</dependency>
Both version yield the same result. No startup messages.
I'm trying to keep XML to an absolute minimum. I have faces-config set up with:
<faces-config 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"
version="2.2">
<!-- Allow Spring Beans to be accessible to JSF. -->
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>`
and my controller is topped by:
#Controller
#URLMapping(id = UserController.INDEX,
pattern = "/",
viewId = "/content/index.xhtml") // Home page.`
Here's my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="foundation-server"
version="3.1">
<!-- PrettyFaces: Specify which package to scan for #UrlMapping annotations -->
<context-param>
<param-name>com.ocpsoft.pretty.BASE_PACKAGES</param-name>
<param-value>foundation</param-value>
</context-param>
<!-- No Pretty Filter required, servlet 3.0+ automatically registers the filter. -->
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
Any help on what I'm doing wrong? The server turns on, I can hit http://localhost:8080/content/index.xhtml just fine, and the JSF template loads. The Spring Bean backs it up. . . But no URL mapping is functioning. If I hit http://localhost:8080/ I just get an error.
When you're using Spring Boot with an embedded container web.xml and web-fragment.xml are ignored. You need to register the PrettyFaces filter in your application's Java configuration:
#Bean
public FilterRegistrationBean prettyFilter() {
FilterRegistrationBean prettyFilter = new FilterRegistrationBean(new PrettyFilter());
prettyFilter.setDispatcherTypes(DispatcherType.FORWARD, DispatcherType.REQUEST,
DispatcherType.ASYNC, DispatcherType.ERROR);
prettyFilter.addUrlPatterns("/*");
return prettyFilter;
}
Spring Boot could be improved to auto-configure this filter for you if PrettyFaces is on the classpath. If you'd like to see such an enhancement, please open an issue.
Related
We have a web application built upon Spring Framework 3.2.x (3.2.12-RELEASE at moment) and Spring Security 3.2.x (i.e 3.2.5-RELEASE)
Security is implemented with classical Spring approach. In web.xml we load ApplicationContext and springSecurityFilterChain
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/spring/applicationContext*.xml
</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>
As of a change in requirements, we are trying to investigate on how to permit different HttpSessions per tab (or window) within the same browser: in this scenario the same physical user can log in the application using two separate app-users.
I can understand that this is not possibile using JSESSIONID cookie approach. I'm trying using Spring Session (Spring Session and Spring Security
and Spring Session Multiple Session) but i can't manage doing it, having difficulties mixing my XML configuration with the JavaConfig approach shown in links before, Tomcat does not even start with a lot of errors.
I'm new with mixing XML and JavaConfig approch, so..can anyone give me an hint in how to proceed with Spring Session?
Are there other ways to fulfill my requirement?
Spring Session project actually contains a few sample projects that demonstrate usage with Spring XML config. Look for the samples with -xml suffix in the sample name.
Generally speaking, what you need to do is manually register appropriate #Configuration class as a #Bean. For example, if you want to use Spring Session backed by Redis, register RedisHttpSessionConfiguration. You'll find the correct #Configuration class to use by looking at appropriate #Enable*HttpSession annotation (in this case #EnableRedisHttpSession) and determining which class is imported via #Import annotation.
I've been struggling for 2-3 weeks now with transforming my project (single module) into a multi module project where the modules are representing the layers of a multilayer architecture (i.e. persistence, business, domain...)
When the project was still a single module, everything worked together. I had my ManagedBean classes managed by JSF and enabled #Autowired for Spring with a workaround, so I could inject my Spring-managed classes into the jsf-managed beans. Now, this won't seem to work anymore, so I'm trying to clean my code up and orientate myself at some examples I've seen (one is the Maven archetype for jsf2 + spring 4 integration).
It seems, there are two ways suggested to do the Dependency Injection:
Do it per #ManagedProperty (But I don't want to do this all per JSF)
Do it per #Autowired and let Spring manage the Backing/Managed Beans (That's what I try to do)
Though many sources (even the Maven archetype, which is working) suggest to just annotate my Backing Bean class with Spring annotations (#Component, #Scope("session")), I always get the error
Caused by: javax.el.PropertyNotFoundException: Target Unreachable, identifier 'administration' resolved to null
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" >
<context:component-scan
base-package="my.base.projectpackage" />
<context:annotation-config />
<context:spring-configured />
<!-- import the other context files for Spring -->
<import resource="classpath*:META-INF/*-springContext.xml"/>
faces-config.xml
<faces-config 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"
version="2.2" metadata-complete="false">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
I've already tried to:
Enable Autowiring into a JSF managed bean (per [...].autowire(this) -> not working, NPE for the Service class)
Try it with #ManagedProperty on JSF annotated Service classes (not working, NPE for the Service class)
Try it with pure Spring classes (like mentioned above, not working identifier resolved to null for the managed bean)
I can compile and build the modules and the whole project without errors, and I've also not forgotten to put intermodular dependencies (like business module is depending on persistence module) into the pom.xml. Every module has packaging "jar" (except ofc the webapp module, which is war). Did I forget to mention something else?
I run the project on Tomcat 8 (though this should not be the source of the error).
Maybe someone of you has got a hint, idea or solution for me.
Thanks in advance.
Edit
I got my project to work again and split it into multiple modules. But the only way to do this was to really copy + paste (argh! really..) my classes into the Maven archetype project and add some minor, not important fixes (like cleaning out my POM files). When it worked there, I was curious, copied the project files back into my old project and tried it there again. Surprisingly, it still didn't work, even though there was no difference anymore which could have caused it not to work. So, the mystery is still unsolved, but after all, I could continue with my work.
Thank you for your help (or for trying to help me, since this was a lost cause) #Tiny.
I'm trying to do a project with JAX-RS, Spring, Hibernate, Maven and JPA. I'm following this article as reference. This project is in Spring MVC. I'm very new to Spring MVC. I have tried to convert it to JAX-RS. What are the important changes I have to do with this?
The main change I noticed that in Spring MVC we use like this: #RequestMapping("/accounts/{username}").
But in JAX-RS we use like this. #Path("/accounts/{username}")
Therefore If I do the changes like this, what are the other key changes we have to do with this?
The most basic thing you need to do in order to port the Spring MVC controllers to JAX-RS controllers, is to provide and integration between Spring and your JAX-RS provider. One way to do that is to make the appropriate changes to web.xml.
Assuming you are using Jersey 2.x and Spring 3.x, the web.xml would look like:
<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">
<module-name>helloworld-spring</module-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>SpringApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>some.package.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Then MyApplication would look like:
package some.package;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;
public class MyApplication extends ResourceConfig {
public MyApplication () {
register(RequestContextFilter.class);
register(JerseyResource.class);
register(SpringSingletonResource.class);
register(SpringRequestResource.class);
register(CustomExceptionMapper.class);
}
}
You will also need the dependency
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.10.1</version>
</dependency>
besides the other Spring 3 and Jersey 2 dependencies.
Once you get that out of the way, you can start transforming the controllers.
For example:
#Path("admin")
#Component
public class AdminController {
#Autowired
private SchoolService service;
#Path("load-home-page")
#GET
public String loadHomePage(Map model){
return "admin/index";
}
//the rest of the code accordingly
}
The documentation for Spring-Jersey integration is here while a complete example can be found here.
Personally I would go the Spring MVC route instead of Jersey if you really don't have a compelling reason to choose Jersey over Spring MVC.
I've got the a WEB-INF/web.xml file with a couple of servlets, along with a context
listener which I use to bootstrap the application. I'd like to use Spring in this
web application. What's the best way to work Spring into this so I can use Spring's
injection mechanisms throughout the entire application - even in the servlets which
exists today?
<?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">
<display-name>Company's XMLRPC service</display-name>
<!-- Servlet Listeners -->
<listener>
<listener-class>com.company.download.context.DefaultServletContextListener</listener-class>
</listener>
<!-- Servlet Declarations -->
<servlet>
<servlet-name>DefaultTrackDownloadServlet</servlet-name>
<servlet-class>com.company.download.web.impl.DefaultTrackDownloadServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DefaultXmlRpcServlet</servlet-name>
<servlet-class>com.company.download.web.impl.DefaultXmlRpcServlet</servlet-class>
</servlet>
<!-- Servlet Configurations -->
<servlet-mapping>
<servlet-name>DefaultTrackDownloadServlet</servlet-name>
<url-pattern>/track</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DefaultXmlRpcServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Can Spring “live” alongside other servlets in the same webapp?
Yes, Spring MVC is basically just a DispatcherServlet that can make use of a ContextLoaderListener.
These two classes are already setup to interact with one or more ApplicationContext instances and have Spring manage the declared beans.
Your custom Servlet classes are not. If you need to inject a bean into your own Servlet instances, you need to get a reference to the ApplicationContext from the ContextLoaderListener and get the beans you want. There are a few options, whether you do it yourself or use built-in features.
The ContextLoaderListener stores the ApplicationContext it loads into a ServletContext attribute named
org.springframework.web.context.WebApplicationContext.ROOT
So you can retrieve it with that (there's a constant for easy use)
ApplicationContext ac = (ApplicationContext) config.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
Other options exist, see some of them here:
Autowiring in servlet
I want to inject an object in servlet using Spring
I work on a web app using Spring annotations to inject my Controllers and Services. In the app, I have users working on projects but I need to make sure one project is only edited by one user at a time so I have an attribute in the table "Project" in the database to save if the project is open and by whom.
I need to add an HTTPSessionListener to be able to "close" the project when the user editing it disconnects. It means that at the "sessionDestroyed" event, I want to call my DAO service to update the project in the database.
The only problem is that this service is injected by Spring and I cannot get it...
I tried to use #Autowired in my HTTPSessionListener but it didn't work, I tried to do like in this solution (Spring – How to do dependency injection in your session listener ) but I get a nullPointerException for the WebApplicationContext...
My web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp"
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"
id="WebApp_ID" version="3.0">
<!-- jsp config => automatic inclusions in all jsp files -->
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<include-prelude>taglibs.jsp</include-prelude>
<include-prelude>setLanguage.jsp</include-prelude>
</jsp-property-group>
</jsp-config>
<display-name>MEANS</display-name>
<!--Spring dispatcher servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern><!-- detect all urls ending with ".do" -->
</servlet-mapping>
<!-- The welcome files -->
<welcome-file-list>
<welcome-file>connection.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>30</session-timeout><!-- the session is automatically disconnected after 30 min of inactivity -->
</session-config>
<listener>
<listener-class>myPackages.listener.MySessionListener</listener-class>
</listener>
And the dispatcher-servlet.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Spring MVC Support for annotations (JSR-303) -->
<mvc:annotation-driven/>
<!-- Spring View resolver => find the jsps -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:suffix=".jsp" />
<!-- Spring info : what packages to scan for detecting annotations -->
<context:component-scan base-package="myPackages"/>
So I need your help... Does anybody have an idea of how I could inject a Spring Service into my HTTPSessionListener?
Thanks in advance for your help!
One of the options (perhaps the best one from the design point of view) is to create a bean of scope session (don't forget to declare RequestContextListener) and put logic associated with the session lifecylce into it. Spring will automatically call its destruction method (annotated with #PreDestroy or configured as destroy-method) when session is to be destroyed.
Your approach with injection into session listener causes problems because you only have DispatcherServlet, not ContextLoaderListener, and WebApplicationContextUtils cannot obtain an application context associated with DispatcherServlet. If you choose to follow that apporach you should create a root application context and extract some of your beans into it, then you will be able to access it from session listener via WebApplicationContextUtils.