• Strut2中的session和servlet中的session的区别


    在jsp中,内通过内置对象 HttpServletRequest的getSession()方法可以获取到HttpSession,比如:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@taglib uri="http://www.wyl.suneyaee" prefix="Wyl"%>
     4 <!-- 引入jquery等 -->
     5 <%@ include file="/WEB-INF/head.jsp"%>
     6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     7 <html>
     8 
     9 <head>
    10 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    11 <script type="text/javascript">
    12 <!--
    13     $(function() {
    14         $('input').keydown(function(e) {
    15             var curKey = e.which;
    16             //46 是 Delete键
    17             if (curKey == 46) {
    18                 clear(this);
    19                 $(this).focus();
    20             }
    21         });
    22 
    23         $('#idUserName').keydown(function(event) {
    24             var curKey = event.which;
    25             //13 是回车键
    26             if (curKey == 13) {
    27                 console.log('你按的是:回车' + '');
    28             }
    29 
    30         });
    31         /**
    32             清除value值,主要是针对于input
    33          */
    34         function clear(obj) {
    35             $(obj).attr('value', null);
    36         }
    37     })
    38 </script>
    39 
    40 <title>Insert title here</title>
    41 </head>
    42 <body>
    43 <Wyl:getIP />
    44 
    45 <form action="doAjax"><input value="aaa" type="text"> <br>
    46 <input value="doAJax" type="submit"></form>
    47 
    48 <br>
    49 <form action="wdAjax"><input value="MyAjax.myAjax()..."
    50     type="text"> <br>
    51 <input value="wdAjax" type="submit"></form>
    52 
    53 <br>
    54 用户名
    55 <form action="toLogin"><input type="text" name="user.userName">
    56 <br>
    57 <%
    58     request.getSession().setAttribute("hahaAnyKey", "我是hahaAnyKey.jsp页面传入session的值");
    59 %>
    60 <input type="text" name="user.passWord"> <input type="submit">
    61 </form>
    62 
    63 
    64 <br>
    65 用户名2 ,模型驱动,而且这个表单对应的LoginAction2.java中封装了dto,而且可以使用Delete键清空输入框
    66 <form action="toLogin2"><input type="text" name="userName"
    67     id="idUserName"> <br>
    68 <input type="text" name="passWord"> <input type="submit">
    69 </form>
    70 
    71 -----------
    72 <span xmlns="http://www.w3.org/1999/xhtml"
    73     class="button collapse-button"></span>
    74 
    75 </body>
    76 </html>

    中的58行,就可以获取到session,然后直接在HttpSession里存放一个值。

    而在Struts2中,比如在一个action中,可以直接通过ActionContext.getContext().getSession()获取Session对象(注意:这个ActionContext是

    com.opensymphony.xwork2.ActionContext类型的),实际上这里的Session是一个Mapper,只是Struts2做了封装,我们还是可以通过这个mapper类型的Mapper来获取到上面登录页面放在HttpSession中的属性。
    比如在一个action中就可以通过如下代码获取到上面jsp中存放在HttpSession中的属性,
    1 ActionContext atx = ActionContext.getContext();
    2             Map<String, Object> session = atx.getSession();
    3             Set<String> set = session.keySet();
    4             Iterator it =  set.iterator();
    5             while(it.hasNext()){
    6                 String key = (String) it.next();
    7                 String val = (String) session.get(key);
    8                 System.out.println("key:"+key+",value:"+val);
    9             }

    打印结果:

    key:hahaAnyKey,value:我是hahaAnyKey.jsp页面传入session的值

    这个Action的完整代码如下:

     1 package com.log;
     2 
     3 import java.io.PrintWriter;
     4 import java.util.Iterator;
     5 import java.util.Map;
     6 import java.util.Set;
     7 
     8 import javax.servlet.http.HttpServletResponse;
     9 
    10 import org.apache.struts2.ServletActionContext;
    11 
    12 import com.log.entity.User;
    13 import com.log.service.UserService;
    14 import com.opensymphony.xwork2.ActionContext;
    15 import com.opensymphony.xwork2.ActionSupport;
    16 import com.util.UtilTime;
    17 
    18 /**
    19  * 普通的登陆
    20  * 
    21  * @author Wei
    22  * 
    23  */
    24 public class LoginAction extends ActionSupport {
    25     /**
    26      * 
    27      */
    28     private static final long serialVersionUID = 1L;
    29     private User user;
    30     private UserService uService = new UserService();
    31     private String error;
    32 
    33     public String getEror() {
    34         return error;
    35     }
    36 
    37     public void setEror(String error) {
    38         this.error = error;
    39     }
    40 
    41     public UserService getuService() {
    42         return uService;
    43     }
    44 
    45     public void setuService(UserService uService) {
    46         this.uService = uService;
    47     }
    48 
    49     public User getUser() {
    50         return user;
    51     }
    52 
    53     public void setUser(User user) {
    54         this.user = user;
    55     }
    56 
    57     @Override
    58     public String execute() throws Exception {
    59         /**
    60          * 账号密码:wyl,1234
    61          */
    62         if (uService.login(user)) {
    63             ActionContext atx = ActionContext.getContext();
    64             Map<String, Object> session = atx.getSession();
    65             //对应于 WylInterceptor.java拦截器,这个拦截器里需要用到
    66             session.put("currentUser", user);
    67             //用来设置登录超时的参数
    68             session.put("lastTime", UtilTime.getCurTime());
    69             return SUCCESS;
    70         } else {
    71 //            this.error = "error";
    72 //            return this.error;
    73             HttpServletResponse  resp = ServletActionContext.getResponse();
    74             //不加 resp.setContentType("text/html;charset=UTF-8"); 会出现页面的中文为乱码的情况
    75             resp.setContentType("text/html;charset=UTF-8");
    76             PrintWriter out  = resp.getWriter();
    77             out.println("账号:"+user.getUserName()+",<br/>密码:"+user.getPassWord()+"  的不正确,请确认");
    78 //            out.println("<br/><input type="button" value="点我" />");
    79             out.println("<a href="login.jsp" style="color:orange;">点我重新登陆</a>");
    80             /**
    81              * 下面的Map<String, Object> session = atx.getSession();为了演示获取到的session(实际上是Mapper,Struts2框架做了封装)
    82              */
    83             ActionContext atx = ActionContext.getContext();
    84             Map<String, Object> session = atx.getSession();
    85             Set<String> set = session.keySet();
    86             Iterator it =  set.iterator();
    87             while(it.hasNext()){
    88                 String key = (String) it.next();
    89                 String val = (String) session.get(key);
    90                 System.out.println("key:"+key+",value:"+val);
    91             }
    92             //什么都不返回,直接用HttpServletResponse在网页上写提示
    93             return null;
    94         }
    95     }
    96 }

    ,调试截图:

  • 相关阅读:
    go ERROR invalid character '<' looking for beginning of value
    C#实现将网址生成二维码图片
    二、WPF入门教程——Bingding学习
    一、WPF入门教程——创建WPF项目
    C#实现DataTable行列转置
    VBS整蛊代码
    Task.WhenAll和Task.WhenAny
    Task.WaitAll和Task.WaitAny
    CancellationTokenSource
    组合ContinueWith
  • 原文地址:https://www.cnblogs.com/Sunnor/p/5932484.html
Copyright © 2020-2023  润新知