Strust2 注解(Annotation)的使用

步骤:
[color=darkblue][size=large]1.导入Struts的jar包[/size][/color],[color=red]红色[/color]标注的为注解必须要用的包
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
[size=medium][color=red]struts2-codebehind-plugin-2.1.8.1.jar[/color][/size]
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar


[size=large][color=darkblue]2.配置web.xml过滤器[/color][/size],[color=red]红色[/color]为必须注意的地方.不用注解的过滤器可以忽略此段内容.初始化的名字(actionPackages)固定不变.value(com.meiyoudao)为你写的action类所在的包路径.过滤器会根据此内容去找匹配的action类.多个包用,号分隔.
[color=red]
actionPackages
com.meiyoudao
[/color]


struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

actionPackages
com.meiyoudao




struts2
/*




[color=darkblue][size=large]3.写Action类,继承ActionSupport类[/size][/color].注意两个类的名称和所访问的URL的区别
代码实例,访问的RUL为:http://localhost/站点名称/ok.action
package com.meiyoudao;

import org.apache.struts2.config.Namespace;
import org.apache.struts2.config.Result;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("/")
@Result(name = "SUCCESS", value = "/index1.jsp")
public class OkAction extends ActionSupport {

@Override
public String execute() throws Exception {
System.out.println("================");
return "SUCCESS";
}

}



代码实例,访问的RUL为:http://localhost/站点名称/haha.action
package com.meiyoudao;

import org.apache.struts2.config.Result;

import com.opensymphony.xwork2.ActionSupport;

@Result("/index.jsp")
public class Haha extends ActionSupport {

@Override
public String execute() throws Exception {
System.out.println("+++++++++++++");
return "success";
}

}

你可能感兴趣的:(Strust2 注解(Annotation)的使用)