Struts Step by Step
原文引用:http://www.blogjava.net/max/archive/2006/10/10/74353.html
1.下载
struts2-2.2.1-lib.zip
http://apache.freelamp.com//struts/library/struts2-2.2.1-lib.zip
struts2-2.2.1-apps.zip
http://apache.freelamp.com/struts/examples/struts2-2.2.1-apps.zip
eclipse-jee-helios-SR1-win32.zip
http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/helios/SR1/eclipse-jee-helios-SR1-win32.zip&url=http://download.actuatechina.com/eclipse/technology/epp/downloads/release/helios/SR1/eclipse-jee-helios-SR1-win32.zip&mirror_id=385
jdk-6u21-windows-i586.exe 下面的地址需要自己选择自己要的jdk
http://www.oracle.com/technetwork/java/archive-139210.html
apache-tomcat-7.0.2.exe
http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.2-beta/bin/apache-tomcat-7.0.2.exe
2.提取包
从struts2-2.2.1-lib.zip中struts-2.2.1\lib提取如下包
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.0.4.jar
freemarker-2.3.16.jar
ognl-3.0.jar
struts2-core-2.2.1.jar
xwork-core-2.2.1.jar
从struts2-2.2.1-apps.zip中struts2-blank-2.2.1.war\WEB-INF\lib提取如下包
javassist-3.7.ga.jar
把这些包拷贝到一个struts-lib文件夹中(这个文件夹自己创建一个,主要用途是临时保存这些包)
3.安装JDK和解压Eclipse文件
4.Eclipse配置Tomcat和JDK
a.启动Eclipse (我用的是Eclipse 3.6 for JEE)
Window-->Preferences-->Server-->Runtime Environments-->Add...-->Apache-->Apache Tomcat v7.0-->Next-->Browse...-->
指向Tomcat 7.0的安装路径-->确定-->Installed JREs...-->Add...-->Standard VM-->Next-->Directory...-->
执行jdk1.6.0_21的文件夹(我的在C:\Program Files\Java\jdk1.6.0_21)-->Finish-->选择jdk1.6.0_21-->OK-->
JRE: jdk1.6.0_21-->Finish-->OK
5.创建第1个例子
a.创建Web项目
点击菜单File-->New-->Other...-->Web-->Dynamic Web Project-->Next-->Project name: Struts2_HelloWorld-->
Target runtime: Apache Tomcat v7.0-->Next-->Next-->Content directory: WebRoot (把WebContent改成WebRoot的原因希望能在MyEclipse配合起来)-->
-->选择Generate web.xml deployment descriptor-->Finish-->
b.配置项目为UTF-8格式打开
右键点击Struts2_HelloWorld-->Properties-->Resource-->Text file encoding 选择Other: UTF-8-->OK
c.配置程序
在Struts2_HelloWorld\WebRoot\WEB-INF\lib下,把提取出来的放在struts-lib文件夹中的jar包拷贝到这里-->
修改Struts2_HelloWorld\WebRoot\WEB-INF下的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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts2_HelloWorld</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
</web-app>
保存-->
右键点击Struts2_HelloWorld\Java Resources: src-->New-->Other...-->General-->File-->Next-->
Enter or select the parent folder: Struts2_HelloWorld/src , File name: struts.xml-->
Finish-->struts.xml修改成如下:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
</struts>
-->保存-->
右键点击Struts2_HelloWorld\WebRoot-->New-->HTML File-->File name: index.html-->Finish-->修改index.html修改成如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<h3>Hello World!</h3>
</body>
</html>
-->保存-->
选择tool-->Window-->Show View-->Servers-->右键点击Servers视图的空白处-->New-->Server-->Tomcat v7.0 Server-->Next-->
选择Struts2_HelloWorld-->选择Add > 按钮-->Finish-->点中Servers视图中的Tomcat v7.0 Server at localhost [Stopped]-->右击-->
Start-->打开IE,地址栏中输入: http://localhost:8080/Struts2_HelloWorld/-->如果网页显示 Hello World!这表明Web已经配置-->
到这里已经完成了一个Struts框架的搭建.
e.第一个Struts 2.0应用程序--Hello World
新建类包(package)-->右键点击Struts2_HelloWorld\Java Resources: src-->New-->Package-->Name: tutorial-->Finish-->
右键点击Struts2_HelloWorld\Java Resources: src\tutorial-->New-->Class-->Name: HelloWorld-->Finish-->
修改HelloWorld.java为
package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 6826663312976947064L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() {
name = "Hello, " + name + "!";
return SUCCESS;
}
}
-->保存-->
在struts.xml中添加action映射(mapping),把struts.xml修改成如下:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="tutorial" extends="struts-default">
<action name="HelloWorld" class="tutorial.HelloWorld">
<result>HelloWorld.jsp</result>
</action>
</package>
</struts>
-->保存-->
右键点击Struts2_HelloWorld\WebRoot-->New-->JSP File-->File name: SayHello.jsp-->Finish-->修改SayHello.jsp修改成如下:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Say Hello</title>
</head>
<body>
<h3>Say "Hello" to: </h3>
<s:form action="HelloWorld">
Name: <s:textfield name="name" />
<s:submit />
</s:form>
</body>
</html>
-->保存-->
右键点击Struts2_HelloWorld\WebRoot-->New-->JSP File-->File name: HelloWorld.jsp-->Finish-->修改HelloWorld.jsp修改成如下:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Hello</title>
</head>
<body>
<h3><s:property value="name" /></h3>
</body>
</html>
-->保存-->
在Servers视图中,选中Tomcat v7.0 Server at localhost [Started, Synchronized]-->右击-->Publish-->
再点中Tomcat v7.0 Server at localhost [Started, Synchronized]右击-->Stop-->
再点中Tomcat v7.0 Server at localhost [Stopping, Synchronized]右击-->Start-->
打开IE,地址栏中输入: http://localhost:8080/Struts2_HelloWorld/SayHello.jsp-->在输入框中输入: 中国-->
点击 Submit-->-如果网页显示 Hello, 中国!这表明Struts已经配置成功.
6.单元测试
a.新建JUnit单元测试
右键点击Struts2_HelloWorld\Java Resources: src-->New-->Other...-->Source Folder-->Proejct name: Struts2_HelloWorld-->
Folder name: test-->Finish-->右键点击Struts2_HelloWorld\Java Resources\test-->New-->Other...-->Java-->Junit-->
Junit Test Case-->Next-->Package: tutorial -->Name: HelloWorldTest-->Class under test: tutorial.HelloWorld-->Next-->
选择HelloWorld 下的execute()-->Finish-->Perform the following action: Add JUnit 4 libraray to the build path-->OK-->
修改HelloWorldTest.java成:
package tutorial;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldTest {
@Test
public void testExecute() {
HelloWorld hello = new HelloWorld();
hello.setName("World");
String result = hello.execute();
assertTrue("Expected a success result!", ActionSupport.SUCCESS.equals(result));
final String msg = "Hello, World!";
assertTrue("Expected the default message!", msg.equals(hello.getName()));
}
}
-->保存-->右击HelloWorldTest.java-->Run as-->JUnit Test-->可以看见开发平台的右边有绿色的条,表明测试成功.
总结
上面的例子简单地演示了,Web 应用程序的基本操作,也即是,页面输入->Action处理->再输出到另外页面。
Struts 2.0的简单易用、方便测试相信也会给大家留下不错的印象吧。我相信,Struts 2.0作为一个全新的Web架构,
将会再次掀起Web开发的热潮。
注:
1.Tomcat报Javassist library is missing in classpath 的解决办法
下载struts2-2.2.1-apps.zip,在里面的struts2-blank-2.2.1.war\WEB-INF\lib下面找到javassist-3.7.ga.jar放到lib里面。
就可以了。
2.HTTP Status 404 - There is no Action mapped for namespace / and action name HelloWorld.
No configuration found for the specified action: 'HelloWorld' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
的解决办法
我把struts.xml写成了sturts.xml造成的。然后点中Tomcat v7.0 Server at localhost [Started, Synchronized]右击-->Stop-->
点中Tomcat v7.0 Server at localhost [Stopped, Synchronized]右击-->Publish-->
再点中Tomcat v7.0 Server at localhost [Stopped, Synchronized]右击-->Start-->
打开IE,地址栏中输入: http://localhost:8080/Struts2_HelloWorld/SayHello.jsp 在submit就可以了。
3.如何 把Eclipse的Web项目发布到实际的Tomcat下?
步骤如下:
点中项目名,我这里点中Struts2_HelloWorld-->右击-->Export-->WAR file-->Browse...-->我这里选择到C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps下面-->保存(S)-->
选择Overwrite existing file复选框-->Finish-->选择 开始-->所有程序(P)-->Apache Tomcat 7.0-->Monitor Tomcat-->
屏幕右下方出现一个红点 的表状图标-->右击-->选择Start Service-->然后打开IE,地址栏中输入: http://localhost:8080/Struts2_HelloWorld/SayHello.jsp -->如果显示出 Say "Hello" to:字样,表明Web已经启动成功。
来源于网络,回归于网络。
我的邮箱:happy.every.day@126.com QQ:48399956
快乐!
2010年9月14日