动作是struts2框架的核心,因为他们的任何mvc(模型 - 视图 - 控制器)框架。每个url将被映射到一个特定的动作,它提供了来自用户的请求提供服务所需的处理逻辑。
但动作也提供其他两个重要的能力。首先,操作从请求数据的传输中起着重要的作用,通过向视图,无论是一个jsp或其它类型的结果。二,动作必须协助的框架,在确定结果应该渲染视图,在响应该请求将被退回。
在struts2的动作,唯一的要求是必须有一个无参数的方法返回string或结果的对象,必须是一个pojo。如果不带参数的方法是不指定,则默认动作是使用execute()方法。
也可以选择扩展actionsupport类实现了6个接口,包括动作界面。动作界面如下:
public interface action { public static final string success = "success"; public static final string none = "none"; public static final string error = "error"; public static final string input = "input"; public static final string login = "login"; public string execute() throws exception; }
让我们来看看hello world示例的操作方法:
package com.h3.struts2; public class helloworldaction{ private string name; public string execute() throws exception { return "success"; } public string getname() { return name; } public void setname(string name) { this.name = name; } }
为了说明这一点,操作方法控制视图,让我们做出以下更改执行方法和扩展类actionsupport 如下:
package com.h3.struts2; import com.opensymphony.xwork2.actionsupport; public class helloworldaction extends actionsupport{ private string name; public string execute() throws exception { if ("secret".equals(name)) { return success; }else{ return error; } } public string getname() { return name; } public void setname(string name) { this.name = name; } }
在这个例子中,我们有一些在execute方法的逻辑来看待的name属性。如果属性等于字符串“secret”,我们返回success 的结果,否则我们返回error 的结果。因为我们已经扩展actionsupport,所以我们可以使用字符串常量的成功和错误。现在,让我们修改我们的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> <constant name="struts.devmode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="com.h3.struts2.helloworldaction" method="execute"> <result name="success">/helloworld.jsp</result> <result name="error">/accessdenied.jsp</result> </action> </package> </struts>
让我们创建以下jsp文件 helloworld.jsp 的webcontent文件夹在eclipse项目。要做到这一点,右键单击webcontent文件夹在项目资源管理器,选择new >jsp file。该文件将要求返回的结果是success,这是一个字符串常量“success”的定义在动作界面:
<%@ page contenttype="text/html; charset=utf-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>hello world</title> </head> <body> hello world, <s:property value="name"/> </body> </html>
以下是由框架的动作的结果将被调用的文件,该文件是等于字符串常量“错误”的error 。以下是accessdenied.jsp 的内容
<%@ page contenttype="text/html; charset=utf-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>access denied</title> </head> <body> you are not authorized to view this page. </body> </html>
我们还需要在webcontent文件夹中创建index.jsp。该文件将作为初始动作url,用户可以直接点击告诉struts 2框架调用helloworldaction类的 execute方法,并呈现 helloworld.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>
就是这样,不需要改变的web.xml文件,所以让我们用同一个web.xml,是之前我们已经创建了范例章。现在,我们已经准备好运行使用struts 2框架的 hello world应用程序。
右键点击项目名称,并单击 export > war file 创建一个war文件。然后在tomcat 的webapps目录下部署这个war。最后,启动tomcat服务器和尝试访问url http://localhost:8080/helloworldstruts2/index.jsp。这会给出以下画面:
让我们为“secret”,并输入一个字,应该看到以下页面:
现在输入任何单词而非“secret”,应该看到以下页面:
经常会定义一个以上的动作,以处理不同的请求,并提供不同的用户的url,因此可以定义不同的类定义如下:
package com.h3.struts2; import com.opensymphony.xwork2.actionsupport; class myaction extends actionsupport{ public static string good = success; public static string bad = error; } public class helloworld extends actionsupport{ ... public string execute() { if ("secret".equals(name)) return myaction.good; return myaction.bad; } ... } public class someotherclass extends actionsupport{ ... public string execute() { return myaction.good; } ... }
在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> <constant name="struts.devmode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="com.h3.struts2.helloworld" method="execute"> <result name="success">/helloworld.jsp</result> <result name="error">/accessdenied.jsp</result> </action> <action name="something" class="com.h3.struts2.someotherclass" method="execute"> <result name="success">/something.jsp</result> <result name="error">/accessdenied.jsp</result> </action> </package> </struts>
正如看到在上述假设的例子,动作的结果是重复的success和error。要解决这个问题,建议创建一个类包含结果的结果。