第一步:在网上下载Struts2的jar包,我这里使用的是struts-2.5.2;
第二步:使用MyEclispe新建一个Web Project,命名为strutsUtil;
第三步:将解压出来的struts-2.5.2子目录下的lib文件夹中如下所示的jar包复制进strutsUtil项目的WEB-INF路径下的lib目录中:
这里所列出是至少要导入的jar包,具体使用的jar包以开发者所用的struts2的功能为主!
第四步:在项目src目录底下编写Struts2的配置文件,struts.xml,该文件可以在使用如下方法得到:
1,将struts-2.5.2子目录下的apps文件夹中的任意一个压缩包解压出来,这些压缩包是Struts2提供给开发者参考的例子项目;
2,进入解压出来的项目底下的WEB-INF的class目录,就可以找到Struts2的配置文件,struts.xml;
3,如下是配置文件的相关模板:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="lea" namespace="/leator" extends="struts-default">
<action name="hello" class="cn.lea.com.HelloStruts2" method="execute">
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
</struts>
第五步:在项目的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"
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>StrutsUtil01</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</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>