JAVA_WEB Struts2学习:Struts2解耦、耦合方式获取request、session、aplication对象

发布时间:2016-12-8 18:10:50 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"JAVA_WEB Struts2学习:Struts2解耦、耦合方式获取request、session、aplication对象",主要涉及到JAVA_WEB Struts2学习:Struts2解耦、耦合方式获取request、session、aplication对象方面的内容,对于JAVA_WEB Struts2学习:Struts2解耦、耦合方式获取request、session、aplication对象感兴趣的同学可以参考一下。

Struts: -OGNL与Servlet的最大区别:数据转移以及类型转化。 xwork-conversion.properties 处理类型转换错误(如果前台校验js很完善:等前台录入数据格式正确后再传给服务器),那么了解该技术即可。 解耦、耦合方式获取request、session、aplication对象:  类 extends ActionSuppport //解耦方式获取request对象 Map request = (Map)ActionContext.getContext().get("request"); request.put("req", "这是request范围内的数据"); //耦合方式获取request对象 HttpServletRequest req = ServletActionContext.getRequest(); //req.setAttribute("",""); System.out.println(req.getParameter("test")); //解耦方式获取session对象 Map session = ActionContext.getContext().getSession(); session.put("ses", "这是Session范围内的数据"); //耦合方式获取session对象 HttpSession ses = req.getSession(); //ses.setAttribute("", ""); //解耦方式获取application对象 Map application = ActionContext.getContext().getApplication(); application.put("app", "这是Application范围内的数据"); //耦合方式获取Application对象 ServletContext app = ServletActionContext.getServletContext(); //app.setAttribute("", ""); action成员属性默认为ognl上下文根目录。 <set name="..." scope="request"> --如果只在当前页操作,用request范围标识 <set name="..." scope="session"> --如果要在多个页操作,用session范围标识 Dojo: 导入Struts2 Ajax标签 <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <%@ taglib uri="/struts-tags" prefix="s" %> <%@ taglib uri="/struts-dojo-tags" prefix="sx"%> 以下是具体演示代码: index: <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <link rel="stylesheet" type="text/css" href="<%=path%>/js/My97DatePicker/skin/WdatePicker.css"> <script language="javascript" type="text/javascript" src="<%=path%>/js/My97DatePicker/WdatePicker.js"></script> </head> <body> <s:form action="myAction!init"> <table><caption>OGNL练习</caption> <s:textfield name="age" label="年龄"></s:textfield><s:fielderror fieldName="age"></s:fielderror> <s:textfield name="birthday" label="生日" onfocus="WdatePicker({isShowClear:false,readOnly:true,doubleCalendar:false,dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate"></s:textfield><s:fielderror fieldName="birthday"></s:fielderror> <s:textfield name="point" label="坐标"></s:textfield><s:fielderror fieldName="point"></s:fielderror> <s:set var="test" value="123" scope="request"></s:set> <s:submit value=" 提 交 "></s:submit> </table> </s:form> </body> </html> struts.xml: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- 资源文件 --> <constant name="struts.custom.i18n.resources" value="message"/> <package name="TestManager" extends="struts-default"> <action name="myAction" class="com.zuxia.yc42.action.MyTestAction"> <result name="success">/jsp/success.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts> public class MyTestAction extends ActionSupport: package com.zuxia.yc42.action; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.zuxia.yc42.po.Point; public class MyTestAction extends ActionSupport { //action成员属性 默认为ognl上下文根目录 private Integer age; private Date birthday; private Point point; private List<String> list; private Map<Integer,String> map; public MyTestAction() { list = new ArrayList<String>(); list.add("aaaaaaa"); list.add("bbbbbbbb"); list.add("cccccccccc"); map = new HashMap<Integer,String>(); map.put(1, "小强"); map.put(2, "小明"); map.put(3, "小李"); } public String init() { Object o = new Object(); System.out.println(age.toString()+"\t"+birthday+"\t"+point); //解耦方式获取request对象 Map request = (Map)ActionContext.getContext().get("request"); request.put("req", "这是request范围内的数据"); //耦合方式获取request对象 HttpServletRequest req = ServletActionContext.getRequest(); //req.setAttribute("",""); System.out.println(req.getParameter("test")); //解耦方式获取session对象 Map session = ActionContext.getContext().getSession(); session.put("ses", "这是Session范围内的数据"); //耦合方式获取session对象 HttpSession ses = req.getSession(); //ses.setAttribute("", ""); //解耦方式获取application对象 Map application = ActionContext.getContext().getApplication(); application.put("app", "这是Application范围内的数据"); //耦合方式获取Application对象 ServletContext app = ServletActionContext.getServletContext(); //app.setAttribute("", ""); return SUCCESS; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Point getPoint() { return point; } public void setPoint(Point point) { this.point = point; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Map<Integer, String> getMap() { return map; } public void setMap(Map<Integer, String> map) { this.map = map; } } success.jsp: <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:debug></s:debug> <s:form> 年龄:<s:property value="#parameters.age" /> 生日:<s:property value="birthday"/> 生日1:<s:date name="birthday" format="yyyy-MM-dd HH:mm:ss"/> 坐标:<s:property value="point"/> 默认语言:<s:property value="#attr.locale"/><br/> Session:<s:property value="#session.ses"/><br/> request:<s:property value="#request.req"/><br/> application:<s:property value="#application.app"/> <s:set var="ts" value="123456"></s:set> <s:property value="%{#ts}"/><br/> List集合数据获取:<s:property value="list[1]"/> List集合长度:<s:property value="list.size"/> <br/> Map集合获取数据:<s:property value="map[2]"/> <br/> <s:property value="#request.test"/> </s:form> </body> </html> public class Point implements Serializable: package com.zuxia.yc42.po; import java.io.Serializable; public class Point implements Serializable { private Integer x; private Integer y; public Integer getX() { return x; } public void setX(Integer x) { this.x = x; } public Integer getY() { return y; } public void setY(Integer y) { this.y = y; } @Override public String toString() { return "X:"+x+"\t Y:"+y; } } xwork-conversion.properties: java.util.Date=com.zuxia.yc42.util.DateConverter public class DateConverter extends StrutsTypeConverter: package com.zuxia.yc42.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import org.apache.struts2.util.StrutsTypeConverter; public class DateConverter extends StrutsTypeConverter { private SimpleDateFormat[] sm = {new SimpleDateFormat("yyyy年MM月dd日"), new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyy.MM.dd"), new SimpleDateFormat("yyyy/MM/dd"), new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"), new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"), }; @Override public Object convertFromString(Map context, String[] values, Class toType) { String time = values[0]; Date date = null; for(SimpleDateFormat simple:sm) { try { date = simple.parse(time); } catch (ParseException e) { continue; } } return date; } @Override public String convertToString(Map context, Object obj) { SimpleDateFormat simple = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); return simple.format((Date)obj); } } public class PointConverter extends StrutsTypeConverter: package com.zuxia.yc42.util; import java.util.Map; import org.apache.struts2.util.StrutsTypeConverter; import com.zuxia.yc42.po.Point; /** * 自定义类型转换类 * @author wang_hao * */ public class PointConverter extends StrutsTypeConverter { /** * 将客户端数据转换成服务器端数据类型 */ @Override public Object convertFromString(Map context, String[] values, Class toType) { String value = values[0]; String[] vals = value.split(","); Integer x = Integer.parseInt(vals[0]); Integer y = Integer.parseInt(vals[1]); Point point = new Point(); point.setX(x); point.setY(y); return point; } /** * 将服务器端数据类型转换为String类型 */ @Override public String convertToString(Map context, Object obj) { return obj.toString(); } }

上一篇:七个高效的文本编辑习惯(以Vim为例)
下一篇:PHP分页类

相关文章

相关评论