好几天了,总算有空和大家一起学习Struts2了。今天我们一起来入门Struts2,喜欢java的朋友们肯定不会对“Hello World”陌生,因为它是带我们步入陌生领域的“第一步”。所以我们一起动手来完成我们的“Hello World”。
2.1 开发环境
动手写之前我们先来统一一下开发环境,呵呵。因为Struts2发布最好开发环境是Servlet2.4(至于原因以后你就会明白),所以我们选择web容器为Tomcat6.x。而IDE嘛,现在流行的是Eclipse,所以我们也随流行的队伍使用Eclipse。现在Apache公布的Struts2版本是2.0.11.2,所以我们也使用这个。以下是这些的下载地址:
Struts2.0.11.2
http://sturts.apache.org
Tomcat6.0
http://tomcat.apache.org
Eclispe3.4
http://www.eclipse.org
注:因为我们是开发J2EE项目,所以建议大家直接下载Eclipse IDE for Java EE Developers。
2.2 添加包
首先新建一个“Dynamic Web Project”,我创建的是”Blog”项目。因为我们所有学习都是来不断完善这个Blog,所以项目直接取名为Blog。
我们将Struts2的5个关键包粘贴到/Blog/WebContent/WEB-INF/lib中,这5个关键包分别是:
a.commons-logging-1.0.4.jar
b.freemarker-2.3.8.jar
c.ognl-2.6.11.jar
d.struts2-core-2.0.11.2.jar
e.xwork-2.0.5.jar
2.3 流程图
图5-1 流程图
说明:用户提出请求(
http://localhost:8080/Blog),浏览器显示页面,用户输入用户名和密码,action利用validate()先进行对输入的排查,如果发现错误就返回index.jsp并显示错误信息;如果输入符合输入规定,则利用execute()进行对输入判断是否正确,如果正确转向welcome.jsp;如果不正确转向error.jsp。
2.4配制web.xml
接下来,我们开始我们的”Hello World”。因为做单纯Java具有输出作用的“Hello World”MS不能让我们了解Struts2,所以我们用Blog项目中的登录功能来完成所谓的“Hello World”,虽然功能简单,但是很能说明好多问题和知识点,麻雀虽小,五脏俱全,也就这个道理了。呵呵。
以下是web.xml代码和诠释:
-------------------------------------------------web.xml--------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Blog</display-name>
< ! -- 定义Struts2的FilterDispatcher的Filter -- >
<filter>
< !-- 定义核心Filter的名字 -- >
<filter-name>struts2</filter-name>
< !-- 定义核心Filter的实现类 -- >
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
< ! -- FilterDispatcher 用来初始化Struts2并且处理所有的Web请求 -- >
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
--------------------------------------------------------------------------------------------------------------------------------
2.5 三个网页代码
配制完后我们可以先对参与本次流程的三个页面进行编码,起代码情况如下:
----------------------------------------------------------index.jsp-----------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ben狐狸-Blog系统</title>
</head>
<body>
<table width="100" height="100">
<s:form action="login" method="post">
<tr hegiht="25">
<td>
<s:textfield name="userName" label="用户名"></s:textfield>
<s:password name="passWord" label="密码"></s:password></td>
</tr>
<tr hegiht="25">
<td>
<s:submit name="submit1" value="登录"></s:submit>
</td>
</tr>
<tr>
<td> </td>
</tr>
</s:form>
</table>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------error.jsp-----------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ben狐狸-Blog系统</title>
</head>
<body>
您输入的用户名或密码错误!
</body>
</html>
-----------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------welcome.jsp----------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ben狐狸-Blog系统</title>
</head>
<body>
Ben狐狸,欢迎您的到来!
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------
2.6 Action类
我们完成了前面的工作后,就要对我们书写的login.action,书写LoginAction.java.它是用来接受用户提交请求数据,并且对数据进行校验,然后在进行判断是否正确,最后确定返回转向的页面。代码如下:
-----------------------------------------------------LoginAction.java----------------------------------------------------
package com.benfox.blog.web;
import java.util.regex.Pattern;
import com.opensymphony.xwork2.ActionSupport;
/*
* @ auther - BenFox
*/
public class LoginAction extends ActionSupport{
private String userName;
private String passWord;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String execute()throws Exception{
String str="";
if(getUserName().equals("BenFox") && getPassWord().equals("123456"))
str = "success";
else str = "error";
return str;
}
public void validate(){
if(userName !=null && !Pattern.matches("\\w(4,25)", userName.trim()))
addFieldError("userName",
"请输入用户名必须是字母和数字,且长度必须是4到25之间!");
if(passWord !=null && !Pattern.matches("\\w(4,25)", passWord.trim()))
addFieldError("passWord",
"请输入密码必须是字母和数字,且长度必须是4到25之间!");
}
}
--------------------------------------------------------------------------------------------------------------------------------
2.7配制struts.xml
写完上面几步后,我们还无法运行该项目,因为它还缺少struts的配制文件(struts.xml),它是进行对action的处理,代码如下:
----------------------------------------------------struts.xml---------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="root" extends="struts-default">
<action name="login"
class="com.benfox.blog.web.LoginAction">
<result name="input">/index.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
--------------------------------------------------------------------------------------------------------------------------------
2.8 运行tomcat
还等什么,所以工作已经完成,接下来就是我们运行看效果的时候了,让我们一起运行tomcat,用浏览器查看我们的“成果”。
2.9 章节小结
本章我们讲述了一个struts2项目所应该具备的基本部署情况,从页面配制文件(web.xml)到struts配制文件(struts.xml),我们一一列举。如果你对这些还有问题,请不要着急,因为我们这次并不是透过这个例子,来分析struts2的所有。所以我们只要了解大概,再以后章节我和大家一起按照这样的部署模块,一部分一部分地详细学习。我已经领悟了struts2的结构,不知道你有没有通过这个例子领悟。下次再聊。