jsp:forward 在 Java 中不使用 JSP 标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/245236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:58:41  来源:igfitidea点击:

jsp:forward in Java without using JSP tag?

javajspjsp-tagsforward

提问by Chris Carruthers

Is there a pure-Java equivalent to <jsp:forward page="..." /> that I can use within a <% ... %> block?

我可以在 <% ... %> 块中使用与 <jsp:forward page="..." /> 等效的纯 Java 吗?

For example, I currently have a JSP page something like this:

例如,我目前有一个 JSP 页面,如下所示:

<%
    String errorMessage = SomeClass.getInstance().doSomething();
    if (errorMessage != null) {
        session.setAttribute("error", errorMessage);
%>
<jsp:forward page="error.jsp" />
<%
    } else {
        String url = response.encodeRedirectURL("index.jsp");
        response.sendRedirect(url);
    }
%>

Having to break the <% ... %> block to use the jsp:forward is ugly and makes it harder to read due to indentation, among other things.

必须打破 <% ... %> 块才能使用 jsp:forward 是丑陋的,并且由于缩进等原因使阅读变得更加困难。

So, can I do the forward in the Java code without use the JSP tag?

那么,我可以在不使用 JSP 标签的情况下在 Java 代码中进行转发吗?

Something like this would be ideal:

这样的事情将是理想的:

<%
    String errorMessage = SomeClass.getInstance().doSomething();
    if (errorMessage != null) {
        session.setAttribute("error", errorMessage);
        someObject.forward("error.jsp");
    } else {
        String url = response.encodeRedirectURL("index.jsp");
        response.sendRedirect(url);
    }
%>

采纳答案by Adam

The someObjectyou are looking for is pageContext.

someObject你要找的是pageContext的

This object is implicitly defined in JSP, so you can use it like this:

这个对象是在 JSP 中隐式定义的,所以你可以像这样使用它:

pageContext.forward("<some relative jsp>");

回答by Hyman Leow

You really should try and avoid scriplets if you can, and in your case, a lot of what you are doing can be replaced with JSTL code. The following replacement for your example is much cleaner, IMO:

如果可以,您真的应该尽量避免使用脚本,在您的情况下,您正在做的很多事情都可以用 JSTL 代码替换。您的示例的以下替换更干净,IMO:

<%
  // Consider moving to a servlet or controller/action class
  String errorMessage = SomeClass.getInstance().doSomething();
  pageContext.setAttribute("errorMessage", errorMessage);
%>
<c:choose>
  <c:when test="${not empty errorMessage}">
    <c:set var="error" scope="session" value="${errorMessage}" />
    <jsp:forward page="error.jsp" />
  </c:when>
  <c:otherwise>
    <c:redirect url="index.jsp" />
  </c:otherwise>
</c:choose>

Ideally, you'd modify error.jsp so that the error message doesn't even need to be set in the session, but I didn't want to change your design too much.

理想情况下,您应该修改 error.jsp 以便甚至不需要在会话中设置错误消息,但我不想对您的设计进行太多更改。

回答by Hyman Leow

A simple approach:

一个简单的方法:

<%@page errorPage="Error.jsp" %>

<%
 String errorMessage = SomeClass.getInstance().doSomething();
 if (errorMessage != null) {
       throw new Exception(errorMessage); // Better throw the exception from doSomething()
 }
 pageContext.forward("index.jsp");
%>


Error.jsp
.........
<%@ page isErrorPage='true' %>
<%
out.print("Error!!!");  
out.print(exception.getMessage());
%>

Better approach:

更好的方法:

Invoke the doSomething() from a servlet. Set your error page in web.xml

从 servlet 调用 doSomething()。在 web.xml 中设置您的错误页面

<error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/WEB-INF/jsp/Error.jsp</location>
</error-page>