先给出上章Struts2简单构建中的struts.xml的代码
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="wul" namespace="/" extends="struts-default"> <action name="product-input" class="com.opensymphony.xwork2.ActionSupport" method="execute"> <result>/WEB-INF/pages/input.jsp</result> </action> <action name="product-save" class="com.wul.strust2.helloworld.Product" method="save"> <result name="details">/WEB-INF/pages/details.jsp</result> </action> </package> </struts>
extends :当前包继承哪个包,继承的,即可以继承其中所有的配置,通常情况下继承struts-default ,struts-default这个包在struts-default.xml文件中定义
namespace="/java"的话,请求要在servletPath前加/java : http://localhost:8080/contextPath/namespace/actionName.action
class 的默认值为:com.opensymphony.xwork2.ActionSupport
method:的默认值为:execute , 可以自定义方法,如”product-save“action中的save方法
type:表示结果的类型,默认值是dispatch
>redirect 重定向
>redirectAction 重定向到一个action
>chain 转发到一个action
重定向:以前的request中存放的变量全部失效,并进入一个新的request作用域
转发:以前的request中存放的变量不会失效,就像把两个页面拼到了一起
下面给一个不同result的type的实例
TestAction.java
package com.zucc.wul.cn.action; public class TestAction { public String execute(){ System.out.println("TestAction's execute...."); return "success"; } }
TestResultAction.java
package com.zucc.wul.cn.action; public class TestResultAction { private int number; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String execute(){ String result=null; //根据请求参数number的值,返回对应的JSP页面 //1.若number除以3余0,返回success.jsp页面 if(number == 0) result = "success"; //2.若number除以3余1,返回login.jsp页面 if(number == 1) result = "login"; //3.若number除以3余2,返回index.jsp页面 if(number == 2) result = "index"; if(number == 3) result = "index2"; return result; } }index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Hello</title> </head> <body> <a href="product-input.action">Product Input</a> </body> </html>Register.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Insert title here</title> </head> <body> <form action="testResult"> <input type="text" name="number"/> <input type="submit" value = "submit"/> </form> </body> </html>Success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Insert title here</title> </head> <body> 注册成功 </body> </html>Fail.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Insert title here</title> </head> <body> 注册失败 </body> </html>
web.xml
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> <include file="struts-default.xml" /> <package name="default" extends="struts-default"> <action name ="testResult" class="com.zucc.wul.cn.action.TestResultAction"> <result name="success" type="dispatcher">/Success.jsp</result> <result name="login" type="redirect">/Fail.jsp</result> <!-- <result name="index" type="redirectAction"> <param name="actionName">testAction</param> <param name="namespace">/atguigu</param> </result> --> <!--通过redirect的响应类型也可以便捷的实现redirectAction的功能 --> <result name="index" type="redirect">/atguigu/testAction.action</result> <!-- 转发到一个action只能这样写 --> <result name="index2" type="chain"> <param name="actionName">testAction</param> <param name="namespace">/atguigu</param> </result> </action> </package> <package name="testPackage" namespace="/atguigu" extends="struts-default"> <action name="testAction" class="com.zucc.wul.cn.action.TestAction"> <result>/Success.jsp</result> </action> </package> </struts>redirectAction 重定向到一个action,需额外在定义一个package,其方法可以到struts/docs/docs/home.html里查询
注意,在result不同type的写法中,通过redirect的响应类型也可以便捷的实现redirectAction的功能,但转发到一个action只能这样写