Consuming Facelets from a JAR leads my WAR to interpret the "JSF viewId" as a .jsp instead of .jsf - spring

I'm afraid this question might not be as "close ended" as a StackOverflow question should be. Hopefully, I'm wrong.
Context
We have a project (the typical Java EE with a JSF web GUI) that, due to requirement's specificities, we needed to split in two projets. One of these new split projects is a subset of the previous, with some slight differences.
So, from ORIGINAL_JAVAEE_WEBAPP.war we created: ORIGINAL_JAVAEE_WEBAPP-Proj01.war and ORIGINAL_JAVAEE_WEBAPP-Proj02.war.
In order to avoid redundancy we've decide to create a JAR file with all the facelets, managed beans and related pojos. The idea was that each WAR would include this JAR, use whatever they need and, then, the WAR would have the specific pages, logos, ...
The problem
I've created a simple example just to practice having a JAR file with facelets and having two WAR files including and using these facelets. In this example, things worked flawlessly.
Nevertheless, "the real world" is not that simple. Our project also relies on Primefaces, Spring Core and Spring Security, just to mention a few.
The implementation is not yet finished - far from it. Right now, under the same Maven project, I've created the JAR module, moved the beans and the facelets to that module and included it as a dependency in the WAR file module.
The problem is that, now, my "FacesContext.getViewRoot().getViewId()" tells me that viewId is "someName.jsp" and not "someName.jsf". This simple change messes up with our "legacy" "authentication phase listener" (a phase listener to ensure that a user is logged in and redirect accordingly) implementation.
So, what can be happening here? Do any of you have any hint of what my next steps might be in order to track the root source of this problem?
Thanks and regards.

This could be an issue with the JSF ViewHandlers registered for the FacesContext. What version of JSF are you using and are there any view handlers defined in the JSF context xml.
Also please check in the web.xml the context params :javax.faces.DEFAULT_SUFFIX, javax.faces.FACELETS_VIEW_MAPPING

Related

SpringBoot creating a framework starter library

I am creating a library using spring-boot (v2.1.6.RELEASE) as a starter project that will facilitate as base extension jar responsible for configuring and starting up some of the components based on client project properties file.
The issue I am facing is that if the client project's SpringBoot Application class contains the same package path as library everything works like charm! but when client project contains different package path and includes ComponentScan, it is not able to load or start components from the library.
Did anyone encounter this issue? how to make client application to auto-configure some of the components from library jar?
Note: I am following the library creation example from here: https://www.baeldung.com/spring-boot-custom-starter
There are many things that can go wrong here, without seeing relevant parts of actual code its hard to tell something concrete. Out of my head, here are a couple of points for consideration that can hopefully lead to the solution:
Since we use starters in our applications (and sometimes people use explicit component scanning in there spring applications) and this obviously works, probably the issue is with the starter module itself. Don't think that the fact that the component scan is used alone prevents the starter from being loaded ;)
Make sure the starter is a: regular library and not packaged as a spring boot application (read you don't use spring boot plugin) and have <packaging>jar</packaging> in your pom.xml or whatever you use to build.
Make sure you have: src/main/resources/META-INF/spring.factories file
(case sensitive and everything)
Make sure that this spring.factories file indeed contains a valid reference on your configuration (java class annotated with #Configuration). If you use component scanning in the same package, it will find and load this configuration even without spring factories, in this case, its just kind of another portion of your code just packaged as a separate jar. So this looks especially "suspicious" to me.
Make sure that #Configuration doesn't have #Conditional-something - maybe this condition is not obeyed and the configuration doesn't start. For debugging purposes maybe you even should remove these #Conditional annotations just to ensure that the Configuration starts. You can also provide some logging inside the #Configuration class, like: "loading my cool library".

Simple working Spring MVC / Maven configuration

I’ve been trying to set up a Spring MVC application from scratch, using Maven, in IntelliJ Idea. I know there are probably some nice Archetypes that can do this for you, but I really want to understand what’s going on. Here’s what I want to do:
Create a simple web application that shows displays “Hello World” under https://localhost:8080/, using an embedded Tomcat (with the Maven plugin). So, one controller, one request mapping, and one template. I’ve been able to get the spring application to boot (the Spring logo appears in the console) and Tomcat seems to run as well (the site responds). The problem is, I haven’t been able to get the request to map to my RequestMap method in my Controller. I’m quite sure that the controller isn’t the problem, but that it’s some configuration/setup issue.
Here’s the thing: I’ve been looking at tutorials, StackOverflow, Spring documentation, etc. for hours now, but I haven’t found a source that really explains how to configure a Spring MVC Maven project. Everyone seems to have a different opinion on what XML files you need, what they should be named, and where they should be located. The consensus seems to be that you need a web.xml file in a folder named WEB-INF, but even there, everyone has a different opinion on where that folder should go. I appreciate that there are multiple different ways to do this, and that there’s no “right” way, but in my experience, there are definitely many “wrong” ways =).
My question right now isn’t necessarily how to get my current project running (I don’t mind starting over), but what kinds of config files are out there (web, spring, app-config, servlet, …), what they do, which ones you need, where they need to go, what they should be named, how they connect, etc. Some people also use the Maven Compiler plugin, and others don’t, and nobody says why :D. Essentially: How do I let Spring know where to find controllers, templates, etc., and how to run and deploy all of this on the embedded Tomcat. I would like to find a source that explains the entire core-ecosystem of Spring MVC in a unified way. I have found many sources that provide “how-to” tutorials, but with little to no explanation (like “add the following servlet.xml file to your WEB-INF folder”). So, if your setup slightly differs from the tutorial (e.g. because you are using IntelliJ instead of Eclipse, Tomcat instead of Jetty, embedded Tomcat, a slightly different folder structure) nothing works, and, most importantly, you have no clue why.
Has anyone learned these setup-basics with anything better than copy/pasting or trial & error? :D
Easiest way to start is by using Spring Boot. Go to following link:
Spring initializr
Choose "Web" for "Selected Dependencise" and click "Generate Project". Download of maven project should start after that. Then import that project in IntelliJ IDEA as a Maven project. And that is a good starting point. You'll get main class that you can run and embeded Tomcat will start at 8080 port. Than you can add some controller, template etc...
Also good guides

Sharing files and backbeans between different wars Java EE

I am working on a large scale system using PrimeFaces 5.0, Java EE 7, Maven 3.0.5, Netbeans 7.4 & GlassFish 4.0
I want to implement it as (multiple WARs , multiple EJBs , one EAR).
Multiple wars could have common files like (JS, CSS, XHTML, Backbeans & Converters)
i have achieved this using jar which contains this resources.
different WAR files, shared resources
I need a session-scoped bean to be shared between different wars, I found this but i found it more than what i need.
http://docs.oracle.com/cd/E18686_01/coh.37/e18690/glassfish.htm#CEGBDHJB
so my questions is:
Is using a jar is the right approach to share what i want ??
where do i put jars like primefaces or omnifaces in the project where they use the same class loader ??
How can i share session-scoped between different wars ??
I have been working on a ear project with similar requirements as yours, according to our experience :
Sure. We have seperated our war projects and use them as extended controllers to carry out front end logic and passing data to view, and they make their service calls via a jar file called common-services.jar . Our whole service layer is living on a single jar file. However if you ask my personal opinion, I think it would have made a lot sense to create a third war file just for the services, and talk restful with all the front-end repos. That way service calls could be opened to third party users without any further work. So to sum it all up, yes it is an acceptable approach, but you should also consider packing it as war.
On a parent pom above all war, so all war files use the same version and it is managed from a single pom.
Carry all session based operations to your third jar / war we have discussed in question 1. Makes much more sense that way. Or I suppose you will need solutions like single sign on. But my first suggestion works like a charm for us.

Browse spring context

Currently, I'm working on a project which has a large number of dependencies.
During development, I sometimes have issue with some spring bean.
Normally, the stack trace can tell me the name of the bean. However, it does not shows me which context.xml file declares the bean. Moreover, it does not tell me which .jar file contains the context file and how that context file is imported into my project (I means the chain of import).
As there are many depedencies, not all context file are uses, many beans are override....
It takes me a lot of time to search the correct context file. One simple solution is to import all related project into my eclipse workspace and everything becomes horribly slow.
I'm thinking about writing a tools to speed up the process:
Given a name of spring bean / the class of bean, the tool will search in the whole class path of application and returns:
The spring context file which declares the bean.
The .jar file that contains the spring context file.
Extract the content spring context file and show it directly to user
If user provides the root spring context file, the tool will shows the import chain to the destination spring context file.
It does not take lots of time to implement this but I just wonder whether somebody has already implement it? I just don't want to reinvent the wheel.
I found the project https://github.com/julior/spring-inspector. It's interesting, but it's not what I need.
If you know a tool like the one above, I'd be very happy to know about it.
You totally should look at Intellij IDEA's great support for Spring Framework.
More information available here: http://www.jetbrains.com/idea/features/spring_framework.html
To find interesting bean in xml config you just need to click on green bean image.
Intellij IDEA is definitely worth its price.

How to separate spring contexts in intelliJ IDEA

I have a problem configuring IntelliJ IDEA for developing spring and maven powered application.
App has two separate spring configurations for production and test purposes. In spring facet props in IDEA I created two different file sets but when configuring one of contexts IDEA shows variants for both ones in code completion. How can I deal with this?
Thanks
Aleksander
The only option is to create two different filesets of spring config. If the beans are defined in both the filesets, it would links the beans to both filesets. Obviously I don't think it (or any IDE) is capable of resolving if it has to use main/test filesets based on your code path. Hope they would enhance the sprint context(fileset) resolution based on the code path (source/test). But it would be difficult for the IDE as the main business logic falls in both main/test context during the flow.
IDEA 2016.2 has checkbox: Check test files:
After check on IDEA stop complain, that test files not included in Spring Facet.
Try to play with it.

Resources