Struts2的第一个例子
1、 创建一个web project
2、 导入jar包
3、 编写web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<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>
</web-app>
4、 写一个action
5、classpath下建立struts.xml文件,把action配置进去
6、部署,打开服务器,访问action根据result标签找到要转向的页面
Action的写法与配置
继承ActionSupport类,并利用通配符配置struts.xml文件中的action
例如:
public class HelloWorldAction extends ActionSupport {
public String test(){
return "index";
}
}
在struts.xml中为
<action name="helloWorldAction_*" method="{1}" class="com.xgss.action.HelloWorldAction">
<result name="index">index.jsp</result>
</action>
部署访问http://localhost:8080/myStruts2/helloworldAction_test.action就可以访问到index.jsp