以前都沒有用過WebWork這個框架,只是聽說過.沒想到現在要用,所以就自學了一下.做了個小例子給大家分享下中間遇到的苦難和經驗.
准備工作:首先要去下載WebWork框架的開發包.我用的2.2.6版本的.
開發過程如下:
1.建立一個WEB工程.你解壓WebWork的開發包以后會發現有兩個jar文件在第一級目錄里面,把他們拷貝進你的工程里面.然后你還會看見lib目錄(webwork開發支持的所有jar文件),lib目錄下面有個defult的目錄,把這個目錄里面的jar文件也都拷貝進你的工程,他們是開發webwork最基本的保障.
2.在你的web.xml加入下面的代碼:
<servlet>
<servlet-name>webwork</servlet-name>
<servlet-class>
com.opensymphony.webwork.dispatcher.ServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>webwork</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
3.創建你的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"
<result name="yes" type="dispatcher">/yes.jsp</result>
</action>
</package>
</xwork>
4.創建webwork.properties文件(和xwork.xml放在一個文件夾下),寫入下列代碼
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
5.創建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";
}
}
6.然后在創建下列兩個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>
第二個頁面: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>
到這里就OK了,運行你的項目吧..
在index.jsp頁面中輸入文字,點提交在yes.jsp頁面給與顯示...
如果你就按照上面這樣做,你會發現會出錯誤.錯誤原因有可能是因為你的tomcat里面解析xml的jar包版本過低.
解決辦法就是下載xalan.jar文件.
然后把里面的jar文件都拷貝到tomcat安裝目錄下面的common目錄下的endorsed目錄下...就OK了.