struts2的request,session,application

1.第一种方法(实现RequestAware,SessionAware, ApplicationAware接口)

这是最常用的一种方法

(1)WebParaAction.java

package com.ru.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.ActionSupport;
import com.ru.domain.User;
//实现RequestAware,SessionAware, ApplicationAware接口
public class webpara extends ActionSupport implements RequestAware,
 SessionAware, ApplicationAware {
//创建对象,map类型
private Map<String,Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
//接收参数
private User user;
public User getUser() {
 return user;
}
public void setUser(User user) {
 this.user = user;
}

@Override
public String execute() throws Exception {
 // TODO Auto-generated method stub
 if(user.getName().equals("如")&&user.getPassword().equals("123")){
//调用set方法将属性放到request,session,application里面
 request.put("name", user.getName());
 session.put("name", user.getName());
 application.put("name", user.getName());
 return "para";
 }else{
 return "error";
 }
 

}
//分别实现接口的方法
@Override
public void setRequest(Map<String, Object> request) {
 // TODO Auto-generated method stub
 this.request=request;
}
@Override
public void setSession(Map<String, Object> session) {
 // TODO Auto-generated method stub
 this.session=session;
}
@Override
public void setApplication(Map<String, Object> application) {
 // TODO Auto-generated method stub
 this.application=application;
}
}
 

(2)webpara.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
//添加struts2标签
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>">
 
 <title>My JSP 'webpara.jsp' starting page</title>
 
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0"> 
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
 </head>

 <body>
 <h1>这个页面获取request,session,application</h1>
 欢迎登录
 <h2>通过request用两种方法获取用户名</h2>
 <s:property value="#request.name"/>
 <%=request.getAttribute("name") %>
 <h2>通过session用两种方法获取用户名</h2>
 <s:property value="#session.name"/>
 <%=session.getAttribute("name") %>
 <h2>application用两种方法获取用户名</h2>
 <s:property value="#application.name"/>
 <%=application.getAttribute("name") %>
 <s:debug />
</html>
 


2.第二种方法(使用构造函数配合ActionContext函数)

package com.ru.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.ru.domain.User;

public class WebParaAction extends ActionSupport {
//创建map类型的request,session和application
private Map request1;
private Map session1;
private Map application1;
//从jsp页面获取参数
private User user;
public User getUser() {
return user;
}


public void setUser(User user) {
this.user = user;
}

//通过构造函数将属性放入
public WebParaAction() {
request1=(Map)ActionContext.getContext().get("request");
session1=ActionContext.getContext().getSession();
application1=ActionContext.getContext().getApplication();
}

//分别调用构造函数中的函数放入属性
@Override
public String execute() throws Exception {
request1.put("name",user.getName());
session1.put("name", user.getName());
application1.put("name", user.getName());
return "ok";
}
}
 

你可能感兴趣的:(application)