JSP部署tomcate时重复局部变量

发布于 2024-10-22 04:35:21 字数 1287 浏览 1 评论 0原文

<% User loginUser = (User)session.getAttribute("loginUser");
int userlevel = Integer.parseInt( (request.getAttribute("userlevel")).toString());
String status = (String)request.getAttribute("formstatus");
%>

根据用户级别我必须在同一页面上包含不同的文件 例如,

 <% if(userlevel > 2){ %>
                                          <%@include file="apr-part3.jsp"%>
                                    <%}else{ %>
                                        You have not permission to view this part
                                    <% } %>

apr-part3.jsp 页面中,某些部分对于用户 3 和 4 来说是通用的 我必须再次检查 apr-part3.jsp 中的用户级别,如下所示 在 apr-part3.jsp 中,

<% User loginUser = (User)session.getAttribute("loginUser");
int userlevel = Integer.parseInt( (request.getAttribute("userlevel")).toString());
String status = (String)request.getAttribute("formstatus");
%>
<% if(userlevel==3){
     do something
}if(userlevel==4){
do something
}

在 eclipse 中一切运行正常,但在 tomcat apache 上部署时 它给出了错误

org.apache.jasper.JasperException:无法编译 JSP 类:

jsp 文件 /jsp-page/apr-part3.jsp 中的第 2 行发生错误 重复的局部变量loginUser

如何避免这些错误

<% User loginUser = (User)session.getAttribute("loginUser");
int userlevel = Integer.parseInt( (request.getAttribute("userlevel")).toString());
String status = (String)request.getAttribute("formstatus");
%>

based on userlevel I have to include different file on the same page
for example

 <% if(userlevel > 2){ %>
                                          <%@include file="apr-part3.jsp"%>
                                    <%}else{ %>
                                        You have not permission to view this part
                                    <% } %>

in apr-part3.jsp page some part is common for user 3 and 4
I have to check user level in apr-part3.jsp as follows again
in apr-part3.jsp

<% User loginUser = (User)session.getAttribute("loginUser");
int userlevel = Integer.parseInt( (request.getAttribute("userlevel")).toString());
String status = (String)request.getAttribute("formstatus");
%>
<% if(userlevel==3){
     do something
}if(userlevel==4){
do something
}

Everything runs fine in eclipse but when deploying on tomcat apache
it gives the error

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 2 in the jsp file: /jsp-page/apr-part3.jsp
Duplicate local variable loginUser

How can I avoid these errors

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳公公是暖光 2024-10-29 04:35:21

@include 将在编译时包含 JSP。所以它基本上最终会出现在同一个 Java 代码块中。您有 2 个选择:

  • 使用 代替。
  • 删除 apr-part3.jsp 中的重新声明。

更好的是,只使用 JSTL/EL。

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:choose>
    <c:when test="${userlevel > 2}">
        <jsp:include page="apr-part3.jsp" />
    </c:when>
    <c:otherwise>
        You have not permission to view this part.
    </c:otherwise>
</c:choose>

同样,请求属性 loginUser 可通过 ${loginUser} 获取,status 可通过 ${formstatus} 获取。另请参阅如何避免 JSP 文件中出现 Java 代码?

The @include will include the JSP during compile time. So it basically ends up in the same block of Java code. You have 2 options:

  • Use <jsp:include> instead.
  • Get rid of the redeclarations in apr-part3.jsp.

Better yet, is to just use JSTL/EL.

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:choose>
    <c:when test="${userlevel > 2}">
        <jsp:include page="apr-part3.jsp" />
    </c:when>
    <c:otherwise>
        You have not permission to view this part.
    </c:otherwise>
</c:choose>

Equivalently, the request attribute loginUser is available by ${loginUser} and status by ${formstatus}. See also How to avoid Java code in JSP files?

拥有 2024-10-29 04:35:21

这是一个小问题,因为它与您的问题标题相关,但与您的示例代码主体无关。

将标签与 scriptlet 一起使用时也会出现此问题。我知道,我知道,这不是最佳做法,但它确实发生了。至少在WebLogic 10.2.3 中是这样。

如果您有一个标记将变量添加到页面上下文以便在 jsp 内使用,就会出现这种情况,例如:

public int doStartTag() throws JspException {
    String myvar = ...;
    pageContext.setAttribute("myvar", myvar);

    return EVAL_BODY_INCLUDE;
}

如果您随后在 using jsp 内的 scriptlet 中声明一个与上下文上的属性名称同名的 var,那么您jsp 编译时出现重复的局部变量错误。例如:

<%
    String myvar = (String)pageContext.getAttribute("myvar");
    ...
 %>

原因是 jsp 编译的 servlet 包含注入的标记代码和样板代码,这些代码实际上已经使用从页面上下文获取的属性值创建了一个“String myvar”。因此,您的 scriptlet 声明实际上是多余的,因为数据已经可用。

检查预编译的 servlet 进行检查(Web 应用程序中的 WEB-INF/weblogic.xml jsp-descriptor 配置)。

This is a small aside as it relates to your question header but not your example body of code.

This problem can occur when using tags with scriptlet too. I know, I know, it's not best practice but, it happens. At least this is the case in WebLogic 10.2.3.

The situation arises if you have a tag that adds a variable to the page context for consumption within the jsp, such as:

public int doStartTag() throws JspException {
    String myvar = ...;
    pageContext.setAttribute("myvar", myvar);

    return EVAL_BODY_INCLUDE;
}

If you then declare a var within scriptlet within the using jsp with the same name as the attribute name on the context then you get a duplicate local variable error on jsp compilation. For instance:

<%
    String myvar = (String)pageContext.getAttribute("myvar");
    ...
 %>

The reason being that the jsp's compiled servlet contains injected tag code and boilerplate code that actually already creates a "String myvar" with the value of the attribute taken from the page context. So, your scriptlet declaration is actually superfluous as the data is already available.

Check the pre-compiled servlet to check (WEB-INF/weblogic.xml jsp-descriptor config in your webapp).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文