当前位置 > 首页 > Java

struts2 页面元素获取

12/5/2010 12:10:00 AM来源:Java

//1

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;
}
?
?
}

//2

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;
?}
?
?
}

//3

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;
}
?
}

//4

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();
?}
?
}
//第二种推荐使用



  • J2EE中的业务逻辑和数据库访问决策说明

    12/16/2010 6:29:00 AM

    业务逻辑和数据库访问决策 这里有2种完全不同的方法来设计JAVA企业程序,其中一种选择是采用标准EJB2实现途径(approach)。我更愿意称这种方法为重量级实现途径,当你使用重量级实现途径时

  • hibernate的Could not execute JDBC batch update错误原因及处理

    12/5/2010 8:16:00 PM

    struts +hibernate进行开发,出现的Could not execute JDBC batch update 的错误,搜索一下原因,是因为表的主键采用关键字id作为列名引起的,据说,命

  • spring 注入 cronExpression

    1/2/2011 7:45:00 PM

    这是时间的设置规则 org.springframework.scheduling.quartz.CronTriggerBean允许你更精确地控制任务的运行时间,只需要设置其cronExpressio

  • 什么地方适合使用Hibernate?

    5/11/2011 12:01:00 AM

    可以从Java应用程序直接调用 Hibernate ,也可以通过另一个框架访问 Hibernate ,也可以从Swing应用程序,servlet,portlet,JSP页面或者能够访问数据库

  • Spring实现国家化

    11/11/2010 8:52:00 AM

    ApplicationContext继承了接口 MessageSource ,所以可以用来处理消息实现国际化 在 MessageSource 中有三个同名的方法getMessage() 第一个:

  • OpenSolaris Man Page Consolidation

    2/24/2007 8:34:00 PM

    The Man Download Center includes SunOS man pages. Check it out -- thousands of SunOS man pages are

  • 关于JBoss入侵和防御!

    11/3/2011 12:28:00 PM

    在默认安装JBOSS,http://localhost:8080就会有一个跟apachetomcat一样的漏洞,此漏洞可以上传war格式 的文件,系统会自动打开并解析此文件。如果war里面是一个J

  • Hibernate--抓取策略fetch

    10/19/2011 6:10:00 AM

    抓取策略,处理的是关联的对象...比如,在一对多关系中,我们拿student表和clas做例子..在每个student实体类中,有一个clas实体的引用..而在clas实体中有一个student实体

  • 关于Spring JAR包详解

    4/16/2011 12:22:00 AM

    spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了 spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-m

  • 引用Tomcat配置虚拟主机

    10/7/2010 9:25:00 PM

    Tomcat 2010-10-0619:50:01阅读0评论0字号:大中小订阅引用【轰隆隆】的 Tomcat 配置虚拟主机 一、打开 tomcat 安装目录下conf/server.