Struts 2 简明教程
Struts 2 - Environment Setup
我们的第一个任务是运行一个极简的 Struts 2 应用程序。本章将指导你如何准备开发环境,以开始使用 Struts 2。
Our first task is to get a minimal Struts 2 application running. This chapter will guide you on how to prepare a development environment to start your work with Struts 2.
我假设你的计算机上已经安装了 JDK (5+)、Tomcat 和 Eclipse。如果你没有安装这些组件,那么请按照快速通道中的给定步骤操作 −
I assume that you already have JDK (5+), Tomcat and Eclipse installed on your machine. If you do not have these components installed, then follow the given steps on fast track −
Step 1 - Setup Java Development Kit (JDK)
你可以从 Oracle 的 Java 网站下载 SDK 的最新版本 − Java SE Downloads 。你会在下载的文件中找到有关安装 JDK 的说明,请按照给出的说明安装并配置该设置。最后,设置 PATH 和 JAVA_HOME 环境变量以引用包含 java 和 javac 的目录,通常分别为 java_install_dir/bin 和 java_install_dir。
You can download the latest version of SDK from Oracle’s Java site − Java SE Downloads. You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally, set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively.
如果你运行的是 Windows,并且在 C:\jdk1.5.0_20 中安装了 SDK,则应在 C:\autoexec.bat 文件中输入以下行。
If you are running Windows and installed the SDK in C:\jdk1.5.0_20, you should be inputting the following line in your C:\autoexec.bat file.
set PATH = C:\jdk1.5.0_20\bin;%PATH%
set JAVA_HOME = C:\jdk1.5.0_20
或者,在 Windows NT/2000/XP 上 −
Alternatively, on Windows NT/2000/XP −
-
You can right-click on My Computer, Select Properties, then Advanced, then Environment Variables. Then, you would update the PATH value and press the OK button.
-
On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk1.5.0_20 and you use the C shell, you would put the following into your .cshrc file.
在 Unix(Solaris、Linux 等)上,如果 SDK 安装在 /usr/local/jdk1.5.0_20 中,而您使用 C shell,您应将以下内容放入 .cshrc 文件中。
On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk1.5.0_20 and you use the C shell, you would put the following into your .cshrc file.
setenv PATH /usr/local/jdk1.5.0_20/bin:$PATH
setenv JAVA_HOME /usr/local/jdk1.5.0_20
或者,如果您使用 Borland JBuilder、Eclipse、IntelliJ IDEA 或 Sun ONE Studio 等集成开发环境 (IDE),请编译并运行一个简单程序以确认 IDE 知道您在哪里安装 Java,否则按照 IDE 提供文档进行适当设置。
Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, compile and run a simple program to confirm that the IDE knows where you installed Java, otherwise do proper setup as per the given document of IDE.
Step 2 - Setup Apache Tomcat
您可以从 https://tomcat.apache.org/ 下载最新版本的 Tomcat。下载安装后,将二进制发行版解压缩到一个方便的位置。
You can download the latest version of Tomcat from https://tomcat.apache.org/. Once you downloaded the installation, unpack the binary distribution into a convenient location.
例如,在 Windows 中为 C:\apache-tomcat-6.0.33,在 Linux/Unix 中为 /usr/local/apachetomcat-6.0.33,并创建指向这些位置的 CATALINA_HOME 环境变量。
For example in C:\apache-tomcat-6.0.33 on windows, or /usr/local/apachetomcat-6.0.33 on Linux/Unix and create CATALINA_HOME environment variable pointing to these locations.
您可以在 Windows 计算机上执行以下命令启动 Tomcat,或者您只需双击 startup.bat
You can start Tomcat by executing the following commands on windows machine, or you can simply double click on startup.bat
%CATALINA_HOME%\bin\startup.bat
or
C:\apache-tomcat-6.0.33\bin\startup.bat
可以通过在 Unix(Solaris、Linux 等)计算机上执行以下命令启动 Tomcat:
Tomcat can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine −
$CATALINA_HOME/bin/startup.sh
or
/usr/local/apache-tomcat-6.0.33/bin/startup.sh
成功启动后,通过访问 http://localhost:8080/ 即可使用包含在 Tomcat 中的默认 Web 应用程序。如果一切都很好,则应显示以下结果 -
After a successful startup, the default web applications included with Tomcat will be available by visiting http://localhost:8080/. If everything is fine, then it should display the following result −
有关配置和运行 Tomcat 的更多信息可在本文档中找到,也可以在 Tomcat 网站上找到: https://tomcat.apache.org/
Further information about configuring and running Tomcat can be found in the documentation included here, as well as on the Tomcat website: https://tomcat.apache.org/
可以通过在 Windows 计算机上执行以下命令停止 Tomcat:
Tomcat can be stopped by executing the following commands on windows machine −
%CATALINA_HOME%\bin\shutdown
or
C:\apache-tomcat-5.5.29\bin\shutdown
可以通过在 Unix(Solaris、Linux 等)计算机上执行以下命令停止 Tomcat:
Tomcat can be stopped by executing the following commands on Unix (Solaris, Linux, etc.) machine −
$CATALINA_HOME/bin/shutdown.sh
or
/usr/local/apache-tomcat-5.5.29/bin/shutdown.sh
Step 3 - Setup Eclipse (IDE)
本教程中的所有示例都使用 Eclipse IDE 编写。我建议您在计算机中安装最新版本的 Eclipse。
All the examples in this tutorial are written using Eclipse IDE. I suggest that, you have the latest version of Eclipse installed in your machine.
若要安装 Eclipse,请从 https://www.eclipse.org/downloads/ 下载最新的 Eclipse 二进制文件。下载安装后,将二进制发行版解压缩到一个方便的位置。
To install Eclipse Download the latest Eclipse binaries from https://www.eclipse.org/downloads/. Once you download the installation, unpack the binary distribution into a convenient location.
例如,在 Windows 中为 C:\eclipse,在 Linux/Unix 中为 /usr/local/eclipse,最后适当地设置 PATH 变量。可以在 Windows 计算机上执行以下命令启动 Eclipse,或者您只需双击 eclipse.exe
For example in C:\eclipse on windows, or /usr/local/eclipse on Linux/Unix and finally set PATH variable appropriately. Eclipse can be started by executing the following commands on windows machine, or you can simply double click on eclipse.exe
%C:\eclipse\eclipse.exe
可以通过在 Unix(Solaris、Linux 等)机器上执行以下命令启动 Eclipse −
Eclipse can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine −
$/usr/local/eclipse/eclipse
成功启动后,如果一切都好,则应显示以下结果 -
After a successful startup, if everything is fine, it should display the following result −
Step 4 - Setup Struts2 Libraries
现在,如果一切正常,您可以继续设置 Struts2 框架。以下是下载和在计算机上安装 Struts2 的简单步骤。
Now if everything is fine, then you can proceed to setup your Struts2 framemwork. Following are the simple steps to download and install Struts2 on your machine.
-
Make a choice whether you want to install Struts2 on Windows, or Unix and then proceed to the next step to download .zip file for windows and .tz file for Unix.
-
Download the latest version of Struts2 binaries from https://struts.apache.org/download.cgi.
-
At the time of writing this tutorial, I downloaded struts-2.0.14-all.zip and when you unzip the downloaded file it will give you directory structure inside C:\struts-2.2.3 as follows.
第二步是在任何位置提取 zip 文件,我在 Windows 7 计算机上的 c:\ 文件夹中下载并提取了 struts-2.2.3-all.zip ,以便将所有 jar 文件都放入 C:\struts-2.2.3\lib 中。确保正确设置 CLASSPATH 变量,否则在运行应用程序时将遇到问题。
Second step is to extract the zip file in any location, I downloaded & extracted struts-2.2.3-all.zip in c:\ folder on my Windows 7 machine so that I have all the jar files into C:\struts-2.2.3\lib. Make sure you set your CLASSPATH variable properly otherwise you will face problem while running your application.