在百度贴吧看到一个水贴,问大家搭建SSH需要多久。然后突然想到,自己在这段时间一直在看看《Core Java》,struts的东西也只是以前会简单的使用。于是就动手写一写。遇到了一些问题。,还是不熟悉,只是会用,知道怎么配置怎么写action。现将遇到的问题记录如下。
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; } }相信大家一定看到了两个class中定义了一样的action,不过看类的元数据,是不同的命名空间。这里比较重要(对我来说)的是
@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 })
com.opensymphony.xwork2.ActionChainResult.class
。redirect的一致,如果在不同的命名空间,直接写命名空间+action的名字。 redirectAction的和chain差不多,但也有些许的区别。redirectAction没有location的配置,action的name需要在params里面配置,key为actionName,namespace也需要在params里面配置,key为namespace。更多的请参考struts-core下的org.apache.struts2.dispatcher.ServletActionRedirectResult.class。下面附上一些常用的几个result type的类
<result-types> <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/> <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/> <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /> </result-types>