课程表

Struts2 教程

Struts2 标签

Struts2 集成

工具箱
速查手册

Redirect Action示例

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

  1. <action name="hello"
  2. class="com.w3xue.struts2.HelloWorldAction"
  3. method="execute">
  4. <result name="success" type="redirect">
  5. <param name="location">
  6. /NewWorld.jsp
  7. </param >
  8. </result>
  9. </action>
可参考上面所说的修改WebContent/WEB-INF/classes/struts.xml文件来定义redirect结果类型:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <constant name="struts.devMode" value="true" />
  7. <package name="helloworld" extends="struts-default">
  8. <action name="hello"
  9. class="com.w3xue.struts2.HelloWorldAction"
  10. method="execute">
  11. <result name="success" type="redirect">
  12. <param name="location">
  13. /NewWorld.jsp
  14. </param >
  15. </result>
  16. </action>
  17. <action name="index">
  18. <result >/index.jsp</result>
  19. </action>
  20. </package>
  21. </struts>
这里的NewWorld.jsp是一个新页面,当你的action返回“success”结果时,将产生redirect结果。我们保留WebContent/WEB-INF/lib/web.xml不做修改,其内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  7. id="WebApp_ID" version="3.0">
  8. <display-name>Struts 2</display-name>
  9. <welcome-file-list>
  10. <welcome-file>index.jsp</welcome-file>
  11. </welcome-file-list>
  12. <filter>
  13. <filter-name>struts2</filter-name>
  14. <filter-class>
  15. org.apache.struts2.dispatcher.FilterDispatcher
  16. </filter-class>
  17. </filter>
  18. <filter-mapping>
  19. <filter-name>struts2</filter-name>
  20. <url-pattern>/*</url-pattern>
  21. </filter-mapping>
  22. </web-app>
创建action类文件Java Resources/src/HelloWorldAction.java如下:

  1. package com.w3xue.struts2;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class HelloWorldAction extends ActionSupport{
  4. private String name;
  5. public String execute() throws Exception {
  6. return "success";
  7. }
  8. public String getName() {
  9. return name;
  10. }
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14. }
创建包含以下内容的主页面WebContent/WEB-INF/index.jsp:

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <%@ taglib prefix="s" uri="/struts-tags"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  5. "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <title>Hello World</title>
  9. </head>
  10. <body>
  11. <h1>Hello World From Struts2</h1>
  12. <form action="hello">
  13. <label for="name">Please enter your name</label><br/>
  14. <input type="text" name="name"/>
  15. <input type="submit" value="Say Hello"/>
  16. </form>
  17. </body>
  18. </html>
创建WebContent/WEB-INF/NewWorld.jsp,如果action返回“success”时,请求将执行redirect结果类型:

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <%@ taglib prefix="s" uri="/struts-tags"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  5. "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <title>Redirected Page</title>
  9. </head>
  10. <body>
  11. <h1>New Page after redirection</h1>
  12. </body>
  13. </html>
现在右键单击项目名称,然后单击“Export”>“WAR File”创建WAR文件。然后在Tomcat的webapps目录中部署这个WAR文件。最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/HelloWorldStruts2/index.action,将显示如下界面:

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

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号