现在的位置: 首页 > 综合 > 正文

JavaScript的应用总结(1)

2013年03月01日 ⁄ 综合 ⁄ 共 3349字 ⁄ 字号 评论关闭

      以前因为接触的项目,用到javascript的地方,本人对javascript这个东西认识不够,甚至以为是前台程序员的工作。最近做的项目里面用到了很多这种脚本语言,才发现这个小东东实在是很可爱,虽然很淘气(不是很面向对像,不是很好调试),但是只要是做web开发的,都难免要接触到他,把这个东西用好了,可以达到很好的展示层效果,能够很好的与用户交互。同时其中XmlHTTPRequest异步这个东西也是一种有用的技术,学习好它就能够更好的了解web这个东西,也是对自己综合素质的提高。千里之行始于足下,这里就从点滴有用的开始总结,是对自己知识的积累,也希望能给同志们一些帮助。

       这篇文章可能会很乱没得什么条理,因为我会从一些实际应用着手,不像写书那样,当然也会尽量做的明白一点。费话少说了,下面进入正题。

注:这样的文章是一些小技巧的总结,会不断更新……

1、限制操作(不能重定向等)的页面的应用

    有时候我们在做web页面时候,特别是在做应用系统的时候,希望用记不能随便改写地址栏,不能重定向,不能最大,最小化等等……;这时候可以设置系统从一个页面开始(manager.jsp),即为开始页面(web.xml中配置)。从manager.jsp打开一个新的页面,用window.open(属性)方法。设置属性限制页面的应用,然后设置manager.jsp自动关闭即可。

 

manager.jsp如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   
    <title>系统首页</title>
   
 <script type="text/javascript" src="<%=path%>/templates/js/object.js"></script>
 <script type="text/javascript"> 
  var winObj = new createWindowObject();
  var url = "<%=path%>/login.jsp";
  //窗体名称/路径、宽度、高度、是否全屏0;1
  winObj.init("",url,-1,-1,1);
  winObj.show();

  //关闭当前窗口,下面两行都是为了设置,关闭不弹出确认选项。
  window.opener=null;//IE6
     window.open("","_self");//IE7/IE8
     setTimeout("window.close(); ",5000);
    
 </script>
  </head>
  <body>
  </body>
</html>

 

web.xml中加入:

<welcome-file-list>
        <welcome-file>/manager.jsp</welcome-file>
    </welcome-file-list>

 

可以用struts2拦截器:

package root.intercept;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor {

 public String intercept(ActionInvocation invocation) throws Exception {
  ActionContext ac = invocation.getInvocationContext();
  Map session = ac.getSession();
  javax.servlet.http.HttpServletRequest request = (javax.servlet.http.HttpServletRequest) ac
    .get(ServletActionContext.HTTP_REQUEST);
  javax.servlet.http.HttpServletResponse response = (javax.servlet.http.HttpServletResponse) ac
    .get(ServletActionContext.HTTP_RESPONSE);
  String url = request.getRequestURI() + "?"
    + request.getQueryString();
  //request.getSession().setAttribute("SYSYHBH", "10000");
  
  //System.out.println(url);
  
  //�����û�Ȩ�� start
  String sysmkbh = request.getParameter("SYSMKBH");
  if(sysmkbh==null){
   if(request.getSession().getAttribute("SYSMKBH")!=null){
    sysmkbh = request.getSession().getAttribute("SYSMKBH").toString();
   }else{
    sysmkbh = "";
   }
  }
  request.getSession().setAttribute("SYSMKBH",sysmkbh);
  String sGnStr = ",";
  if(request.getSession().getAttribute("SYSYHJSMKGN")!=null){
   List lGn = (List)request.getSession().getAttribute("SYSYHJSMKGN");
   for(int i=0;i<lGn.size();i++){
    HashMap hmGn = (HashMap)lGn.get(i);
    if(hmGn.get("MKBH").equals(sysmkbh)){
     sGnStr = sGnStr+hmGn.get("GNMC")+",";
    }
   }
  }
  request.getSession().setAttribute("SYSYHJSMKGN_MK",sGnStr);
  //�����û�Ȩ��end
  
  if(session.get("SYSYHBH")==null){
   if(url.indexOf("loginAction.do")!=-1){
   }else{
    String rootPath = request.getContextPath();
    response.setCharacterEncoding("gb2312");
    response.getWriter().write(
      "<script>"
      + "alert('�����ʱ�������µ�½��');"
      + "if(top != self){top.location='"
      + rootPath
      + "/login.jsp'}else{window.location='"
      + rootPath + "/login.jsp'}"
      + "</script>");
    return null;
   }
   
  }
  return invocation.invoke();
 }
}

抱歉!评论已关闭.