命名空间:
@Namespace
当出现@Namespace时,它会取代当前所有Action的名空间。
例:当一个LoginAction在org.hzy.actions.xx下面中,其命名空间是/xx,(其LoginAction方法名叫login())
访问路径:http:localhost:8080:/struts2/xx/login,
出现@Namespace("/aa"),则其名空间是/aa,访问/xx/login则找不到,而是/aa/login
访问路径:http:localhost:8080:/struts2/aa/login,
@Action
当@Action中最前面出现"/"时,它会忽略 @Namespace中的名空间,
例:上面是/aa/login,当出现@Action("/ss")时,这时的名空间是/,
访问路径:http:localhost:8080:/struts2/ss
而当@Action中没出现"/"时,它是走@Namespace的名空间,
例:上面是@Namespace("/aa"),当出现@Action("bb")时,这时的名空间是/aa,
访问路径:http:localhost:8080:/struts2/aa/bb
当Action跳到JSP中,是以当前Action的名空间下去找的,
例:访问路径:http:localhost:8080:/struts2/aa/bb,它找bb对应的JSP,是在content/aa下找的,
是在Action当前名空间下找。
当在有效的名空间当中找不到action,零配置就会去找相应的jsp,两者都找不到则报错。
例子:
package org.hzy.actions.xx;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ResultPath;
@ResultPath("/WEB-INF/Jsp")//改变默认的/WEB-INF/content路径
//@Namespace("/cc")//覆盖org.hzy.actions.xx(/xx)
public class HelloWorldAction {
// @Actions({
// @Action("/aa"),
// @Action("/los"),
// @Action("bb") //是/cc/bb
// })
public String login(){
System.out.println("helloworld!!!!!!!!!!!!!");
return "success";
}
}
(1)
@Action("login")//跳到login-query
public String login() throws Exception {
// TODO Auto-generated method stub
System.out.println("login!!!!!!!!!!!!!!!!!!!!!");
System.out.println(this.uname);
return "query";
}
@Action("login-query")//对应的是content下面的login-query-los.jsp
public String query() throws Exception {
// TODO Auto-generated method stub
System.out.println("query!!!!!!!!!!!!!!!!!!!!!");
System.out.println(this.uname);
return "los";
}
(2)
//这里location="test"是跳到test action
@Results({ @Result(name = "failure", location = "test", type="chain") })
public class LosAction {
@Action(value = "url", results = { @Result(name = "success", location = "/WEB-INF/Jsp/los.jsp", type = "chain") })
public String login() throws Exception {
// TODO Auto-generated method stub
System.out.println("los!!!!!!!!!!!!");
return "failure"; //会先找本身的result 再去找全局的
}
@Action(value = "test", results = { @Result(name = "success", location = "/WEB-INF/Jsp/los.jsp", type = "dispatcher") })
public String test() throws Exception {
// TODO Auto-generated method stub
System.out.println("test!!!!!!!!!!!!");
return "success";
}