简体   繁体   English

如何从jsp调用Java方法?

[英]How to invoke a java method from jsp?

In my jsp, I have the following: 在我的jsp中,我具有以下内容:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:set scope="request" var="test" value="${com.xxx.foo.Bar.getBar()}" />

But this seems to store test as a string with the literal value of ${com.xxx.foo.Bar.getBar()} rather than the return value of that method (which is an enum). 但这似乎将test存储为字符串,其字面值为${com.xxx.foo.Bar.getBar()}而不是该方法的返回值(这是一个枚举)。

Here getBar() is an instance method, not a static method. 这里的getBar()是实例方法,而不是静态方法。

What am I doing wrong? 我究竟做错了什么?

As suggested by others in the comments, I solved this by creating a servlet and passing in the info to the jsp, like this: 正如其他人在评论中所建议的那样,我通过创建一个servlet并将信息传递给jsp来解决了这个问题,如下所示:

public class FooServlet extends HttpServlet
{
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException
    {
        Bar bar = new Bar();
        request.setAttribute("bar", bar.getFooBar() );
        request.getRequestDispatcher("/myPage.jsp").forward(request, response);
    }
}

In the jsp: 在jsp中:

<%=request.getAttribute("bar") %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM