【问题标题】:Calling Groovy classes from Java EE + adding to jsp从 Java EE 调用 Groovy 类 + 添加到 jsp
【发布时间】:2016-12-13 01:08:36
【问题描述】:

背景

您好,我是 Java EE 的新手。我正在关注this tutorial,它概述了如何从 java 项目中调用 groovy 类。这样做的原因是我构建的许多预先存在的代码都是用 groovy 编写的,并且对于我想做的事情来说是相当可扩展的。

上下文

从 Java 和 Groovy 的角度来看,该代码是有效的。但是,当启动 tomcat 服务器并打开 index.jsp 时,我无法点击代码。

查看index.jsp 文件时,我看到错误:cannot resolve variable for languagesentimentmessage

其他信息/想法

这可能与我的依赖关系有关吗? 我使用maven 导入ant:ant-antlr:1.6.5asm:asm-all:3.3.1jstl:jstl:1.2org.codehaus.groovy:groovy-all:2.2.1。他们是错的吗?在添加依赖之前,Java.sun.com/jsp/jstl/core 找不到(很明显)

预期输出

打开网络浏览器,页面显示“我正在使用 java!没关系....我是从 groovy 调用的,令人兴奋”

实际输出

我正在使用!那是

代码如下:

index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
  <title>Test Sandbox</title>
</head>
<body>
<p>I'm using <c:out value="${language}" />! That's <c:out value="${sentiment}" /></p>
<p><c:out value="${message}" /></p>
</body>
</html>

JavaServlet.java

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JavaServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("language", "java");
        req.getSession().setAttribute("sentiment", "ok...");
        resp.sendRedirect("index.jsp");
        req.getSession().setAttribute("message", main.message());
    }
}

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://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <servlet>
        <servlet-name>JavaServlet</servlet-name>
        <servlet-class>JavaServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>JavaServlet</servlet-name>
        <url-pattern>/javacallinggroovy</url-pattern>
    </servlet-mapping>

</web-app>

main.groovy

class main {

    static def message() {
        "I was called from Groovy. Exciting, isn't it?"
    }
}

【问题讨论】:

    标签: java jsp tomcat intellij-idea groovy


    【解决方案1】:

    请将这些添加到您的 pom.xml 中

    <!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    

    【讨论】:

    • 那行不通。 Intellij 与它有什么关系吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多