Struts2 零配置注意事项

本文深入解析了Struts2框架中Action和Namespace的使用规则,包括它们如何影响URL路径、命名空间的作用及其相互之间的区别,以及如何在有效命名空间内查找Action和JSP文件。

命名空间:

@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”;
    }
}

Action跳转到action

(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”;
}


转载于:https://my.oschina.net/wmsjhappy/blog/273601

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值