首页 > 代码库 > sessionScope整体的意思是获得存放在session.setAttrbute(key,value)的值

sessionScope整体的意思是获得存放在session.setAttrbute(key,value)的值

sessionScope整体的意思是获得存放在session.setAttrbute(key,value)的值  

本文转载自熊佳佳《sessionScope整体的意思是获得存放在session.setAttrbute(key,value)的值》
 
package com.demo.struts2;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport {

    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        if(getUsername().equals("admin")&&getPassword().equals("admin")){
            ActionContext.getContext().getSession().put("user", getUsername());
            return SUCCESS;
        }else{
            return ERROR;
        }
    }
    
}
--------------------------------------------------------------------------------------------
<%@ taglib prefix="s" uri="/struts-tags" %>
<body>
      <s:text name="succTip">
          <s:param>${sessionScope.user}</s:param>
      </s:text>
  </body>
========================================================
package com.demo.struts2;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport {

    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
            ActionContext ctx=ActionContext.getContext();
            Integer counter=(Integer)ctx.getApplication().get("counter");
            if(counter==null){
                counter=1;
            }else{
                counter=counter+1;
            }
        
    ctx.getApplication().put("counter", counter);
            ctx.getSession().put("user", getUsername());
            if(getUsername().equals("root")&&getPassword().equals("admin")){
                ctx.put("tip", "服务器提示:您已经成功登陆");
                return SUCCESS;
            }else{
                ctx.put("tip", "服务器提示:登陆失败");
                return ERROR;
            }

    
    }
    
}
--------------------------------------------------------------------------------------------
<%@ taglib prefix="s" uri="/struts-tags" %>
  <body>
本站访问次数:${applicationScope.counter}<br> 
${sessionScope.user }您已经登陆<br>
${request.tip }
<br>
  </body>