WebWork框架简单示例

开发 后端
本文介绍一个WebWork框架的小例子,从WebWork框架的下载开始,到src下的文件创建和action的定义,一步一步完成这个实例。

1 先下载WebWork框架开发包http://www.opensymphony.com/webwork/download.action 我用的是2.2.4的.

2 .在WebWork框架里建立一个WEB工程.你解压WebWork的开发包以后会发现有两个jar文件在***级目录里面,把他们拷贝进你的工程里面.然后你还会看见lib目录(webwork开发支持的所有jar文件),lib目录下面有个defult的目录,把这个目录里面的jar文件也都拷贝进你的工程,他们是开发webwork最基本的保障.

3 在src下创建两个文件.

(1)xwork.xml

  1. <!DOCTYPE xwork PUBLIC   
  2. "-//OpenSymphony Group//XWork 1.1.1//EN"   
  3. "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">   
  4. <xwork>   
  5. <include file="webwork-default.xml" />   
  6. <package name="webwork" extends="webwork-default">   
  7. <action name="hello" class="helloworld.HelloWorldAction">   
  8. <result name="yes" type="dispatcher">/yes.jsp  
  9. </result>   
  10. </action>   
  11. </package>   
  12. </xwork>   

(2)webwork.properties

webwork.i18n.encoding=GBK


### Load custom property files (does not override webwork.properties!)


# added the MockTag to the path of Tags that the TagDirective will search through


webwork.velocity.tag.path = com.opensymphony.webwork.views.velocity.ui, org.displaytag.tags


webwork.ui.templateDir = template


### Load custom default resource bundles


### XSLT Cache


webwork.xslt.nocache = true

3 web.xml

  1. xml version="1.0" encoding="UTF-8"?> 
  2. <web-app version="2.4"   
  3. xmlns="http://java.sun.com/xml/ns/j2ee"   
  4. xmlns:xsi="http://www.w3.org/2001/XML  
  5. Schema-instance"   
  6. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  7. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
  8. <welcome-file-list> 
  9. <welcome-file>index.jspwelcome-file> 
  10. welcome-file-list> 
  11. <servlet>   
  12. <servlet-name>webworkservlet-name>   
  13. <servlet-class>   
  14. com.opensymphony.webwork.dispatcher.ServletDispatcher   
  15. servlet-class>   
  16. servlet>   
  17. <servlet-mapping>   
  18. <servlet-name>webworkservlet-name>   
  19. <url-pattern>*.actionurl-pattern>   
  20. servlet-mapping>   
  21. web-app> 

 

4 在WebWork框架中创建HelloWorldAction类在helloworld包下,填写代码如下:

  1. package helloworld;   
  2. import com.opensymphony.xwork.Action;   
  3. public class HelloWorldAction implements Action {   
  4. private String userName;   
  5. public String getUserName() {   
  6. return userName;   
  7. }   
  8. public void setUserName(String userName) {   
  9. this.userName = userName;   
  10. }   
  11. public String execute() throws Exception {   
  12. // 处理乱码   
  13. //userName = new String  
  14. (userName.getBytes("iso-8859-1"),"GBK");   
  15. System.out.println(userName);   
  16. return "yes";   
  17. }   
  18. }  

5然后在创建下列两个jsp页面.

***个页面:index.jsp

  1. <%@ page language="java" import="java.util.*"   
  2. pageEncoding="GBK"%>   
  3. <%   
  4. String path = request.getContextPath();   
  5. String basePath = request.getScheme()+":  
  6. //"+request.getServerName()+":  
  7. "+request.getServerPort()+path+"/";   
  8. %>   
  9. <!DOCTYPE HTML PUBLIC   
  10. "-//W3C//DTD HTML 4.01 Transitional//EN">   
  11. <html>   
  12. <head>   
  13. <base href="<%=basePath%>">   
  14. <title>My JSP 'index.jsp' starting page  
  15. </title>   
  16. <meta http-equiv="pragma" c>   
  17. <meta http-equiv="cache-control" c>   
  18. <meta http-equiv="expires" c>       
  19. <meta http-equiv="keywords" c>   
  20. <meta http-equiv="description" c>   
  21. <!--   
  22. <link rel="stylesheet" type="text/css"   
  23. href="styles.css">   
  24. -->   
  25. </head>   
  26. <body>   
  27. <form action="hello.action" method="post">   
  28. <input type="text" name="userName"/>   
  29. <br>   
  30. <input type="submit"/>   
  31. </form>   
  32. </body>   
  33. </html>  

第二个页面:yes.jsp

  1. <%@ page language="java" import="java.util.*"   
  2. pageEncoding="GBK"%>   
  3. <%@ taglib prefix = "ww" uri = "/webwork" %>   
  4. <%   
  5. String path = request.getContextPath();   
  6. String basePath = request.getScheme()+":  
  7. //"+request.getServerName()+":  
  8. "+request.getServerPort()+path+"/";   
  9. %>   
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01   
  11. Transitional//EN">   
  12. <html>   
  13. <head>   
  14. <base href="<%=basePath%>">   
  15. <title>My JSP 'yes.jsp' starting page</title>   
  16. <meta http-equiv="pragma" c>   
  17. <meta http-equiv="cache-control" c>   
  18. <meta http-equiv="expires" c>       
  19. <meta http-equiv="keywords" c>   
  20. <meta http-equiv="description" c>   
  21. <!--   
  22. <link rel="stylesheet" type="text/css"   
  23. href="styles.css">   
  24. -->   
  25. </head>   
  26. <body>   
  27. yes. <br>   
  28. <ww:property value="%{userName}"/>   
  29. </body>   
  30. </html> 

【编辑推荐】

  1. WebWork注入Servlet方法详解
  2. WebWork中返回INPUT的原因
  3. WebWork如何实现文件上传配置过程
  4. 通过WebWork实现HelloWorld
  5. WebWork与Spring+Hibernate的整合
责任编辑:冰荷 来源: blog
相关推荐

2009-07-16 16:27:26

Struts WebW

2009-07-09 16:22:12

WebWork配置

2009-07-10 12:00:27

2009-07-16 15:14:27

WebWork用户登陆

2009-07-14 15:52:00

WebWork文件下载

2009-07-14 16:08:41

WebWork学习

2009-07-14 17:34:53

Webwork配置

2009-07-16 14:08:14

webwork配置

2009-07-20 13:29:13

xwork.xmlWebWork

2009-07-16 14:58:03

WebWork安装WebWork配置

2009-07-16 16:51:56

WebWork验证机制

2009-07-16 16:08:30

WebWork Act

2009-07-16 16:01:54

WebWork敏捷开发

2009-07-08 10:30:57

WebWork

2009-07-08 09:55:51

WebWork下载

2009-07-08 10:11:30

WebWork

2023-05-29 09:18:28

.NET网络通信

2009-07-14 17:53:11

WebWork安装WebWork配置

2009-07-09 15:55:18

WebWork配置文件

2009-07-16 17:42:47

WebWork配置
点赞
收藏

51CTO技术栈公众号