1.首先创建index.jsp,主体一行代
<a href="${pageContext.request.contextPath}/hello">第一次使用strut2</a>
2.创建HelloAction
创建方法say,返回String
public String say(){
return "good";
}
3.配置web.xml,屏蔽所有请求
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4.配置struts.文件xml
<package name="default" namespace="/" extends="struts-default">
<action name="hello" class="cn.itcast.action.HelloAction"
method="say">
<result name="good">/hello.jsp</result>
</action>
</package>
5.创建hello.jsp
<h1>hello struts</h1>
工作流程如图
注意事项:
maven默认不编译src下面的 .xml文件,需要在pom.xml里面加上
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>