先记下基本步骤吧,中间遇到很多莫名其妙的问题:
步骤:
1.创建web项目
2.导入struts2的jar包,我新建了一个用户库,以后可以直接导入用户库
3.在web.xml中配置FilterDispatcher
struts2框架是基于mvc模式的,他提供了核心控制类,对所有请求统一处理,这个控制器是一个名为FilterDispatcher 的Servlet过滤器充当的
2.1以前的版本配置模板如下:
<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
4.编写Action类,实现Action接口,返回5中静态数据类型结果
5.在struts.xml文件中配置action:一个action创建完成后,还要为其配置一个URL,这样,FilterDispatcher才知道URL对应哪个action,当截止到URL时,FilterDispatcher就调用对应的action对请求进行处理
模板如下:
<?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="HelloWorld" extends="struts-default">
<action name="HelloWorld" class="org.sunxin.struts2.ch02.action.HelloWorldAction">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
6.发布测试