一、配置web.xml
<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.test.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>
///////////使用注解来配置Action///////////////////////////
@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")
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;
}
}