1.FilterDispatcher: 需要的 struts2-codebehind-plugin-2.3.4.jar
StrutsPrepareAndExecuteFilter:需要
struts2-convention-plugin-2.3.4.jar
asm-3.3.jar
asm-commons-3.3.jar
附件中有完整案例
2.struts.xml
<?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.convention.default.parent.package" value="default-package" />
<package name="default-package" extends="convention-default"></package>
<!-- 修改请求的后缀名为.do 修改后每个请求必须要有后缀了-->
<constant name="struts.action.extension" value="do,action"/>
<!-- 如果 注解的action配置改变时候不需要重新启动tomcate -->
<constant name="struts.devMode" value="true"/>
<constant name="struts.convention.classes.reload" value="true" />
<!-- 配置log4j 的 日志级别 -->
<constant name="log4j.logger.org.apache.struts2.convention" value="DEBUG" />
</struts>
3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!--发现 比FilterDispatcher不需要配置actionPackages-->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.action
package com.luob.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import com.opensymphony.xwork2.ActionSupport;
@Namespace(value="/user")
@ResultPath("/WEB-INF/jsps")
@Result(name="success",location="success.jsp")
public class Annotation extends ActionSupport{
@Action(value="/exec")
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
@Action(value="/input")
public String init(){
return SUCCESS;
}
}
5.访问地址
访问input:
http://localhost:8080/Struts2_An/user/input.action
http://localhost:8080/Struts2_An/user/input
http://localhost:8080/Struts2_An/user/input.do
execute 方法就不说了
和上面的一样
6.如果修改action 的代码如下:
package com.luob.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import com.opensymphony.xwork2.ActionSupport;
@Namespace(value="/user")
@Result(name="success",location="/success.jsp")
@Action(value="Annotation")
public class Annotation extends ActionSupport{
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
public String init(){
return SUCCESS;
}
}
7.访问地址
访问execute:
http://localhost:8080/Struts2_An/user/Annotation.do
http://localhost:8080/Struts2_An/user/Annotation.action
访问 init:
http://localhost:8080/Struts2_An/user/Annotation!init.do
http://localhost:8080/Struts2_An/user/Annotation!init.action
8.当然 还可以 测试下 替换成@Result(name="success",location="success.jsp") 没有了/ 就会跑到 WEB-INF/conent/user/success.jsp
9.总之 够你研究的