zoukankan      html  css  js  c++  java
  • Struts2--访问web元素

    后台action不能通过request等获取前台数据, 如果有人登陆了, 会往session设定值, 怎么实现呢?

    action如何往session里设定值?

    所以action访问 request, session,application等web元素的方法?

    总结:

    1. jsp调用action

    2. action里 用map的IoC方法给session赋值

      public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware

      session.put("s1", "name");

    3. 在jsp显示里用  <s:property value="#request.变量名"/> 调用变量

       也可以通过 <s:property value="#parameters.name"/> 

     

    只介绍一种最常用的:

    1. jsp页面显示: 4个按钮分别提交同一个form的方法:

    <form name="f" action="" method="post">
    用户名:<input type="text" name="name"/>
    密码:<input type="text" name="password"/>
    <br />
    <input type="button" value="submit1" onclick="javascript:document.f.action='login/login1';document.f.submit();" />
    <input type="button" value="submit2" onclick="javascript:document.f.action='login/login2';document.f.submit();" />
    <input type="button" value="submit3" onclick="javascript:document.f.action='login/login3';document.f.submit();" />
    <input type="button" value="submit4" onclick="javascript:document.f.action='login/login4';document.f.submit();" />
    </form>
    

     

    2. struts.xml配置:

     <package name="login" extends="struts-default" namespace="/login">
            <action name="login*" class="com.bjsxt.struts2.user.action.LoginAction{1}">
                <result>/user_login_success.jsp</result>
            </action>
        </package>
    

    3. action 的4种处理方法:

    loginAction1.java: 取得Map类型的方法

    package com.bjsxt.struts2.user.action;
    
    import java.util.Map;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction1 extends ActionSupport {
    	
    	private Map request;
    	private Map session;
    	private Map application;
    	
    	public LoginAction1() {
    		request = (Map)ActionContext.getContext().get("request");
    		session = ActionContext.getContext().getSession();
    		application = ActionContext.getContext().getApplication();
    	}
    	
    	public String execute() {
    		request.put("r1", "r1");
    		session.put("s1", "s1");
    		application.put("a1", "a1");
    		return SUCCESS; 
    	}	
    }
    

    loginAction2.java: 取得Map类型的 IoC方法: 最常用

    package com.bjsxt.struts2.user.action;
    
    import java.util.Map;
    
    import org.apache.struts2.interceptor.ApplicationAware;
    import org.apache.struts2.interceptor.RequestAware;
    import org.apache.struts2.interceptor.SessionAware;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware {
    	
    	private Map<String, Object> request;
    	private Map<String, Object> session;
    	private Map<String, Object> application;
    	
    	//DI dependency injection
    	//IoC inverse of control
    	public String execute() {
    		request.put("r1", "r1");
    		session.put("s1", "s1");
    		application.put("a1", "a1");
    		return SUCCESS; 
    	}
    
    	@Override
    	public void setRequest(Map<String, Object> request) {
    		this.request = request;
    	}
    
    	@Override
    	public void setSession(Map<String, Object> session) {
    		this.session = session;
    	}
    
    	@Override
    	public void setApplication(Map<String, Object> application) {
    		this.application = application;
    	}	
    }
    

    loginAction3.java: 取得真实类型的 方法:  

    package com.bjsxt.struts2.user.action;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction3 extends ActionSupport {
    	
    	private HttpServletRequest request;
    	private HttpSession session;
    	private ServletContext application;
    	
    	public LoginAction3() {
    		request = ServletActionContext.getRequest();
    		session = request.getSession();
    		application = session.getServletContext();
    	}
    	
    	public String execute() {
    		request.setAttribute("r1", "r1");
    		session.setAttribute("s1", "s1");
    		application.setAttribute("a1", "a1");
    		return SUCCESS; 
    	}
    	
    }
    

    loginAction4.java: 取得真实类型的 IoC 方法:

    package com.bjsxt.struts2.user.action;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    import org.apache.struts2.interceptor.ServletRequestAware;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction4 extends ActionSupport implements ServletRequestAware {
    	
    	private HttpServletRequest request;
    	private HttpSession session;
    	private ServletContext application;	
    	
    	public String execute() {
    		request.setAttribute("r1", "r1");
    		session.setAttribute("s1", "s1");
    		application.setAttribute("a1", "a1");
    		return SUCCESS; 
    	}
    
    	@Override
    	public void setServletRequest(HttpServletRequest request) {
    		this.request = request;
    		this.session = request.getSession();
    		this.application = session.getServletContext();
    	}	
    }
    

      

    最后如何在jsp中拿到request等值:  <s:property value="#request.变量名"/> 

    #代表提取的不是 valuestack里的内容,而是context里的值

    <body>
    	User Login Success!
    	<br />
    	<s:property value="#request.r1"/> | <%=request.getAttribute("r1") %> <br />
    	<s:property value="#session.s1"/> | <%=session.getAttribute("s1") %> <br />
    	<s:property value="#application.a1"/> | <%=application.getAttribute("a1") %> <br />
    	<s:property value="#parameters.name"/>  <br />
    	<s:property value="#parameters.password"/> <br />
    	<s:property value="#attr.a1"/><br />
    	<s:property value="#attr.s1"/><br />
    	<s:property value="#attr.r1"/><br />
    	<s:debug></s:debug>
    	<br />
    </body>
    

      

  • 相关阅读:
    Android Jetpack之WorkManager: 观察结果
    解决'androidx.arch.core:core-runtime' has different version for the compile (2.0.0) and runtime (2.0.1)
    我要研究一下minio,管理大量的照片
    分发消息的写法
    百度地图坐标转换
    HighChart 实现从后台取数据来实时更新柱状和折线组图
    导出Excel
    Java 8新特性之集合
    java中的Switch case语句
    提问:"~"运算符
  • 原文地址:https://www.cnblogs.com/wujixing/p/5202723.html
Copyright © 2011-2022 走看看