request, session, application of struts2

5026 단어 application
1. The first method (implementing RequestAware, SessionAware, ApplicationAware interfaces)
This is the most common method
(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. The second method (using the constructor with the ActionContext function)
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";
}
}

좋은 웹페이지 즐겨찾기