先是一些参数设置,网上COPY的:
<!-- 指定WEB应用的编码集,相当于调用HttpServletRequest.setCharacterEncodint方法,如果使用了 velocity或freemarker,它也用于指定输出的编码格式 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!--<!– 指定请求后缀为.action,指定多个请求后缀用逗号分隔 –>--> <!--<constant name="struts.action.extension" value="do,action" />--> <!--设置浏览器是否缓存静态内容,建议:开发阶段关闭,运行时开启 --> <constant name="struts.serve.static.browserCache" value="false" /> <!--当struts.xml配置文件修改后,系统是否重新加载该文件,开发阶段打开此功能 --> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 开发提示:出错时打印更详细的信息--> <constant name="struts.devMode" value="true" />
在使用Action请求转发到另一个Action时,不可以使用默认的type类型。默认type类型为dispatcher,该类型只可以用于Action跳转到页面(使用服务器请求转发方式)。
只能使用redirect、redirectAction和chain来进行Action之间的跳转。
<package name="KaitoAction" extends="struts-default" namespace="/kaito"> <action name="user-*" class="com.Vocaloid.Kaito.Action.LoginAction" method="{1}"> <result name="emptyUsername">/Jsp/emptyUsername.jsp</result> <result name="loginSuccess">/Jsp/loginSuccess.jsp</result> <result name="loginFailed">/Jsp/loginFailed.jsp</result> <result name="view">getInfo.action</result> </action> <action name="getInfo" class="com.Vocaloid.Kaito.Action.GetAction"> <result name="printInfo">/Jsp/view.jsp</result> </action> </package>
当使用result的默认type请求转发到action时,会出现如下错误:
我在每个Action前加了“类名 方法名”的输出用于定位错误,从输出可以看出,getInfo.action的excute()方法并没有能够调用起来,即在第一个action向第二个action跳转时出现了错误:
如果Action在不同package下而又没有命名空间(namespace)的话,在IDEA中会报错(Myelipse和eclipse没尝试),提示说无法识别,有时会导致tomcat启动时失败(我只出现过一次启动失败,之后即使报错也可以正常启动并识别action):
如果要进行action之间的传参,result的type类型应该使用chain。这才是以服务器请求转发的方式实现的action之间的跳转:
/Jsp/emptyUsername.jsp
/Jsp/loginSuccess.jsp
/Jsp/loginFailed.jsp
getInfo.action
/Jsp/view.jsp