la-team / light-admin

[PoC] Pluggable CRUD UI library for Java web applications
http://lightadmin.org
Apache License 2.0
665 stars 255 forks source link

Support for Spring Boot Embedded applications #131

Closed jbcpollak closed 10 years ago

jbcpollak commented 10 years ago

Hi, I'd really like to use this with an embedded Spring Boot application, can you please explain how that can be done?

max-dev commented 10 years ago

Hi Joshua, running LightAdmin/Spring Boot in embedded mode is not supported yet, 'cause it still needs a couple of tweaks to be done (haven't investigated it yet).

jbcpollak commented 10 years ago

Hi @max-dev , do you know what is needed? If you can point me in the right direction, I might be able to help out. I saw this article, I don't know if its as simple as this or not: http://stackoverflow.com/questions/20915528/how-can-i-register-a-secondary-servlet-with-spring-boot

jimmyfm commented 10 years ago

I tried to embed the non working demo into a real project and i had errors since boot could not find the templates

jbcpollak commented 10 years ago

Hi @jimmyfm - You can solve this by excluding the templates in your dependency manager, it doesn't look like LightAdmin uses them. If you use Maven, try this:

        <dependency>
            <groupId>org.lightadmin</groupId>
            <artifactId>lightadmin</artifactId>
            <version>1.0.0.M3-SNAPSHOT</version>
            <exclusions>
                <exclusion>
                    <artifactId>tiles-velocity</artifactId>
                    <groupId>org.apache.tiles</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>tiles-freemarker</artifactId>
                    <groupId>org.apache.tiles</groupId>
                </exclusion>
            </exclusions>
        </dependency>

The exclusion really should be in LA's pom file.

Unfortunately, you'll get your code running, but the LA servlet still won't be initialized.

jimmyfm commented 10 years ago

I tried with the exclusions but i keep having the same problem:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Aug 28 17:09:22 CEST 2014
There was an unexpected error (type=Not Found, status=404).
/views/layout/internal-layout.jsp

Here the code i used: https://github.com/jimmyfm/lightadmin-spring-boot

jbcpollak commented 10 years ago

@jimmyfm - I'm new to spring-boot but as far as I can tell this is a standard error page you'll see for any 404 errors, until something is registered on /error. This is happening because the LA servlet is not being started, so nothing is registered on the URL you specified ("/admin" or whatever)

If you bundle your spring-boot app as a war and deploy it into a servlet container, you should see LA start correctly. I created this bug to sort out how to get the servlet started correctly when built as a jar.

max-dev commented 10 years ago

BTW, I've already dropped tiles-velocity & tiles-freemarker dependencies. You don't have to exclude them explicitly anymore.

max-dev commented 10 years ago

Hi @jbcpollak, I've almost finished with stabilization of the latest LightAdmin version. Right after finishing, I'll take a look at "embedded" mode.

max-dev commented 10 years ago

@jimmyfm @jbcpollak: Successfully ran lightadmin-spring-boot in embedded mode (tomcat)

jbcpollak commented 10 years ago

Hi @max-dev - This is great news. Is a compatible version published to a maven repo? I'll try it now!

max-dev commented 10 years ago

It's in the latest 1.0.0.BUILD-SNAPSHOT version available from LightAdmin Nexus. Will release it to Maven Central soon.

jbcpollak commented 10 years ago

Ok, I am using the snapshot and added the servletContextInitializer() function, and it looks like lightadmin starts up now. I am getting this error:

    Caused by: java.lang.NoClassDefFoundError: javax/servlet/jsp/JspFactory
        at org.lightadmin.core.view.LightAdminSpringTilesInitializer$SpringTilesContainerFactory.createAttributeEvaluatorFactory(LightAdminSpringTilesInitializer.java:148) ~[lightadmin-1.0.0.BUILD-SNAPSHOT.jar:1.0.0.BUILD-SNAPSHOT]
        at org.apache.tiles.factory.BasicTilesContainerFactory.createContainer(BasicTilesContainerFactory.java:106) ~[tiles-core-2.2.2.jar:2.2.2]
        at org.apache.tiles.startup.AbstractTilesInitializer.createContainer(AbstractTilesInitializer.java:124) ~[tiles-core-2.2.2.jar:2.2.2]

I'm not sure why, I assume it is a dependency issue, but I'll dig into it.

max-dev commented 10 years ago

Have you checked you POM against the one from LightAdmin-SpringBoot project? javax.servlet.jsp.JspFactory is located in tomcat-embed-jasper dependency.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-jetty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
jbcpollak commented 10 years ago

ok, I got it working. I had to downgrade my tomcat version from 8.0.9 to 8.0.8 and add this dependency (which you had in your example). Oddly enough, it works with scope=provided, which doesn't make much sense to me.

Tomcat is the default, so I don't think you need to exclude jetty and include tomcat. I don't have that and am using Tomcat. In-fact, I had the opposite for a while trying to get Jetty working, but eventually gave up.

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

Thanks again for working on this!

jimmyfm commented 10 years ago

How do you compile and launch the spring boot application?

To compile i use

mvn clean package

Then i launch with

java $JAVA_OPTS -Dserver.port=$PORT -jar target/*.jar

The maven package create a runnable jar with this manifest

Manifest-Version: 1.0
Build-Jdk: 1.7.0_65
Start-Class: org.lightadmin.boot.LightAdminBootApplication
Created-By: Apache Maven 3.0.5
Spring-Boot-Version: 1.1.5.RELEASE
Main-Class: org.springframework.boot.loader.JarLauncher
Archiver-Version: Plexus Archiver

And launching trough org.springframework.boot.loader.JarLauncher as described below bring up the error i posted earlier. Sorry for not digging deeper in the first place.

jbcpollak commented 10 years ago

@jimmyfm - try making your main class look like this:

https://github.com/la-team/light-admin/blob/d227683fe8fa67ff0abb4a2d63eab9dce1e0afc1/lightadmin-spring-boot/src/main/java/org/lightadmin/boot/LightAdminBootApplication.java

And you might want to check out this entire project as a template:

https://github.com/la-team/light-admin/tree/d227683fe8fa67ff0abb4a2d63eab9dce1e0afc1/lightadmin-spring-boot

jimmyfm commented 10 years ago

I used exactly that class, before posting again i cloned the latest version of the lightadmin-spring-boot and started from there with the process i described below.

Just to be sure here the code to replicate the error: https://github.com/jimmyfm/lightadmin-spring-boot

max-dev commented 10 years ago

@jimmyfm I've just finished releasing the last version 1.0.0.RELEASE and checked embedded mode works as expected. Please clone the latest lightadmin-spring-boot and run it using:

$ mvn package && java -jar target/lightadmin-boot.jar

or

$ mvn spring-boot:run

And you should have it up and running on localhost:8080/admin. Feel free to ask any questions