标签:
对数据操作的所有方法:(把数据保存到域中)
方式1、直接获取ServletAPI(耦合)
核心类:ServletActionContext 提供静态方法(解耦)
HttpServletRequest request=ServletActionContext.getRequest(); HttpSession session= request.getSession(); ServletContext application=ServletActionContext.getServletContext(); request.setAttribute("request_data", "request_data1"); session.setAttribute("session_data", "session_data1"); application.setAttribute("application_data", "application_data1");
方式2、通过ActionContext获取不同(代表request、session、application、)的Map
ActionContext ac=ActionContext.getContext(); Map<String, Object> request=ac.getContextMap(); Map<String, Object> session=ac.getSession(); Map<String,Object> application=ac.getApplication(); request.put("request_data", "request_data"); session.put("request_data","session_data"); application.put("application_data", "application_data");
方式3:实现接口的方法:(RequestAware、SessionAware、)
(解耦)
package cn.lyjs.action; import java.util.Map; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionSupport; public class DataAction1 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{ private Map<String,Object> request; private Map<String,Object> session; private Map<String,Object> application; @Override public void setApplication(Map<String, Object> application) { // TODO Auto-generated method stub this.application=application; } @Override public void setRequest(Map<String, Object> request) { // TODO Auto-generated method stub this.request=request; } @Override public void setSession(Map<String, Object> session) { // TODO Auto-generated method stub this.session=session; } @Override public String execute() throws Exception { // 数据 request.put("request_data", "request_data1_actionAware"); session.put("session_data", "session_data1_actionAware"); application.put("application_data", "application_data1_actionAware"); // return SUCCESS; } }
请求数据自动封装:
实现原理:
参数拦截器:
类型转换:
Struts中JSP提交的数据,Struts会自动转换为action中属性类型
对于基本数据类型以及日期类型会自动转换
日期类型只支持yyyy-MM-dd
如何使格式,需要自定义类型转换器:
局部转换器
全局转换器
标签:
原文地址:http://www.cnblogs.com/lyjs/p/5090909.html