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