歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2 注解模式的幾個知識點

Struts2 注解模式的幾個知識點

日期:2017/3/1 9:56:12   编辑:Linux編程

struts的東西也只是以前會簡單的使用。於是就動手寫一寫。遇到了一些問題。,還是不熟悉,只是會用,知道怎麼配置怎麼寫action。現將遇到的問題記錄如下。

1.redirect , redirectAction的配置,有的時候,在使用這些result type的時候會需要跳轉到不同的namespace中。那麼在配置中該怎麼寫呢?

package com.struts2.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;


@ParentPackage(value="struts-default")
@Namespace(value = "/admin")
@Controller
public class IndexAction{
private int i;

public int getI() {
return i;
}

public void setI(int i) {
this.i = i;
}

@Action(value = "/login", results = {
@Result(name="sucess", location = "/index.jsp"),
@Result(name="chain", location = "chain",type="chain"),//同namespace下 chain
@Result(name="otherNsChain", type="chain",params= {
"namespace","/user","actionName","chain"
}),//不同namespace下
@Result(name="redirect",type="redirect",location="redirect.action"),//同namespace
@Result(name="otherNsRedirect",type="redirect",location="/user/redirect.action"),//不同namespace
@Result(name="redirectAction",type="redirectAction",params= {
"actionName","redirectAction"
}),//同namespace
@Result(name="otherNsRedirectAction",type="redirectAction",params= {
"namespace","/user","actionName","redirectAction"
})//不同namespace
})
public String login() {
String loc = "sucess";

int j = getI();
switch (j) {
case 1:
loc="chain";
break;
case 2:
loc="redirect";
break;
case 3:
loc="redirectAction";
break;
case 4:
loc = "otherNsChain";
break;
case 5:
loc = "otherNsRedirect";
break;
case 6:
loc = "otherNsRedirectAction";
}
System.out.println(loc);
return loc;
}

@Action(value="chain",results= {
@Result(name="chain",location="/chain.jsp")
})
public String chain() {
String loc = "chain";
System.out.println("IndexAction.chain()");
System.out.println("chain");

return loc;
}


@Action(value="redirect",results= {
@Result(name="redirect",location="/redirect.jsp")
})
public String redirect() {
String loc = "redirect";
System.out.println("同namespace下的 redirect");
System.out.println("IndexAction.redirect()");
return loc;
}

@Action(value="redirectAction",results= {
@Result(name="redirectAction",location="/redirectAction.jsp")
})
public String redirectAction() {
String loc = "redirectAction";
System.out.println("IndexAction.redirectAction()");
System.out.println("同namespace下 的 redirectAction");
return loc;


}

}

---------------------------------------------------------------

package com.struts2.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

@ParentPackage(value="struts-default")
@Namespace(value="/user")
@Controller
public class UserAction {

@Action(value="redirectAction",results= {
@Result(name="redirectAction",location="/otherNsRedirectAction.jsp")
})
public String redirectAction() {
String loc = "redirectAction";
System.out.println("UserAction.redirectAction()");
System.out.println("不同namespace下的redirectAction");
return loc;


}

@Action(value="/chain",results= {
@Result(name="chain",location="/OtherNschain.jsp")
})
public String chain() {
String loc = "chain";
System.out.println("UserAction.chain()");
System.out.println("不同namespace下的chain");
return loc;
}


@Action(value="/redirect",results= {
@Result(name="redirect",location="/otherNsRedirect.jsp")
})
public String redirect() {
String loc = "redirect";
System.out.println("不同namespace下的 redirect");
System.out.println("UserAction.redirect()");
return loc;
}




}

Copyright © Linux教程網 All Rights Reserved