Hello Struts2
- 首先,在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>
- 在src目录下编写struts.xml文件(注意版本去核心包里面copy)
<?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>
<constant name="struts.action.extension" value="do"></constant>
<package name="MyPackage" extends="struts-default">
<action name="first">
<result>/first.jsp</result>
</action>
</package>
</struts>
- 编写index.html
<a href="first.action">first struts</a>
- 编写first.jsp
实现action
- 任何javabean都可以成为action,当action被执行时只要提供一个供框架调用的入口方法即可.
- Struts2中的action不是必须实现action接口,只要含有execute()方法并且返回控制串即可.
<action name="helloStruts_*" class="com.kexin.hellostruts.HelloStruts" method="{1}">
<result name="add">/first.jsp</result>
<result name="update">/first.jsp</result>
<result name="delete">/first.jsp</result>
</action>
package com.kexin.hellostruts;
public class HelloStruts {
public String add(){
System.out.println("add");
return "add";
}
public String update(){
System.out.println("update");
return "update";
}
public String delete(){
System.out.println("delete");
return "delete";
}
}