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

<!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="webwork" extends="webwork-default">   
<action name="hello" class="helloworld.HelloWorldAction">   
<result name="yes" type="dispatcher">/yes.jsp  
</result>   
</action>   
</package>   
</xwork>   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

(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

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

 

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

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

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

***个页面:index.jsp

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

第二个页面:yes.jsp

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

【编辑推荐】

  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 16:08:41

WebWork学习

2009-07-14 15:52:00

WebWork文件下载

2009-07-14 17:34:53

Webwork配置

2009-07-16 14:08:14

webwork配置

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-08 10:30:57

WebWork

2009-07-08 10:11:30

WebWork

2009-07-08 09:55:51

WebWork下载

2009-07-16 16:01:54

WebWork敏捷开发

2009-07-20 13:29:13

xwork.xmlWebWork

2023-05-29 09:18:28

.NET网络通信

2009-06-01 14:32:10

jpa技术Java框架

2009-07-14 12:47:07

WebWork工作方式

2009-07-14 14:41:33

Webwork与Spr
点赞
收藏

51CTO技术栈公众号