Struts 2 简明教程

Struts 2 - Results & Result Types

如前所述, <results> 标记在 Struts2 MVC 框架中扮演 view 的角色。此操作负责执行业务逻辑。执行业务逻辑后的下一步是使用 <results> 标记显示视图。

As mentioned previously, the <results> tag plays the role of a view in the Struts2 MVC framework. The action is responsible for executing the business logic. The next step after executing the business logic is to display the view using the <results> tag.

通常有一些导航规则附加到结果。例如,如果操作方法是验证用户,那么有三个可能的结果。

Often there is some navigation rules attached with the results. For example, if the action method is to authenticate a user, there are three possible outcomes.

  1. Successful Login

  2. Unsuccessful Login - Incorrect username or password

  3. Account Locked

在这种情况下,操作方法将配置三个可能的结果字符串和三个不同的视图来呈现结果。我们在前面的示例中已经看到过这一点。

In this scenario, the action method will be configured with three possible outcome strings and three different views to render the outcome. We have already seen this in the previous examples.

但是,Struts2 并没有将您绑定到使用 JSP 作为视图技术。毕竟,MVC 范例的全部目的是保持层分离和高度可配置。例如,对于 Web2.0 客户端,您可能想要返回 XML 或 JSON 作为输出。在这种情况下,您可以创建用于 XML 或 JSON 的新结果类型并实现此目的。

But, Struts2 does not tie you up with using JSP as the view technology. Afterall the whole purpose of the MVC paradigm is to keep the layers separate and highly configurable. For example, for a Web2.0 client, you may want to return XML or JSON as the output. In this case, you could create a new result type for XML or JSON and achieve this.

Struts 带有一些预定义的 result types 和我们已经看到过的 dispatcher 的默认结果类型,用于分发到 JSP 页面。Struts 允许您使用其他标记语言作为视图技术来呈现结果,流行的选择包括 Velocity, Freemaker, XSLTTiles

Struts comes with a number of predefined result types and whatever we’ve already seen that was the default result type dispatcher, which is used to dispatch to JSP pages. Struts allow you to use other markup languages for the view technology to present the results and popular choices include Velocity, Freemaker, XSLT and Tiles.

The Dispatcher Result Type

dispatcher 结果类型是默认类型,如果未指定其他结果类型,将使用此类型。它用于转发到服务器上的 servlet、JSP、HTML 页面等。它使用 RequestDispatcher.forward() 方法。

The dispatcher result type is the default type, and is used if no other result type is specified. It’s used to forward to a servlet, JSP, HTML page, and so on, on the server. It uses the RequestDispatcher.forward() method.

我们在前面的示例中看到了“简写”版本,其中我们提供了一个 JSP 路径作为结果标记的主体。

We saw the "shorthand" version in our earlier examples, where we provided a JSP path as the body of the result tag.

<result name = "success">
   /HelloWorld.jsp
</result>

我们还可以在 <result…​> 元素内使用 <param name = "location"> 标记指定 JSP 文件,如下所示:

We can also specify the JSP file using a <param name = "location"> tag within the <result…​> element as follows −

<result name = "success" type = "dispatcher">
   <param name = "location">
      /HelloWorld.jsp
   </param >
</result>

我们还可以提供一个 parse 参数,默认情况下为 true。parse 参数确定是否为 OGNL 表达式解析 location 参数。

We can also supply a parse parameter, which is true by default. The parse parameter determines whether or not the location parameter will be parsed for OGNL expressions.

The FreeMaker Result Type

在此示例中,我们将看到如何使用 FreeMaker 作为视图技术。Freemaker 是一个流行的模板引擎,用于使用预定义模板生成输出。现在,让我们创建一个名为 hello.fm 的 Freemaker 模板文件,内容如下:

In this example, we are going to see how we can use FreeMaker as the view technology. Freemaker is a popular templating engine that is used to generate output using predefined templates. Let us now create a Freemaker template file called hello.fm with the following contents −

Hello World ${name}

上述文件是一个模板,其中 name 是一个参数,它将从外部使用已定义的动作传递进来。您将把此文件保持在 CLASSPATH 中。

The above file is a template where name is a parameter which will be passed from outside using the defined action. You will keep this file in your CLASSPATH.

接下来,让我们修改 struts.xml 以指定结果,如下所示:

Next, let us modify the struts.xml to specify the result as follows −

<?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.tutorialspoint.struts2.HelloWorldAction"
         method = "execute">
         <result name = "success" type = "freemarker">
            <param name = "location">/hello.fm</param>
         </result>
      </action>

   </package>

</struts>

让我们保留我们在示例章节中创建的 HelloWorldAction.java、HelloWorldAction.jsp 和 index.jsp 文件。

Let us keep our HelloWorldAction.java, HelloWorldAction.jsp and index.jsp files as we have created them in examples chapter.

现在右键单击项目名称,然后单击 Export > WAR File 以创建 War 文件。

Now Right click on the project name and click Export > WAR File to create a War file.

然后将此 WAR 部署在 Tomcat 的 webapps 目录中。最后,启动 Tomcat 服务器并尝试访问 URL http://localhost:8080/HelloWorldStruts2/index.jsp 。这将生成以下屏幕。

Then deploy this WAR in the Tomcat’s webapps directory. Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/index.jsp. This will produce the following screen .

helloworldstruts4

输入一个值“Struts2”并提交页面。您应该看到下一页。

Enter a value "Struts2" and submit the page. You should see the next page.

helloworldstruts5

如您所见,这与 JSP 视图完全相同,只不过我们不必绑定到使用 JSP 作为视图技术。在此示例中,我们使用了 Freemaker。

As you can see, this is exactly same as the JSP view except that we are not tied to using JSP as the view technology. We have used Freemaker in this example.

The Redirect Result Type

redirect 结果类型调用标准 response.sendRedirect() 方法,导致浏览器对给定位置创建新的请求。

The redirect result type calls the standard response.sendRedirect() method, causing the browser to create a new request to the given location.

我们可以提供 <result…​> 元素 body 中或 <param name = "location"> 元素中的一个位置。Redirect 也支持 parse 参数。以下是一个使用 XML 配置的样例 −

We can provide the location either in the body of the <result…​> element or as a <param name = "location"> element. Redirect also supports the parse parameter. Here’s an example configured using XML −

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

因此,只需修改您的 struts.xml 文件,以按上述方式定义重定向类型,并创建一个新的文件 NewWorld.jpg,当 hello 操作返回成功时您将被重定向到该文件。您可以检查 Struts 2 Redirect Action 样例以获得更深入的了解。

So just modify your struts.xml file to define redirect type as mentioned above and create a new file NewWorld.jpg where you will be redirected whenever hello action will return success. You can check Struts 2 Redirect Action example for better understanding.