1.准备类库。不同的开发需求所用的类库是不一样的,但至少需要如下的5个类库。
struts2-core-2.0.11.jar xwork-2.0.4.jar ognl-2.6.jar freemaker-2.3.8.jar commons-logging-1.0.4.jar
2.在web.xml文件中配置FilterDispatcher。这一步是固定的,可以编写好一个web.xml文件保存起来。以后都用这个文件。
3.开发action。针对每个功能点,编写一个action类。
4.编写相关的结果页面。针对action返回的结果代码,编写相应的结果页面。
5.在Web应用程序的WEB-INF/classes目录下创建struts.xml,对action进行配置,将action与结果页面关联在一起。
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts info</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>