Strust2 注解(Annotation)的使用

步骤:
1.导入Struts的jar包, 红色标注的为注解必须要用的包
           commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-codebehind-plugin-2.1.8.1.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar


2.配置web.xml过滤器, 红色为必须注意的地方.不用注解的过滤器可以忽略此段内容.初始化的名字(actionPackages)固定不变.value(com.meiyoudao)为你写的action类所在的包路径.过滤器会根据此内容去找匹配的action类.多个包用,号分隔.
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.meiyoudao</param-value>
</init-param>
<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>actionPackages</param-name>
			<param-value>com.meiyoudao</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>



3.写Action类,继承ActionSupport类.注意两个类的名称和所访问的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";
	}

}

你可能感兴趣的:(annotation)