Struts2
Result结果配置
全局结果
在package标签中配置global-results标签
<package name="xxx" namespace="/" extends="struts-default">
<global-results>
<result name="xxx">xxx.jsp</result>
</global-results>
</package>
作用是为package中配置的所有action提供全局的返回结果页面
局部结果
在action标签中配置result标签
<action name="xxx" method="xxx"
class="xxx">
<result>xxx.jsp</result>
</action>
作用是为当前action配置结果页面
结果类型
结果类型是在父类配置struts-default中定义的,struts框架默认实现了绝大多数的。
常用的结果类型有:
dispatcher:转发。为result标签的默认值。
<result type="dispatcher">xxx.jsp</result>由action转发到jsp。
result标签中配置的结果必须是jsp页面
chain:转发。
<result type="chain"> <param name="actionName">xxx.action</param> </result>由action转发到action。
result标签中配置的结果必须是action
redirect:重定向。
<result type="redirect"> <param name="location">xxx.jsp</param> </result>由action重定向到jsp。
result标签中配置的结果必须是jsp页面
redirectAction:重定向。
<result type="redirectAction"> <param name="actionName">xxx.action</param> </result>由action重定向到action。
result标签中配置的结果必须是action
stream: 结果为文件流。文件下载
<result name="success" type="stream"> <param name="contentType">image/jpeg</param> <param name="inputName">imageStream</param> <param name="contentDisposition">attachment;filename="document.pdf"</param> <param name="bufferSize">1024</param> </result>contentType:下载文件类型
contentDisposition:下载到客户端时,客户端文件名称
bufferSize:读文件的缓存大小
inputName:对应要输出到客户端流声明的名称
json:转发。
<package name="xxx" namespace="/xxx" extends="json-default"> <action name="json" method="xxx" class="org.itheima.struts.action.JsonAction"> <result name="success" type="json"> <param name="encoding">utf-8</param> <param name="root">jsonObject</param> </result> </action> </package>由action转发到json结果
encoding:配置编码格式
root:配置对象。action类中必须提供一个和root值相同的属性名称,且需要提供getter方法。
jsonActionRedirect: 重定向。
<action name="xxx" method="xxx" class="org.itheima.struts.action.JsonAction"> <result name="success" type="jsonActionRedirect"> xxx.action </result> </action>由action重定向到json结果。
json Plugin的加载
- 拷贝
struts2-json-plugin-xxx.jar - 让package继承
json-default
Struts与Servlet交互对接
解决的问题
客户端与服务端交互时,通常会带参数过来,服务器也会回写数据给客户端。在此过程中,参与着请求,和响应,以及会话。servlet在此过程中提供了HttpServletRequest作为获取请求数据的方案,HttpServletResponse作为响应的方案,HttpSession负责了会话方案。Struts其实是基于servlet实现的web框架,他需要遵循规则提供请求,响应和会话的API供开发人员使用,因此Struts针对这一问题提供了自己的一套API封装,提供了多种方式的访问。
ActionContext
ActionContext对象实例获取
ActionContext context = ActionContext.getContext();ActionContext是绑定在当前线程中的。框架在请求线程开启时,将ActionContext实例对象就绑定上来了,因此我们可以通过静态方法直接获取
请求参数的获得
Map<String, Object> parameters = context.getParamters();相当于Servlet中的
request.getParamters()方法数据回显到页面
context.put(key, value);相当于Servlet中的
request.setAttribute()方法
ServletActionContext
- Servlet继承子ActionContext,因此具备ActionContext的一切功能。
- ServletActionContext是对ActionContext功能的扩展,提供了多个静态方法。
请求获得
HttpServletRequest request = ServletActionContext.getRequest();响应获得
HttpServletResponse response = ServletActionContext.getResponse();
接口实现类获得
- ServletContextAware
- ServletRequestAware
- ServletResponseAware
- ParameterAware
- SessionAware
- ApplicationAware
- PrincipalAware
Struts数据的封装
属性驱动:基本数据类型封装(掌握)
页面表单
<input type="text" name="name"> <input type="text" name="password">action类
private String name; private String password; public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; }- 要求页面中提供的key和action类中属性名称必须一致
- action类中必须提供属性的setter方法
页面表达式:对象封装
页面表单
<input type="text" name="user.name"> <input type="text" name="user.password">action类
private User user; public void setUser(User user) { this.user = user; } public User getUser() { return user; }- 页面中的key相当于
对象.属性 - action中的对象名称需要要和页面中key的对象名称相同
- action中的对象中的属性名称要和页面中key的对象的属性相同
- action中的对象必须提供无参构造函数
- action中的对象必须提供getter和setter
- 页面中的key相当于
模型驱动:对象封装(掌握)
页面表单
<input type="text" name="name"> <input type="text" name="password">Aciton类需要实现ModelDriven接口,并且实现接口方法
public class Action03 extends ActionSupport implements ModelDriven<User> { private User user; @Override public User getModel() { if (user == null) { user = new User(); } return user; } }页面中的key的名称需要和模型对象的名称一致
List数据(了解)
页面表单
<input type="text" name="list[0].name"> <input type="text" name="list[0].age"> <input type="text" name="list[1].name"> <input type="text" name="list[1].age">action类
public class DataAction05 extends ActionSupport { private List<User> list; public List<User> getList() { return list; } public void setList(List<User> list) { this.list = list; } public String test() { System.out.println(list); return SUCCESS; } }
Map数据(了解)
页面表单
<input type="text" name="map['a'].name"> <input type="text" name="map['a'].age"> <input type="text" name="map['b'].name"> <input type="text" name="map['b'].age">action类
public class DataAction05 extends ActionSupport { private Map<String, User> map; public Map<String, User> getMap() { return map; } public void setMap(Map<String, User> map) { this.map = map; } }
类型转换器
解决的问题
客户端传输过程中传输特定类型的字符串时,到action类中需要转换为对应的对象时,中间的转换的问题。
主要解决的是对象类型的转换
配置方案
新建一个类继承StrutsTypeConverter,实现内部方法
public class DateConversion extends StrutsTypeConverter { private SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); @Override public Object convertFromString(Map context, String[] values, Class toClass) { if (values != null && values.length > 0) { String dateString = values[0]; try { return sdf.parse(dateString); } catch (ParseException e) { e.printStackTrace(); return null; } } return null; } @Override public String convertToString(Map context, Object o) { if (o != null && o instanceof Date) { return sdf.format(o); } return null; } }在项目的src下新建
xwork-conversion.properties,在其中配置要转换的类型java.util.Date=org.itheima.struts.action.DateConversion
本文详细介绍了Struts2框架中的结果配置方法,包括全局结果配置与局部结果配置的区别及应用,各种结果类型如dispatcher、chain、redirect等的功能与配置方式。同时,还讲解了Struts2如何通过ActionContext和ServletActionContext与Servlet进行交互,以及Struts2的数据封装机制,如属性驱动和模型驱动等。
545

被折叠的 条评论
为什么被折叠?



