×

Struts2 教程

Struts2 概述Struts2 MVC架构Struts2 环境设置Struts2 架构Struts2 Hello World示例Struts2 配置文件Struts2 Actions动作Struts2 拦截器Struts2 结果类型Struts2 值栈/OGNLStruts2 文件上传Struts2 数据库访问Struts2 发送电子邮件Struts2 验证框架Struts2 本地化/国际化(i18n)Struts2 类型转换Struts2 主题和模板Struts2 异常处理Struts2 注释

Struts2 标签

Struts2 控制标签Struts2 数据标签Struts2 表单标签Struts2 Ajax标签

Struts2 集成

Struts2 Spring集成Struts2 Tiles集成Struts2 Hibernate集成

Struts2 Redirect Action示例


Struts2 结果类型Struts2 结果类型


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



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

可参考上面所说的修改WebContent/WEB-INF/classes/struts.xml文件来定义redirect结果类型:



<?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.bootwiki.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结果。我们保留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>

创建action类文件Java Resources/src/HelloWorldAction.java如下:



package com.bootwiki.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,如果action返回“success”时,请求将执行redirect结果类型:



<%@ 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>

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



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




Struts2 结果类型Struts2 结果类型


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)