最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Struts2 学习笔记之重定向(redirect)

旗下网站admin46浏览0评论

Struts2 学习笔记之重定向(redirect)

Struts2 学习笔记之重定向(redirect)

redirect结果类型调用标准的response.sendRedirect()方法,使得浏览器向给定的位置创建一个新请求。我们可以在<result ...>元素的主体中或作为<param name="location">的元素中给定位置。redirect还支持parse参数,以下是使用XML配置的示例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    ".0.dtd">

<struts>
   <constant name="struts.devMode" value="true" />
   <package name="helloworld" extends="struts-default">

      <action name="hello" 
         class="cn.w3cschool.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是一个新页面,当你的action返回“success”结果时,将产生redirect结果。web.xml,HelloWorldAction.java,index.jsp不做修改;新建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" 
   ".dtd">
<html>
<head>
<title>Redirected Page</title>
</head>
<body>
   <h1>New Page after redirection</h1>
</body>
</html>

现在右键单击项目名称,然后单击“Export”>“WAR File”创建WAR文件。然后在Tomcat的webapps目录中部署这个WAR文件。最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/Struts2Demo1/index.jsp,将显示如下界面:

在文本框中输入任何值并提交,你可以看到redirection后的下一页:

发布评论

评论列表(0)

  1. 暂无评论