全局配置:通常用于整个项目的登录界面或者错误界面
要求:要跳转到同一个界面的返回值都相同
在struts.xml中进行配置,作用于整个包
<package name=”user” extends=”struts-default”>
<global-results>
<result name=”error”> /error.jsp</result>
</global-results>
</package>
跳转配置:
Struts2常用的结果集类型有dispatcher ,redirect和 redirectAction
其中,dispatcher(转发)为默认的结果类型
Redirect:重定向URL路径改变 是客户端变化 只能在URL中加上参数字段
Dispatcher 转发 URL路径不变 是服务器端的变化 会自动带上参数跳转、
<action name=”user”>
<result name=”success” type=”redirect”>/index.jsp</result>
</action>
重定向后路径会从http://localhost:8080/strutsDemo/user.action
变为http://localhost:8080/strutsDemo/index.jsp
当目标文件在WEB-INF下时,我们不能使用redirect
因为tomcat规定不能直接访问WEB-INF下的文件,使用redirect就直接访问了。所以我们这里要使用默认的类型dispatcher (可不进行type设置,因为是默认)
<action name=”user”>
<result name=”success” type=”dispatcher”>/WEB-INF/index.jsp</result>
</action>
注意:使用redirect重定向到其他资源,将昌盛一个新的请求,原来的请求内容将全部丢失。(意味着目标文件是不能访问到Action实例的,以及Action错误和字段错误等)
如果某些数据需要被访问
解决方法:
1.将数据保存在session中
ActionContext.getContext().getSession().put("userInfo", userInfo);
2.通过参数传递数据
参数(url?name=value)
location (默认) - 指定该逻辑视图对应的实际视图资源(不用管,反正是默认)可不设置
parse - 默认为true. 如果设置为false, location参数不会被当作Ognl表达式解析.可不设置
示例:
<result type=”redirect” parse=”true”>list.action?user.userName=${user.userName}
</result>
redirectAction
redirectAction也是重定向到其他资源,重新生成一个新的请求,也会丢失第一次的请求信息。但是是用于定向到Action,它使用ActionMapperFactory类的ActionMapper实现重定向。
示例:
<result type=”redirectAction”>
<param name=”actionName”>login</param>//跳转的目标活动的活动名(必选参数)
<param name=”namespace”>/login</param> //跳转的目标活动所在的命名空间名称(当两个活动在同一个命名空间中,可以省略namespace参数的设置)
</result>
常量配置
1.通过struts.xml文件;(推荐)//通过常量加载资源文件
<constant name="struts.custom.i18n.resources" value="cn.itcast.config.msg"></constant>
2.通过web.xml文件
<filter>
<init-param>
<param-name>struts.ui.theme</param-name>
<param-value>simple</param-value>
</init-param>
</filter>
3.通过struts.properties文件
常量名=常量值 ----每行配置一个常量
struts.ui.theme=simple
注意:Struts2框架搜索常量的顺序如下,同一个常量出现在多个文件中时,后出现的常量值会覆盖前面的值。
1.struts-default.xml:在struts-core-2.x.x.x.jar文件中
2.struts-plugin.xml:在struts-Xxx-2.x.x.x.jar等Struts2插件jar文件中
3.struts.xml
4.struts.properties
5.web.xml
修改主题:(写包外)
<constant name=”struts.ui.theme” value=”simple”></constant>
注意:当主题设置为simple是,表单中的label不生效,必须在外面自己写上用户名
<s:textfield name=”user.name” label=”用户名”></textfield>