struts2下的annotation
web.xm写法如下:
?包名最好3级目录:
package com.lele.action;import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.ExceptionMapping; import org.apache.struts2.convention.annotation.ExceptionMappings; 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.Results; import com.opensymphony.xwork2.ActionSupport; /** * * @author lele609 * */ @ParentPackage("struts-default") // 父包 @Namespace("") /*@Results( { @Result(name = com.opensymphony.xwork2.Action.SUCCESS, location = "/msg.jsp"), @Result(name = com.opensymphony.xwork2.Action.ERROR, location = "/erlogin.jsp") })*/ // @ExceptionMappings 一级声明异常的数组// @ExceptionMapping 映射一个声明异常 @ExceptionMappings( { @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") }) public class LoginAction extends ActionSupport { private static final long serialVersionUID = -2554018432709689579L; private String loginname; private String pwd; // @Action(value="login") 指定某个请求处理方法的请求URL。注意,它不能添加在Action类上,要添加到方法上。 @Action(value = "loginName",results={@Result(name="success",location="/msg.jsp"),@Result(name="error",location="/erlogin.jsp")}) public String login() throws Exception { if ("HEFE".equalsIgnoreCase(loginname.trim())&&"123".equalsIgnoreCase(pwd.trim())) { return SUCCESS; } else { System.out.println("==========="); return ERROR; } } @Action(value = "add", results = { @Result(name = "success", location = "/index.jsp") }) public String add() throws Exception { return SUCCESS; } public String getLoginname() { return loginname; } public void setLoginname(String loginname) { this.loginname = loginname; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }?就可以测试了 1 楼 canuo 2012-05-06 求struts2.1.8 实例