简体   繁体   English

如何通过使用JavaScript函数重定向到Struts2中的另一个JSP页面

[英]How to redirect to another jsp page in Struts2 by using JavaScript function

I want to redirect my jsp to another jsp page by using JavaScript function when I open the BeforeLogin page. 当我打开BeforeLogin页面时,我想使用JavaScript函数将jsp重定向到另一个jsp页面。 But I got below error message. 但我得到以下错误信息。

The Struts dispatcher cannot be found. 找不到Struts调度程序。 This is usually caused by using Struts tag without the associated filter. 这通常是由于未使用关联过滤器而使用Struts标签引起的。 Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. 仅当请求已通过其Servlet过滤器(该过滤器初始化此标签所需的Struts调度程序)时,Struts标记才可用。

I am not sure what to do. 我不确定该怎么办。

BeforeLogin.jsp: BeforeLogin.jsp:

function newpage(){
    window.open("Login.jsp");
}

<body onLoad="goNewWin()">
<a href="BeforeLogin.jsp">click here to login</a>
</body>

Login.jsp: Login.jsp:

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head>
<title>
   <s:text name="Login"/>
</title>
</head>
<body> ..... </body>

web.xml: web.xml:

<filter>
    <filter-name>DispatcherFilter</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>DispatcherFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

struts.xml: struts.xml:

<package name="struts2" namespace="/" extends="struts-default">

<action name="login" class="com.action.LogonAction">
        <result name="input">Login.jsp</result>
</action>

This error is created because you're calling a JSP that is trying to use Struts tags without having passed through an action first, and hence not finding the required underlying structure. 产生此错误的原因是,您正在调用一个试图使用Struts标记的JSP,而没有先通过一个动作,因此找不到所需的基础结构。

Struts2 is an MVC framework, which design implies that a new request must pass through a (C)ontroller (the action) and then be rendered through a (V)iew (the JSP). Struts2是一个MVC框架,该设计意味着新的请求必须通过(C)托管程序(动作),然后通过(V)iew(JSP)进行呈现。

You must avoid calling JSPs directly, you must instead call the login action, that after its execution will dispatch the login JSP. 您必须避免直接调用JSP,而必须调用login操作,该操作执行后将分派登录JSP。

window.open("login.action");

Create actionless result and use it instead of JSP. 创建不采取行动的结果,并使用它代替JSP。

struts.xml: struts.xml:

<action name="showLogin">
        <result>Login.jsp</result>
</action>

JS: JS:

function newpage(){
    window.open("showLogin");
}

Explanation: 说明:

The error clearly says that 该错误清楚地表明

This is usually caused by using Struts tags without the associated filter 这通常是由于使用不带相关过滤器的Struts标记引起的

it means that you have a JSP and Struts tags inside it, but this JSP is not used by the filter, because it could be a welcome file, ie index.jsp , error code file, ie 404.jsp , configured in the web.xml . 这意味着您内部有一个JSP和Struts标记,但是过滤器未使用此JSP,因为它可能是在web.xml配置的欢迎文件,即index.jsp ,错误代码文件,即404.jsp web.xml

These resources are handled by the web server before any filter or servlet mapped to the resource is being involved. 在涉及映射到该资源的任何过滤器或Servlet之前,Web服务器将处理这些资源。

Usually welcome files contain a redirect code to a valid action which then dispatch to a JSP that can have Struts tags. 通常,欢迎文件包含一个指向有效动作的重定向代码,然后将其分配给可以具有Struts标记的JSP。 Don't use JSPs directly in your application, use actions instead. 不要在应用程序中直接使用JSP,而应使用操作。

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

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