JavaScud | Forum | JIRA | Blog |
  Dashboard > WebWork2文档中文化计划 > ... > Tutorial > Basic configuration and your first action - Hello WebWorld
  WebWork2文档中文化计划 Log In View a printable version of the current page.  
  Basic configuration and your first action - Hello WebWorld
Added by scud, last edited by yangkaifeng on Aug 25, 2006  (view change)
Labels: 
(None)

你的第一个action - Hello WebWorld

一个action是一个(particular)特殊的URL请求时执行的一段代码. 再actions执行后, a result visually displays the outcome of whatever code was executed in the action. 一个result 通常是一个HTML页面, 但是也能是一个PDF文件,一个Excel电子表格, 或者是一个Java applet窗口. 在此文档中, 我们主要集中在HTML results中讨论, 因为大部分情况下我们是工作在Web环境下.

When you submit an html form using WebWork, the form is sent to a Java class that you write yourself, not to a JSP page. These classes are called WebWork actions. The html form  typically looks like: <form action="foo.action">.

在模型-视图-控制器方式中,WebWork的action是控制器的一部分, 留给JSP页面的是他们最擅长的工作: 创建视图. (如果你不知道什么是模型-视图-控制器, 不要担心.)

Suppose you want to create a simple "Hello, World" example in which a message is displayed whenever a user goes to a URL such as http://localhost/helloWorld.action. Because you've mapped WebWork's servlet to *.action, you need an action named helloWorld. To create the "Hello, World" example, you need to do three things:

  1. 创建一个action类: HelloWorld.
  2. 创建一个result: hello.jsp.
  3. 配置action和result.

代码

HelloWorld.java
package example.helloworld;
import com.opensymphony.xwork.Action;
import java.util.*;

public class HelloWorld implements Action {
private String message;

 public String execute() {
   message = "Hello, WebWorld!\n";
   message += "The time is:\n";
   message += DateFormat.getDateInstance().format(new Date());;

   return SUCCESS;
 }

 public String getMessage() {
   return message;
 }

}

 

粘贴此代码到文件webapp/hello.jsp中:

hello.jsp
<%@ taglib prefix="ww" uri="/webwork" %>
 <html>
   <head>
    <title>Hello Page</title>
   </head>
  <body>
    The message generated by my first action is:
    <ww:property value="message"/>
  </body>
 </html>

 

如下编辑xwork.xml文件, 添加helloWorld action和something called an interceptor to the default package. Read more: xwork.xml

xwork.xml
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
  <xwork>
   <include file="webwork-default.xml"/>

   <package name="default" extends="webwork-default">
     <!-- Include webwork defaults (from WebWork JAR). -->
     <default-interceptor-ref name="completeStack"/>

      <action name="helloWorld"
         class="example.helloworld.HelloWorld">
         <result name="success">hello.jsp</result>
      </action>
   </package>
  </xwork>

 
不要忘记

编译你的action到 webapp/WEB-INF/classes下, 必须重新启动你的web应用.


 

现在,试着: 在你的浏览器地址栏中输入url http://localhost/helloWorld.action ,查看发生了什么. 你应该看到页面输出"Hello, WebWorld!".

代码如何工作

上述四个文件在一起是像这样工作的.

  • 你的浏览器请求url http://localhost/helloWorld.action, 发送到你的web应用服务器.
  • 应用服务器接受此请求helloWorld.action. 查看webapp/WEB-INF/web.xml中的配置, 它发现所有的 *.action (这是缺省配置) requests are to be handed off to com.opensymphony.webwork.dispatcher.FilterDispatcher. Essentially, the request is handed to WebWork now.
  • WebWork 查看在 xwork.xml 中action名为 "helloWorld"的配置. There it finds that this corresponds to the class "HelloWorld," instantiates it, 调用它的方法execute().
  • execute() 返回 SUCCESS, WebWork 又一次在xwork.xml 中查看,装载返回值是SUCCESS 时的页面. 它找到页面"hello.jsp".
  • 页面hello.jsp 处理(此<ww:property value="message" />标签调用HelloWorld.java中的方法 getMessage()) 和发送到浏览器.

结束语,就WebWork, 所有的html表单元素发送到一个action. 此action返回一个常数result-name,比如:SUCCESS, ERROR或INPUT. Based on the xwork.xml, a given result-name may produce a page (as in this example), another action, or some other web resource (image, pdf). In this example, the form contained no data.

hellp.jsp中<%@ taglib prefix="ww" uri="webwork" %>
是否应改为<%@ taglib prefix="ww" uri="/webwork" %>

Posted by Anonymous at Jun 28, 2006 14:49 | Reply To This

同意楼上的朋友!

Posted by Anonymous at Aug 18, 2006 17:09 | Reply To This

hellp.jsp中<%@ taglib prefix="ww" uri="webwork" %>
是否应改为<%@ taglib prefix="ww" uri="/webwork" %>

这与你的web.xml中的taglib配置有关,eg:
<taglib>
<taglib-uri>webwork</taglib-uri>
<taglib-location>/WEB-INF/webwork.tld</taglib-location>
</taglib>

Posted by Anonymous at Sep 10, 2006 22:31 | Reply To This

恩,加不加/和设置有关,

Posted by Anonymous at Oct 30, 2006 17:50 | Reply To This

HelloWorld.java
这个文件里面,日期导的包有问题。
应该把java.util.*换成
import java.text.DateFormat;
import java.util.Date;

Posted by Anonymous at Nov 08, 2006 10:59 | Reply To This

在现在的版本中,直接用<%@ taglib prefix="ww" uri="/webwork" %>,而且在web.xml中不用加taglib了吧.

Posted by Anonymous at May 10, 2007 22:06 | Reply To This
Site running on a free Atlassian Confluence Open Source Project License granted to WebWork China. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.3 Build:#808 May 29, 2007) - Bug/feature request - Contact Administrators