Struts2重定向动作怎么配置

发布时间:2022-09-29 11:27:22 作者:iii
来源:亿速云 阅读:110

本文小编为大家详细介绍“Struts2重定向动作怎么配置”,内容详细,步骤清晰,细节处理妥当,希望这篇“Struts2重定向动作怎么配置”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

重定向结果类型调用标准的response.sendRedirect()方法,导致浏览器创建到给定位置的新请求。

我们可以在<result...>元素的主体中或作为<param name = "location">元素提供位置。重定向还支持parse参数。这是一个使用XML配置的示例

<action name = "hello" 
   class = "com.tutorialspoint.struts2.HelloWorldAction"
   method = "execute">   <result name = "success" type = "redirect">
       <param name = "location">
         /NewWorld.jsp      </param >
   </result></action>

因此,只需修改您的WebContent/WEB-INF/classes/struts.xml文件以定义上述重定向类型

<?xml version = "1.0" Encoding = "UTF-8"?><!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
   <constant name = "struts.devMode" value = "true" />
   <package name = "helloworld" extends = "struts-default">
      <action name = "hello" 
         class = "com.tutorialspoint.struts2.HelloWorldAction"
         method = "execute">
         <result name = "success" type = "redirect">
            <param name = "location">
             /NewWorld.jsp            </param >
        </result>
      </action>
      <action name = "index"> 
         <result >/index.jsp</result>
      </action>
   </package></struts>

这里NewWorld.jsp是一个新页面,只要您的操作返回“成功”,您就会被重定向到该页面。让我们保持WebContent/WEB-INF/lib/web.xml不变,因此其内容如下

<?xml version = "1.0" Encoding = "UTF-8"?><web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns = "http://java.sun.com/xml/ns/javaee" 
   xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id = "WebApp_ID" version = "3.0">
   <display-name>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>   
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher      </filter-class>
   </filter>
   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping></web-app>

创建动作类文件Java Resources/src/HelloWorldAction.java如下

package com.tutorialspoint.struts2;import com.opensymphony.xwork2.ActionSupport;public class HelloWorldAction extends ActionSupport{   private String name;   public String execute() throws Exception {      return "success";
   }   
   public String getName() {      return name;
   }   public void setName(String name) {      this.name = name;
   }
}

让我们使用以下内容创建主页WebContent/WEB-INF/index.jsp

<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
   pageEncoding = "ISO-8859-1"%><%@ taglib prefix = "s" uri = "/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd"><html>
   <head>
      <title>Hello World</title>
   </head>
   <body>
      <h1>Hello World From Struts2</h1>
      <form action = "hello">
         <label for = "name">Please enter your name</label><br/>
         <input type = "text" name = "name"/>
         <input type = "submit" value = "Say Hello"/>
      </form>
   </body></html>

让我们创建WebContent/WEB-INF/NewWorld.jsp,如果操作返回“成功”,请求将被重定向

<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
   pageEncoding = "ISO-8859-1"%><%@ taglib prefix = "s" uri = "/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd"><html>
   <head>
      <title>Redirected Page</title>
   </head>
   <body>
      <h1>New Page after redirection</h1>
   </body></html>

现在右键单击项目名称,然后单击导出 > WAR 文件以创建一个 War 文件。然后在 Tomcat 的 webapps 目录中部署这个 WAR。最后,启动 Tomcat 服务器并尝试访问 URL http://localhost:8080/HelloWorldStruts2/index.action。这将为您提供以下屏幕

Struts2重定向动作怎么配置

在文本框中输入任何值并提交页面。重定向后您应该会看到下一页

Struts2重定向动作怎么配置

读到这里,这篇“Struts2重定向动作怎么配置”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. cc.Node.动作
  2. Struts2 - 配置备忘

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

struts2

上一篇:Struts2文件如何上传

下一篇:Struts2的概念是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》