搭建环境(struts-2.3.3版本):
第一步:创建一个web project
第二步:添加相应的jar包
第三步:在web.xml中配置核心控制器
<filter>
<filter-name>strtus2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>strtus2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
第四步:配置struts.xml
可以新建一个struts.xml,头文件在struts-core包中struts-default.xml里
第五步:创建一个action类,所有方法必须是public修饰,String类型返回值,没有参数,默认执行execute方法
package com.gcx.action;
public class HelloAction {
public String execute(){
System.out.println("hello strtus");
return "success";
}
}
第六步:配置action
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- extends 必须写,直接间接继承strtus-default name自定义 -->
<package name="hello" extends="struts-default">
<!-- name 是请求名称,class对应action 包名加类名-->
<action name="hello" class="com.gcx.action.HelloAction">
<!-- result结果集 name中的值和actio方法返回的值一样 -->
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
第七步:启动tomcat,浏览器访问http://localhost:8080/01struts_hello/hello