MVC循序渐进(环境篇) eclipse+tomcat(一)
为了以后比较分析,我把所有的实现代码放到一个web应用程序中,所以看这篇文章前请看以上系列文章。
一、安装webwork
http://opensymphony.com/webwork/ 下载最新版本的webwork 解压
我们用到的jar包有
lib/core 下所有jar包 和 webwork-2.1.7.jar
拷贝到WEB-INF/lib 文件夹下。
二、配置
webapp\WEB-INF\web.xml
webapp\WEB-INF\classes\ xwork.xml(webapp\WEB-INF\src\ xwork.xml 建立 eclipse编译自动放到classes目录下)
webapp\WEB-INF\web.xml
****************************************************************
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>
<display-name>Examples</display-name>
<description>Examples. </description>
<servlet>
<servlet-name>webwork</servlet-name>
<servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>model2.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>webwork</servlet-name>
<url-pattern>*.webwork</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/model2/LoginServlet</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>webwork</taglib-uri>
<taglib-location>/WEB-INF/lib/webwork-2.1.7.jar</taglib-location>
</taglib>
</web-app>
************************************************************************
webapp\WEB-INF\classes\ xwork.xml(webapp\WEB-INF\src\ xwork.xml 建立 eclipse编译自动放到classes目录下)
**********************************************************************
<!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">
<default-interceptor-ref name="defaultStack" />
<action name="login" class="webwork.LoginAction">
<result name="error" type="dispatcher">login.jsp</result>
<result name="success" type="dispatcher">succeed.jsp</result>
</action>
</package>
</xwork>
*************************************************************************
其他webwork.properties,validators.xml等配置在以后介绍
三、所有程序
webapp\webwork\ login.jsp
webapp\webwork\ succeed.jsp
webapp\WEB-INF\src\webwork\ LoginAction.java
webapp\webwork\ login.jsp
*************************************************************************
<%@ taglib uri="webwork" prefix="ww" %>
<%@ page language="java" pageEncoding="GB2312" %>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Lomboz JSP</title>
</head>
<body bgcolor="#FFFFFF">
<form action="login.webwork" method="post">
<div>
<span style="width:70;">用户名:</span>
<span><input type="text" name="username"></span>
</div>
<div>
<span style="width:70;">密 码:</span>
<span><input type="password" name="password"></span>
</div>
<div>
<span style="width:70;"></span>
<span><input type="submit"></span>
</div>
<div style="color:red;">
<ww:property value="msg" />
</div>
</form>
</body>
</html>
*************************************************************************
webapp\webwork\ succeed.jsp
*************************************************************************
<%@ page language="java" pageEncoding="GB2312" %>
<%@ taglib uri="webwork" prefix="ww" %>
<%@ page import="com.opensymphony.xwork.util.OgnlValueStack" %>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Lomboz JSP</title>
</head>
<body bgcolor="#FFFFFF">
<ww:property value="#session['username']"/>
<ww:property value="msg" />
<br>
////////////////////////////////////////
<br>
<%
Object username=session.getAttribute("username");
if(username!=null)
out.println(username);
else
response.sendRedirect("login.jsp");
OgnlValueStack stack = (OgnlValueStack)request.getAttribute("webwork.valueStack");
out.println(stack.findValue("msg"));
%>
</body>
</html>
*************************************************************************
webapp\WEB-INF\src\webwork\ LoginAction.java
*************************************************************************
package webwork;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;
public class LoginAction extends ActionSupport {
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
private String msg;
public String getMsg() {
return this.msg;
}
public String execute() throws Exception {
boolean isSucceed = false;
if (username != null) {
System.out.println("登陆处理开始.........");
username = username.trim();
password = password.trim();
if (username.equals("") && username.equals("")) {
isSucceed = false;
msg = "用户名或密码为空";
} else {
isSucceed = true;
}
if (isSucceed) {
if (username.equals("zkjbeyond")
&& password.equals("zkjbeyond")) {
isSucceed = true;
//放到session中
ActionContext.getContext().getSession().put("username", username);
msg = "登陆成功";
} else {
isSucceed = false;
msg = "用户名密码不正确";
}
}
} else {
isSucceed = false;
msg = "请输入用户名密码";
}
System.out.println(msg + isSucceed);
System.out.println("登陆处理结束.........");
if (isSucceed)
return Action.SUCCESS;
else
return Action.ERROR;
}
}
*************************************************************************
参考:http://www.opensymphony.com/webwork/wikidocs/Documentation.html