zoukankan      html  css  js  c++  java
  • struts不同session范围添加、访问属性

    第01步:编写bean类

    package com.self.bean;
    
    import java.util.Date;
    
    public class User {
        private Date birthday = null;
    
        public Date getBirthday() {
            System.out.println("获得-User:" + birthday);
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            System.out.println("设置-User:" + birthday);
            this.birthday = birthday;
        }
    
    }

    第02步:编写action类

    package com.self.action;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.self.bean.User;
    
    public class ThreeScopes {
    
        private User lilan;
    
        public ThreeScopes() {
        }
        
        /**向3种范围加入值**/
        public String showdata() {
            /**方式01**/
            ActionContext ac= ActionContext.getContext();
            ac.put("request", "请求范围01");//向request范围加入值
            ac.getSession().put("session", "会话范围01");//向Session范围加入值
            ac.getApplication().put("app", "应用范围01");//向应用范围加入值
            
            /**方式02**/
            ServletContext sc=ServletActionContext.getServletContext();//应用范围
            HttpServletRequest request=ServletActionContext.getRequest();//会话范围
            request.setAttribute("request2", "请求范围2");
            request.getSession().setAttribute("session2", "会话范围2");
            sc.setAttribute("app2", "应用范围2");
            return "tsuccess";
        }
    
        public User getLilan() {
            return lilan;
        }
    
        public void setLilan(User lilan) {
            this.lilan = lilan;
        }
    
    }

    第03步:配置struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
        <constant name="struts.action.extension" value="do"/>
        
        <package name="transform" namespace="/" extends="struts-default">
            <action name="list_*" class="com.self.action.ThreeScopes" method="{1}">
                <result name="tsuccess">
                    /outdata.jsp
                </result>
            </action>
        </package>
    </struts>

    第04步:编写访问action界面

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <html>
      <body>
          <form action="list_showdata.do">
              <BR>
              <input type="submit" value="访问action,显示结果">
          </form>
      </body>
    </html>

    第05步:编写数据显示界面

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <html>
      <body>
          访问不同范围属性值01:<BR>
        ${applicationScope.app }<BR>
        ${sessionScope.session }<BR>
        ${requestScope.request }<BR><BR><BR>
          访问不同范围属性值02:<BR>
        ${applicationScope.app2 }<BR>
        ${sessionScope.session2 }<BR>
        ${requestScope.request2 }<BR>
      </body>
    </html>
  • 相关阅读:
    我们为何要使用多线程,它有什么优点?
    Java并发和多线程那些事儿
    【BJG吐槽汇】第2期
    【BJG吐槽汇】第一期
    360:且用且珍惜!解决虚拟机linux启动缓慢以及ssh端卡顿的问题!
    多个不同的app应用间应该如何进行消息推送呢?
    JSONResult 封装
    MySQL 优化集锦
    学习bootstrap3
    开发一个响应式的静态网站---实战
  • 原文地址:https://www.cnblogs.com/zjsy/p/4293578.html
Copyright © 2011-2022 走看看