本周三利用空余的时间把医院HIV月报构建在了Struts2框架上,真的很简单,代码量比 Struts1.x 少了一半。
WebWork 创始人 Reckard Oberg 在构建 WebWork 原始版本的时候说:“框架的强大之处不在于它能让你做什么,而是你没必要做什么。”
Struts2 抛弃了原先 Struts 的原始框架,构建在 xwork 之上。Struts2继承了WebWork2 清爽的架构及代码编写风格。
下面就开始 WebWork2 之旅吧,感受一下代码编写是多么的简单和清晰。
从http://www.opensymphony.com/webwork下载最新的完整的开源包并解压。
新建 Web Project。
把 xwork.jar common-logging.jar oscore.jar velocity-dep.jar ognl.jar webwork-2.2.4.jar 复制到WEB-INF/lib目录下,修改web.xml文件内容如下:
xml 代码
- <!DOCTYPE?web-app?PUBLIC?"-//Sun?Microsystems,?Inc.//DTD?Web?Application?2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd">??
- <web-app>??
- ????<filter>??
- ????????<filter-name>webwork</filter-name>??
- ????????<filter-class>??
- ????????????com.opensymphony.webwork.dispatcher.FilterDispatcher??
- ????????</filter-class>??
- ????</filter>??
- ????<filter-mapping>??
- ????????<filter-name>webwork</filter-name>??
- ????????<url-pattern>/*</url-pattern>??
- ????</filter-mapping>??
- ????<taglib>??
- ????????<taglib-uri>webwork</taglib-uri>??
- ????????<taglib-location>??
- ????????????/WEB-INF/lib/webwork-2.2.4.jar??
- ????????</taglib-location>??
- ????</taglib>??
- </web-app>??
新建action: HelloWorld.java 实现 Action 接口,并且必须实现 execute() 方法
java 代码
- package?org.vv.action;??
- import?com.opensymphony.xwork.Action;??
- public?class?HelloWorld?implements?Action?{??
- ????private?String?message;??
- ????private?String?name;??
- ????public?String?getName()?{??
- ????????return?name;??
- ????}??
- ????public?void?setName(String?name)?{??
- ????????this.name?=?name;??
- ????}??
- ????public?String?execute()?throws?Exception?{??
- ????????if?(name?==?null?||?"".equals(name)?||?"World".equals(name))?{??
- ????????????message?=?"Blank?names?or?names?of?'World'?are?not?allowed!";??
- ????????????return?INPUT;??
- ????????}??
- ????????message?=?"Hello,?"?+?name?+?"!\n";??
- ????????message?+=?"The?time?is:\n";??
- ????????message?+=?System.currentTimeMillis();??
- ????????return?SUCCESS;??
- ????}??
- ????public?String?getMessage()?{??
- ????????return?message;??
- ????}??
- }??
在classes目录下新建xwork.xml文件,最终内容如下:
xml 代码
- <!DOCTYPE?xwork?PUBLIC?"-//OpenSymphony?Group//XWork?1.1.1//EN"
- "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">??
- <xwork>??
- ????<include?file="webwork-default.xml"?/>??
- ????<package?name="default"?extends="webwork-default">??
- ????????<default-interceptor-ref?name="completeStack"?/>??
- ????????<action?name="helloWorld"?class="org.vv.action.HelloWorld">??
- ????????????<result?name="success">hello.jsp</result>??
- ????????????<result?name="input">name.jsp</result>??
- ????????</action>??
- ????</package>??
- </xwork>??
新建 name.jsp 页面文件,内容如下:
jsp 代码
- <%@?page?language="java"?import="java.util.*"?pageEncoding="GB18030"%>??
- <%@?taglib?prefix="ww"?uri="webwork"%>??
- <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">??
- <html>??
- ????<head>??
- ????????<title></title>??
- ????</head>??
- ????<body>??
- ????????<ww:if?test="message!=null">??
- ????????????<font?color="red">?<ww:property?value="message"?/>?</font>??
- ????????</ww:if>??
- ????????Please?enter?your?name:??
- ????????<form?action="helloWorld.action">??
- ????????????<input?type="textfield"?name="name"??
- ????????????????value="<ww:property?value="name"/>"?/>??
- ????????????<input?type="submit">??
- ????????</form>??
- ????</body>??
- </html>??
新建 hello.jsp 页面文件,内容如下:
jsp 代码
- <%@?page?language="java"?import="java.util.*"?pageEncoding="GB18030"%>??
- <%@?taglib?prefix="ww"?uri="webwork"%>??
- <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">??
- <html>??
- ????<head>??
- ????????<title></title>??
- ????</head>??
- ????<body>??
- ????????The?message?generated?by?my?first?action?is:??
- ????????<ww:property?value="message"?/>??
- ????</body>??
- </html>??
注意:<ww:property value="message" /> 中的 message 得到的是 HelloWorld 中getMessage()方法的返回值,而非 message 属性。
ok,5个短小的文件,构成了一个轻量级的应用。在实际应用中,一般是继承ActionSupport 类,通过addFieldError("name",String)来达到验证效果。
上面的代码只需修改两个地方。
HelloWorld.java类:
java 代码
- public?class?HelloWorld?extends?ActionSupport?{??
- ????……??
- ????public?String?execute(){??
- ???????if(……)??
- ??????????addFieldError("name","Blank?names?or?names?of?'World'?are?not?allowed!")??
- ???????……??
- ????}??
- ????……??
- }??
name.jsp更改:
jsp 代码
- <ww:if?test="message!=null">??
- ????<font?color="red">??
- ????????<ww:property?value="fieldErrors['name']"?/>??
- ????</font>??
- </ww:if>??
----------------------------------------
和 Struts2 一样WebWork2 的 UI 标签库极大地简化了JSP页面,name.jsp最终更改为:
jsp 代码
- <ww:form?action="helloWorld">??
- ????<ww:textfield?label="Please?enter?your?name:"?name="name"?/>??
- ????<input?type="submit"?/>??
- </ww:form>??
ww:textfield已经包含了错误信息了。
1 楼
bat_xie
2007-12-18
为什么我运行你的程序直接运行helloWorld.action
抛出如下的错误
007-12-18 14:56:06 com.opensymphony.webwork.dispatcher.DispatcherUtils serviceAction
严重: Could not execute action
java.lang.NullPointerException
at com.opensymphony.xwork.DefaultActionProxy.<init>(DefaultActionProxy.java:57)
at com.opensymphony.xwork.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:46)
at com.opensymphony.webwork.dispatcher.DispatcherUtils.serviceAction(DispatcherUtils.java:216)
抛出如下的错误
007-12-18 14:56:06 com.opensymphony.webwork.dispatcher.DispatcherUtils serviceAction
严重: Could not execute action
java.lang.NullPointerException
at com.opensymphony.xwork.DefaultActionProxy.<init>(DefaultActionProxy.java:57)
at com.opensymphony.xwork.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:46)
at com.opensymphony.webwork.dispatcher.DispatcherUtils.serviceAction(DispatcherUtils.java:216)