开发者社区> 问答> 正文

jfinal maven项目jetty如何使用支持jsp - jfinal报错

我在IntelliJ 搭建jfinal + maven环境,依赖如下:


<dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp.jstl</groupId>
      <artifactId>javax.servlet.jsp.jstl-api</artifactId>
      <version>1.2.1</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty.orbit</groupId>
      <artifactId>org.apache.taglibs.standard.glassfish</artifactId>
      <version>1.2.0.v201112081803</version>
    </dependency>

    <dependency>
      <groupId>com.jfinal</groupId>
      <artifactId>jfinal</artifactId>
      <version>2.2</version>
    </dependency>
   
<dependency>
      <groupId>com.jfinal</groupId>
      <artifactId>jetty-server</artifactId>
      <version>8.1.8</version>
      <!--<scope>provided</scope>-->
    </dependency>

  </dependencies>

  <build>
    <plugins>
      
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.3.10.v20160621</version>
        <configuration>
          <httpConnector>
            <port>8080</port>
          </httpConnector>
          <stopPort>9966</stopPort>
          <stopKey>stop</stopKey>
          <scanIntervalSeconds>5</scanIntervalSeconds>
          <webApp>
            <contextPath>/</contextPath>
          </webApp>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>



启动类:



package com.ef.demo.common;
import com.jfinal.core.JFinal;
public class Stratup {

    public static void main(String[] args) {
        JFinal.start("src/main/webapp", 8080, "/", 5);
    }
}



Controller:



public class IndexController extends Controller {
    public void index(){
//        renderText("hello JFinal world!");//正常
        renderJsp("index.jsp");//报错: JSP support not configured
    }
}


以上配置启动正常但是访问jsp页面会报错:

JSP support not configured

我有2个问题:


1. 如何配置在开发环境(IntelliJ IDEA)maven+jetty+jsp(我已经尝试把官demo的jar的依赖都加进去了但还是不能renderJSP  )的依赖?

2. pom.xml 里面 jetty server的依赖的groupid用com.jfinal和用org.eclipse.jetty区别是什么?(如果把jfinal的jettey改成相同版本的eclipse的jetty则启动报错: Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jetty/webapp/WebAppContext )

@JFinal 或者谁能帮忙? 谢谢

展开
收起
montos 2020-05-31 09:26:29 2356 0
1 条回答
写回答
取消 提交回答
  • Idea你就用你的jetty-maven-plugin插件启动就好了。

    pom里面jetty-server scope加上另外把JFinal和junit留下,其他的去掉。加上这个:

    <dependency>
    	<groupId>javax.servlet</groupId>
    	<artifactId>jstl</artifactId>
    	<version>1.2</version>
    </dependency>




    启动方法看图。

    ######

    下面给出 dependency:

    <!-- Jetty JSP:根据 Dependency Hierarchy 视图拆分成了七个 dependency,
                - 方便项目以及便于 导出 war的在jetty与tomcat间无缝迁移
                - Dependencies: http://mvnrepository.com/artifact/org.eclipse.jetty/jetty-jsp
    	<dependency>
    		<groupId>org.eclipse.jetty</groupId>
    		<artifactId>jetty-jsp</artifactId>
    		<version>8.1.8.v20121106</version>
    		<scope>provided</scope>
    	</dependency>
    	 -->
    	<dependency><!-- jetty-server-8.1.8 开发时JSP 支持 -->
    		<groupId>org.eclipse.jetty.orbit</groupId>
    		<artifactId>javax.servlet.jsp</artifactId>
    		<version>2.2.0.v201112011158</version>
    		<scope>provided</scope>
    	</dependency>
    	<dependency><!-- jetty-server-8.1.8 开发时JSP 支持 -->
    		<groupId>org.eclipse.jetty.orbit</groupId>
    		<artifactId>org.apache.jasper.glassfish</artifactId>
    		<version>2.2.2.v201112011158</version>
    		<scope>provided</scope>
    	</dependency>
    	<dependency><!-- jetty-server-8.1.8 开发时JSP 支持 -->
    		<groupId>org.eclipse.jetty.orbit</groupId>
    		<artifactId>javax.el</artifactId>
    		<version>2.2.0.v201108011116</version>
    		<scope>provided</scope>
    	</dependency>
    	<dependency><!-- jetty-server-8.1.8 开发时JSP 支持 -->
    		<groupId>org.eclipse.jetty.orbit</groupId>
    		<artifactId>com.sun.el</artifactId>
    		<version>2.2.0.v201108011116</version>
    		<scope>provided</scope>
    	</dependency>
    	<dependency><!-- jetty-server-8.1.8 开发时JSP 支持 -->
    		<groupId>org.eclipse.jetty.orbit</groupId>
    		<artifactId>org.eclipse.jdt.core</artifactId>
    		<version>3.7.1</version>
    		<scope>provided</scope>
    	</dependency>
    	<dependency><!-- JSTL 支持 -->
    		<groupId>org.eclipse.jetty.orbit</groupId>
    		<artifactId>javax.servlet.jsp.jstl</artifactId>
    		<version>1.2.0.v201105211821</version>
    		<exclusions><!-- 避免导出 servlet jar 包 -->
    		    <exclusion>
    		        <groupId>org.eclipse.jetty.orbit</groupId>
    		        <artifactId>javax.servlet</artifactId>
    		    </exclusion>
    		</exclusions>
    	</dependency>
    	<dependency><!-- JSTL 支持 -->
    		<groupId>org.eclipse.jetty.orbit</groupId>
    		<artifactId>org.apache.taglibs.standard.glassfish</artifactId>
    		<version>1.2.0.v201112081803</version>
    		<exclusions><!-- 避免导出 servlet jar 包 -->
    		    <exclusion>
    		        <groupId>org.eclipse.jetty.orbit</groupId>
    		        <artifactId>javax.servlet</artifactId>
    		    </exclusion>
    		</exclusions>



        特别注意,其实最简单的方式,只需要第一个 jetty-jsp 即可,但 war 包打出来会包含很多 jetty 的东东,而通常你的生产环境用的是 tomcat,所以又需要去掉这些东东,所以上面做了一个拆分,并将 scope 全部声明为了 provided,打 war 包时不会包含这些东东。




    ######

    引用来自“JFinal”的评论

    下面给出 dependency:

    <!-- Jetty JSP:根据 Dependency Hierarchy 视图拆分成了七个 dependency,
                - 方便项目以及便于 导出 war的在jetty与tomcat间无缝迁移
                - Dependencies: http://mvnrepository.com/artifact/org.eclipse.jetty/jetty-jsp
    	<dependency>
    		<groupId>org.eclipse.jetty</groupId>
    		<artifactId>jetty-jsp</artifactId>
    		<version>8.1.8.v20121106</version>
    		<scope>provided</scope>
    	</dependency>
    	 -->
    	<dependency><!-- jetty-server-8.1.8 开发时JSP 支持 -->
    		<groupId>org.eclipse.jetty.orbit</groupId>
    		<artifactId>javax.servlet.jsp</artifactId>
    		<version>2.2.0.v201112011158</version>
    		<scope>provided</scope>
    	</dependency>
    	<dependency><!-- jetty-server-8.1.8 开发时JSP 支持 -->
    		<groupId>org.eclipse.jetty.orbit</groupId>
    		<artifactId>org.apache.jasper.glassfish</artifactId>
    		<version>2.2.2.v201112011158</version>
    		<scope>provided</scope>
    	</dependency>
    	<dependency><!-- jetty-server-8.1.8 开发时JSP 支持 -->
    		<groupId>org.eclipse.jetty.orbit</groupId>
    		<artifactId>javax.el</artifactId>
    		<version>2.2.0.v201108011116</version>
    		<scope>provided</scope>
    	</dependency>
    	<dependency><!-- jetty-server-8.1.8 开发时JSP 支持 -->
    		<groupId>org.eclipse.jetty.orbit</groupId>
    		<artifactId>com.sun.el</artifactId>
    		<version>2.2.0.v201108011116</version>
    		<scope>provided</scope>
    	</dependency>
    	<dependency><!-- jetty-server-8.1.8 开发时JSP 支持 -->
    		<groupId>org.eclipse.jetty.orbit</groupId>
    		<artifactId>org.eclipse.jdt.core</artifactId>
    		<version>3.7.1</version>
    		<scope>provided</scope>
    	</dependency>
    	<dependency><!-- JSTL 支持 -->
    		<groupId>org.eclipse.jetty.orbit</groupId>
    		<artifactId>javax.servlet.jsp.jstl</artifactId>
    		<version>1.2.0.v201105211821</version>
    		<exclusions><!-- 避免导出 servlet jar 包 -->
    		    <exclusion>
    		        <groupId>org.eclipse.jetty.orbit</groupId>
    		        <artifactId>javax.servlet</artifactId>
    		    </exclusion>
    		</exclusions>
    	</dependency>
    	<dependency><!-- JSTL 支持 -->
    		<groupId>org.eclipse.jetty.orbit</groupId>
    		<artifactId>org.apache.taglibs.standard.glassfish</artifactId>
    		<version>1.2.0.v201112081803</version>
    		<exclusions><!-- 避免导出 servlet jar 包 -->
    		    <exclusion>
    		        <groupId>org.eclipse.jetty.orbit</groupId>
    		        <artifactId>javax.servlet</artifactId>
    		    </exclusion>
    		</exclusions>



        特别注意,其实最简单的方式,只需要第一个 jetty-jsp 即可,但 war 包打出来会包含很多 jetty 的东东,而通常你的生产环境用的是 tomcat,所以又需要去掉这些东东,所以上面做了一个拆分,并将 scope 全部声明为了 provided,打 war 包时不会包含这些东东。




    谢谢2位的仔细回答,我综合了一下,既能达到最简配置又能在开发(jetty)和生产(tomcat)切换,选择通过jetty-maven-plugin 启动项目而不是在java运行Main方法启动。(后者需要去掉provide scope才能正常启动,但这样又会造成package时候有多余jar)为方便后来者,这里贴出:

    <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.8.2</version>
          <scope>test</scope>
        </dependency>
    
        <dependency>
          <groupId>com.jfinal</groupId>
          <artifactId>jfinal</artifactId>
          <version>2.2</version>
        </dependency>
    
        <dependency>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-server</artifactId>
          <version>8.1.8.v20121106</version>
          <scope>provided</scope>
        </dependency>
    
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.3.10.v20160621</version>
            <configuration>
              <httpConnector>
                <port>8080</port>
              </httpConnector>
              <stopPort>9966</stopPort>
              <stopKey>stop</stopKey>
              <scanIntervalSeconds>5</scanIntervalSeconds>
              <webApp>
                <contextPath>/</contextPath>
              </webApp>
            </configuration>
          </plugin>
    
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
              <source>1.7</source>
              <target>1.7</target>
              <encoding>UTF-8</encoding>
            </configuration>
          </plugin>
    
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4</version>
            <configuration>
              <encoding>UTF-8</encoding>
            </configuration>
          </plugin>
        </plugins>
      </build>

    运行mvn jetty:run命令即可启动工程




    ######回复 @如梦技术 : 嗯嗯 intellij 右边有很方便的菜单 单击即可######如图,点一下那个红框框里面的就好,不用自己敲命令。######

    @如梦技术 , 问一个相关问题 intellij如何设置自动编译,像这样设置无效:http://www.360doc.com/content/14/1110/08/16002580_423959393.shtml

    否则每次改java需要手动点make麻烦

    ######

    引用来自“sucanber”的评论

    @如梦技术 , 问一个相关问题 intellij如何设置自动编译,像这样设置无效:http://www.360doc.com/content/14/1110/08/16002580_423959393.shtml

    否则每次改java需要手动点make麻烦

    我是intellij 14######

    这里再补充一下,新版本 jfinal 在用整合的 Jetty 启动时,需要使用下面的 depencency,前面给出的不再适用:

    <!-- jsp support by jetty -->
    <dependency>
    	<groupId>org.eclipse.jetty</groupId>
    	<artifactId>jetty-jsp</artifactId>
    	<version>8.1.8.v20121106</version>
    	<scope>provided</scope>
    </dependency>

    如果是 IDEA 下开发,要将 scope 改成 compile,在打 war 包时注意要再改回来成为 provided,独立部署用的容器大多都有 jsp 支持的 jar 包

    2020-05-31 09:26:56
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Java Spring Boot开发实战系列课程【第15讲】:Spring Boot 2.0 API与Spring REST Docs实战 立即下载
Java Spring Boot开发实战系列课程【第7讲】:Spring Boot 2.0安全机制与MVC身份验证实战(Java面试题) 立即下载
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题) 立即下载