Actions是Struts2框架的核心,因为它们适用于任何MVC(Model View Controller)框架。 每个URL映射到特定的action,其提供处理来自用户的请求所需的处理逻辑。
但action还有另外两个重要的功能。 首先,action在将数据从请求传递到视图(无论是JSP还是其他类型的结果)方面起着重要作用。 第二,action必须协助框架确定哪个结果应该呈现在响应请求的视图中。
创建Action
Struts2中actions的唯一要求是必须有一个无参数方法返回String或Result对象,并且必须是POJO。如果没有指定no-argument方法,则默认是使用execute()方法。你还可以扩展ActionSupport类,该类可实现六个接口,包括Action接口。Action的接口如下:
- 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示例中的action方法:
- package com.w3xue.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;
- }
- }
为了说明action方法控制视图的要点,让我们对execute方法进行以下更改,并扩展ActionSupport类如下:
- package com.w3xue.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,所以我们可以使用String常量、SUCCESS和ERROR。 现在,让我们修改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.w3xue.struts2.HelloWorldAction"
- method="execute">
- <result name="success">/HelloWorld.jsp</result>
- <result name="error">/AccessDenied.jsp</result>
- </action>
- </package>
- </struts>
创建视图
让我们在你的eclipse项目的WebContent文件夹中创建下面的jsp文件HelloWorld.jsp。右键单击项目资源管理器中的WebContent文件夹,然后选择“New”> “JSP File”。如果返回结果是SUCCESS将调用此文件(这个字符串常量“success”是在Action接口中定义的):
- <%@ 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>
如果action的结果是ERROR,即字符串常量为“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文件。此文件将用作初始的action 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文件的需求,所以我们可以使用前面在Hello World示例章节中创建的的web.xml文件。现在,我们准备好使用Struts 2框架运行我们的Hello World应用程序了。
执行应用程序
右键单击项目名称,然后单击“Export”>“WAR File”创建WAR文件。 然后在Tomcat的webapps目录中部署这个WAR文件。最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/HelloWorldStruts2/index.jsp。显示的图示如下:

让我们输入一个单词为“SECRET”,你会看到如下页面:

现在输入除“SECRET”之外的任何词,你会看到如下页面:

创建多个Actions
你会需要频繁定义多个action来处理不同的请求,并为用户提供不同的URL,因此你将如下定义的不同类:
- package com.w3xue.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文件中的配置这些action:
- <?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.w3xue.struts2.HelloWorld"
- method="execute">
- <result name="success">/HelloWorld.jsp</result>
- <result name="error">/AccessDenied.jsp</result>
- </action>
- <action name="something"
- class="com.w3xue.struts2.SomeOtherClass"
- method="execute">
- <result name="success">/Something.jsp</result>
- <result name="error">/AccessDenied.jsp</result>
- </action>
- </package>
- </struts>
正如你在上面的例子中看到的,action的结果SUCCESS和ERROR是重复的。为了解决这个问题,建议您创建一个包含结果的类。
转载本站内容时,请务必注明来自W3xue,违者必究。