任何结果都是由简单的实践得出的,当然我下面的介绍也是模仿那些大牛们,下面WebWork的介绍也是从古老而经典的HelloWorld开始。
在今天,各种各样的框架让我们眼花缭乱,但我始终坚信一点:“存在就是合理的”(黑格尔之哲学),各种框架利弊优劣等不是本篇之目的。
首先表述一下我做这个HelloWorld的机器环境
JDK5
Eclipse 3.3.1
mysql 5.0.45-community-nt
jar包:
commons-fileupload.jar - Apache Commons Fil Upload version 1.0
commons-logging.jar - Apache Commons Logging version ???
cos-multipart.jar - Cos Multipart File Upload version ???
mail.jar - Mail API version ???
ognl.jar - OGNL version 2.6.5
oscore.jar - OSCore version 2.2.4
pell-multipart.jar - Pell Multipart File Upload version ???
velocity-dep.jar - Velocity (plus dependencies) version 1.3.1
xwork.jar - XWork version 1.0.5
打开Eclipse建一个Hello的web工程
先写一个User.java 即MVC中的M:
[color=darkred]
package com.test;
/**
*
* @author yiqiao
*/
public class User {
private String userName;
private String userPwd;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
}
在看一下MVC中的C:
[color=darkred]
package com.test;
import com.opensymphony.xwork.Action;
/**
*
* @author yiqiao
*/
public class LoginAction implements Action {
private User user = new User();
public String execute() throws Exception {
if (user.getUserName().equals("yiqiao"))
return this.SUCCESS;
else
return this.ERROR;
}
public User getUser() {
return user;
}
}
页面3个:
index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>test</title>
</head>
<body>
<DIV align = "center">
LOGIN
</DIV>
<FORM action = "login.action" method = "post">
<DIV align = "center">
<TABLE border = "1" cellpadding = "0" cellspacing = "0">
<TR>
<TD>
username:
</TD>
<TD>
<INPUT type = "text" size = "10" name = "user.userName">
</TD>
</TR>
<TR>
<TD>
password:
</TD>
<TD>
<INPUT type = "text" size = "10" name = "user.userPwd">
</TD>
</TR>
<TR>
<TD colspan = "2" align = "center">
<INPUT type = "submit" value = "submit" name = "submit">
<INPUT type = "reset" value = "reset" name = "reset">
</TD>
</TR>
</TABLE>
</DIV>
</FORM>
</body>
</html>
2:ok.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix = "ww" uri = "webwork" %>
<html >
<head >
<title > OK </title>
</head>
<body>
userName= <ww:property value = "user.userName" />
<br>
userPwd= <ww:property value = "user.userPwd" />
<br>
</body>
</html>
3:error.jsp
<html >
<head>
<title> ERROR </title>
</head>
<body>
Error !!!
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>WebWork</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
xwork.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!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">
<action name="login" class="com.test.LoginAction">
<!-- 这里的意思是登录成功后跳转到 /ok.jsp 页面 -->
<result name="success" type="dispatcher">
<param name="location">/ok.jsp</param>
</result>
<!-- 登录失败后跳转到 / error .jsp 页面 -->
<result name="error" type="dispatcher">
<param name="location">/error.jsp</param>
</result>
<!-- 此段代码为作用是将 request 请求的参数传递到 action 中 -->
<interceptor-ref name="params" />
</action>
</package>
</xwork>