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

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




<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.jspresult>
      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"%>
   
<html>
<head>
<title>Redirected Pagetitle>
head>
<body>
   <h1>New Page after redirectionh1>
body>
html>

现在右键单击项目名称,然后单击“Export”>“WAR File”创建WAR文件。然后在Tomcat的webapps目录中部署这个WAR文件。最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/Struts2Demo1/index.jsp,将显示如下界面:
Struts2 学习笔记之重定向(redirect)_第1张图片
在文本框中输入任何值并提交,你可以看到redirection后的下一页:
Struts2 学习笔记之重定向(redirect)_第2张图片

你可能感兴趣的:(java,Struts2,jsp)